Accessing object's method instead of M2Doc service - m2doc

I'm trying to access a method of a (Java) object, but M2Doc always tries to find a suitable service instead, which cannot be found. Is there a way to access the method?
Concretely, I have a variable, which contains a BigDecimal object. If I use {m:myBigDecimal}, then the full number is printed, typically with a lot of decimals: 1146.730013716953713173.
To truncate that, I'd prefer to use the setScale(int) method of BigDecimal, like {m:myBigDecimal.setScale(2)}, but this results in Couldn't find the 'setScale(java.math.BigDecimal,java.lang.Integer)' service.
Is there a way to use this method (and ideally generally other methods as well) without creating a dedicated M2Doc service for this?
Thanks!

Related

Is there a way to create a constrained data type in Clojure?

As an example, a string that contains only a valid email address, as defined by some regex.
If a field of this type would be a part of a more complex data structure, or would be used as a function parameter, or used in any other context, the client code would be able to assume the field is a string containing a valid email address. Thus, no checks like "valid?" should be ever necessary, so approach of domaintypes would not work.
In Haskell this could be accomplished by a smart constructor (section 1.2) and in Java by ensuring the type is immutable (all setters private) and by adding a check in the constructor that throws a RuntimeException if the string used to create the type doesn't contain a valid email address.
If this is impossible in plain Clojure, I would like to see an example implementation in some well known extensions of the language, like Typed Clojure.
Ok, maybe, I understand now a question and I formulate in the comment my thoughts not really well. So I try to suggest an admissible solution to your question and then I try to explain some ideas I tried to tell in the comment.
1) There is a gen-class that generates compiled bytecode for a class and you can set constructor for the class there.
2) You can create a record with defrecord in some namespace that is private by convention in your project, then you
create another namespace with public api and define your factory function here. So the user of your public namespace will be able to call only public functions of your public namespace. (Of course, he can call also private ones, but with some another code)
3) You can just define a function like make-email that will return a map.
So you didn't specify your data structure anywhere.
4) You can just document your code where you will warn people to use the factory function for construction.
But! In Java if your code requires some interface, then it's user problem to give to your code the valid interface implementation. So if you write even a little bit general code in Java you already has lost the property of the valid email string. This stuff with interfaces is because Java is statically typed language.
Clojure is, in general, dynamically typed, so the user, in general, should be able to pass arbitrary data structure to arbitrary function without any type problems in compile time and it's his fault if he pass the wrong data. That makes, for example, this thing possible: You create a record and create a factory (constructor) function. And you expect a record to be passed in your code. But the user can pass a map with the same keys as your record fields names and the code will work.
So, in general, if you want the user of your code to be responsible for passing a required typed in dynamically typed language, then it cost nothing for user to be responsible for constructing it in a correct way that you provide to him.
Another solutions are: User just write tests. You can specify in your api functions :pre and :post conditions to check the structure. You can use typed clojure with the ideas I wrote above. And you can use some additional declarative libraries, like that was mentioned in the first comment of #Thumbnail.
P.S. I'm not a clojure professional, so I could easily miss some better solutions.

Best practices with Ember ArrayProxy

Ember's Em.ArrayProxy and Em.Array have many programatic methods available for notifying observers of changes to content. For example:
arrayContentDidChange
arrayContentWillChange
enumerableContentDidChange
enumerableContentWillChange
contentArrayWillChange
Em.ArrayProxy also has several methods for manipulating the ArrayProxy's content. For example:
this.pushObject('something random');
// Or
this.insertAt(2, 'something random');
When using the latter methods, does one have to use them in conjunction with the former methods? It seems silly that Ember's usually-automated property observers would require a manual kick here but I don't find the documentation very clear.
No, you don't have to use any methods in conjunction.
If you want to add items to your ArrayProxy, simply pushObject(). You would know this by just using the method and seeing that it just works.
From the docs:
This mixin implements Observer-friendly Array-like behavior. It is not a concrete implementation, but it can be used up by other classes that want to appear like arrays.
http://emberjs.com/api/classes/Ember.Array.html
Ember.Array is a type of class that in other programming languages (without mixins) receives the name of an interface.
An ArrayProxy wraps any other object that implements Ember.Array
http://emberjs.com/api/classes/Ember.ArrayProxy.html
Ember.ArrayProxy is exactly what the name says, a proxy, that wraps around any object that has implemented the Ember.Array interface.
The other methods, that you mention, might be implemented/overridden if you are making your own "subclass" of Ember.Array. Some must be implement to make your subclass ArrayProxy friendly. Or if you want to add custom behaviour, maybe write to a log whenever arrayContentDidChange, then you override that method and add whatever logic your app needs.
That is Object Oriented Programming and all those explanations are out of scope for the documentation of any framework.
Are you asking whether pushObject et cetera trigger those events?
From the documentation for insertAt:
This will use the primitive replace() method to insert an object at the specified index.
From the documentation for replace:
You should also call this.enumerableContentDidChange()
So, yes, a properly implemented ArrayProxy will trigger those events when you add or remove things.

Java's "UserContext" equivalent in Django

I'm writing REST services using Django Rest Framework. I want to keep some of the values like Language Code, AppId in a global variable (not static) and access it where ever I want. This may change for every request. In Java we call it as "UserContext". So that I can parse my header and assign the values into that and access it in Data or View Layer (for example). This will help me to avoid passing the values in every method. At the same it has to maintain life cycle for each request.
Is there anything like that in Django?
The plain answer is that django-tls (https://github.com/aino/django-tls) can make request available everywhere where you import it, and then you can just set attributes on request, request.user, or something similar.
That said, in 99% of the cases this is a bad idea, for instance if all you need it for is to avoid passing values to functions then it's extremely ill advised. You'll just make your code unreadable. If you have a lot of variables to pass around, maybe some of them need combining into some class?

Robust method for making a copy of a TRemotable object in Delphi 2007

Is there a robust mechanism for making a copy of TRemotable object (or descendant) in Delphi 2007?
I'm creating a Delphi web service client that receives a variety of objects from a web service; of course, all are descendants of TRemotable. In the client, I create a matching object, then copy all the properties from the web service provided TRemotable to my own object. This is done via TypInfo.GetPropList() and then a loop around TypInfo. GetPropValue() and TypInfo.SetPropValue() method calls. Although this works great for the simple types - strings, ints, bool, etc, it doesn't work for complex types, like dates, times or sub-object types. And I assume that if the web service ever makes use of a new complex type, my copy code would also break.
It looks like one possibility is to serialize the object out to an XML document, then read it into the new object. But this seems like a great deal of overhead to just copy a series of properties around.
Found a more robust solution, seems to work fine for simply types, TXS... derivative types and subobject types:
procedure CopyNormal(Source, Target: TRemotable);
var
Converter: IObjConverter;
NodeObject: IXMLNode;
NodeParent: IXMLNode;
NodeRoot: IXMLNode;
XML: IXMLDocument;
XMLStr: WideString;
begin
XML:= NewXMLDocument;
NodeRoot:= XML.AddChild('Root');
NodeParent:= NodeRoot.AddChild('Parent');
Converter:= TSOAPDomConv.Create(NIL);
NodeObject:= Source.ObjectToSOAP(NodeRoot, NodeParent, Converter, 'CopyObject', '', [ocoDontPrefixNode], XMLStr);
Target.SOAPToObject(NodeRoot, NodeObject, Converter);
end;
TDateTime is just a Double by a different name, and you ought to be able to copy it without trouble. Or are you using some other format for your dates and times?
As for sub-objects, they can be handled by making your copy routine recursive. If it comes across a property that's an object, have it make a copy of that object and assign it to the parent object. (NOTE: This will only work if the sub-object also has published properties.)
Yes you could make the copy recursive, but that still leaves you with problems, how to copy internal private fields. You would have to expose all fields as properties and in my opinion that is not clean and is cumbersome.
I would definitely serialize the object. SOAP has so much overhead on its own that serialization is super fast in comparison. I would even argue that a simple HTTP approach using REST would be better.
You can look at my SimpleStorage framework that was made with such tasks (serialization) in mind. Especially look at adapters feature.
You can get it at: http://www.cromis.net/blog/downloads/
There are also articles there that show how to use it. If you are already using other XML library and don't want to switch I would still prefer the serialization approach if I were you.
I doubt that you can make a http request last under 30ms. Serialization would take way less than that. Now add the SOAP overhead and you are super fast compared to it :)

Consuming custom objects between webservices

I have a webservice that is designed to accept performance data via a custom object. The custom object contains a Collection (Generic List) of performance measures among other data. The performance measure consists of simple data types (strings, ints, and a datetime). The only method exposed by the webservice requires this custom object (performance data object) to be passed in.
The problem lies in using this custom object externally. I wish to use the Add() and Item() methods of the Generic List class along with various other features within this class within another webservice. If I request the object from the Performance Data Webservice it seralizes the inner collection to an arrayList. I would like it to remain a generic collection.
I have toyed with using the XmlInclude method but currently havent found a solution with it.
The next thing I tried to do was create an assembly of this specific object that both the Peformance Data web service can use and any satelite programs (i.e. another webservice). The issue here is when I try to pass in the custom object created by the seperate assembly the performance data webservice barks its a different type. (Also I am applying the XmlInclude(GetType( custom assembly)) attribute to the exposed method). However still thinks the types are not convertable.
Note: I would prefer to call the Performance Data WS to get the custom object instead of having to deal with adding assemblies to each project that needs access.
Anyone have an idea other than restructing the program to work with methods exposed by the ArrayList?
If you use WCF, you can configure what type of collection comes out, whether an ArrayList, a fixed array, or a generic List.
I have found a solution that will work with .Net 2.0. By using Web Services Contract First (WSCF http://www.thinktecture.com/resourcearchive/tools-and-software/wscf/wscf-walkthrough)
I was able to pass generic collections between two services. A down side to WSCF, as the name suggests, is the approach requires the use of contract-first instead of the more common code-first methodology. Lucky it is not terribly complicated to modify the class and proxy after they are created. Hope this helps any lost travelers...