Hi,
> On 17 Oct 2017, at 11:46, Herby Vojčík <
[hidden email]> wrote:
>
> newFromAccessors: aCollection ofObject: anObject
> "Performs supplied accessors on anObject
> and returns my instance with accessor -> result pairs."
>
> ^ self newFrom: (aCollection collect: [ :each | each -> (anObject perform: each) ])
>
> "I use it for example in this piece of code:
> NeoJSONObject
> newFromAccessors: #(good bad)
> ofObject: self dao sumOfAllAnswers
> "
If (but see further) I would add this kind of functionality, I would go for the following methods in a 'copying' protocol on the instance side (in analogy with Object>>#copyFrom:) :
NeoJSONObject>>#copyFrom: anObject accessors: accessors
accessors do: [ :each |
self at: each put: (anObject perform: each) ]
NeoJSONObject>>#copyFrom: anObject instVarNames: instVarNames
instVarNames do: [ :each |
self at: each put: (anObject instVarNamed: each) ]
NeoJSONObject>>#copyFrom: aDictionary keys: keys
keys do: [ :each |
self at: each put: (aDictionary at: each) ]
NeoJSONObject>>#copyAllInstVarsFrom: anObject
self copyFrom: anObject instVarNames: anObject class allInstVarNames
Which would allow you to write stuff like:
NeoJSONObject new copyFrom: NeoJSONTestObject2 example1 accessors: #(id height width).
NeoJSONObject new copyFrom: NeoJSONTestObject2 example1 instVarNames: #(id height width).
NeoJSONObject new copyAllInstVarsFrom: NeoJSONTestObject2 example1.
NeoJSONObject new copyFrom: { #id->1. #height->200. #width->100 } asDictionary keys: #(width height).
However, there are a couple of reasons why I am not so enthusiastic.
- You want conversions to/from NeoJSONObject and your domain objects, by creating one for the other, that is exactly why mapping exists: to do this without copying (i.e. directly & more efficiently)
- Mapping is also better because it can be applied to values as well (recursively)
- I would rather put the responsibility of conversion to the domain objects (say in #toNeoJSONObject and #fromNeoJSONObject:)
- I would prefer NeoJSONObject to keep very simple
Just some comments, thinking aloud.
Sven