During my daily work in another framework, we are able to do something very handy like so: myThing := Thing findOrCreateByKey: 'red_thing'. where the key is some underscored all lower case unique name for an object we will be calling all the time. this will return either a new thing with a key of 'red_thing' or the current one that already exists. We can also do something like: myThing updateWithDictionary: aDictionary. and it will flip through the dictionary and assign values to the instance variables. The above is very handy. I am currently creating a bunch of methods that populate my image with real objects, and could use these methods. My question is. Does a behavior like this already exist? If not, I'd like to add these methods to my project (in monticello). They will be used with several different objects, so should I add the methods to Object? I would imagine this is something I would want to use a great deal. How would I go aobut making this something I could include easily in further projects? I realize that the 'updateWithDictionary' is very inefficient, but this will just be somethign that runs once, during data import, and never again. I just want to make sure I am not doing anything whacky. Thanks!
|
Hi - comments below
In your example above what type of object is Thing? If the object stored at red_thing is to be a global then you'd want Thing>>findOrCreateByKey: aKey ^Smalltalk at: aKey ifAbsentPut:[self createThingFor:aKey] Look at the senders/implementors of #at:ifAbsentPut: for other ideas e.g. Dictionary>>#at:ifAbsentPut: MyClass>>updateWithDictionary: aDictionary aDictionary keysAndValuesDo:[:key :value| self perform: key asMutator with: value].
|
oh! Thing, in this case, is just an object.. that extends Object.. where 'red_thing' is just a string..
so, myThing's key would just be a string.. that way, i could do something like myThing := Thing getByKey: 'red_thing' .. or myThing := Thing getByKey: 'blue_thing' yes, this will be a global thing.. >> Thing>>findOrCreateByKey: aKey >> ^Smalltalk at: aKey ifAbsentPut:[self createThingFor:aKey] i am not sure this is what i want to do.. i jus want to use the above to create a bunch of object.. then later, do something like: newThing := Thing findByKey: 'green_thing'. the above looks like i would have a nebulous 'key'.. and from there, i can use it to grab my instance of Thing. is that correct? oh! and asMutator is what i was looking for.. thanks so much! On Tue Jan 13 2015 at 1:07:42 PM Paul DeBruicker <[hidden email]> wrote:
Hi - comments below |
Free forum by Nabble | Edit this page |