[Nitro] [ANN] Facets 1.8 (RC)

transfire at gmail.com transfire at gmail.com
Sat Jan 27 20:55:00 EST 2007

On Jan 27, 6:35 pm, "George Moschovitis" 
<george.moschovi... at gmail.com> wrote:
> Tom,
> here is a pattern that emerges often in Nitro/Og source code:
> klass.ann(:self, :table => []) unless klass.ann(:self, :table)
> klass.ann!(:self, :table) << info
> can you propose a shortcut or idiom for this common pattern?

maybe. something like this might work:

  require 'facets/core/hash/insert'

  klass.ann(:self).insert(:table, [])
  klass.ann!(:self, :table) << info

Its a little shorter at least. there may be other ways, i'd have to 
play with it. also, #insert is new brand new method in Facets. on ruby-
talk I asked about what best to call it. i'm still not 100% that's the 
best name, but in anycase it will only store a value if the key 
doesn't alrady exist and it returns true or false whterh the value was 
stored by the command or not.

Of course, if all else fails, at the very least nitro could define a 
special method:

  class Module
    def ann_insert_array(ref, key, info=nil)
      klass.ann(ref, key => []) unless klass.ann(ref, key)
      klass.ann!(ref, key) << info if info

or something like that.

BTW. i'm thinking allowing the notation 'klass.ann(:self).table'. this 
can be done in a light-weight manner by returning a subclass of 
OpenObject instead of a Hash when only the annotation reference is 
given, eg. 'klass.ann(:self)'  but i'm not 100% sure this a good idea. 
What we can't do for sure is 'klass.ann.self' notation. That we have 
to give up b/c it makes things too complicated.


More information about the Nitro-general mailing list