[Nitro] Singletons

transfire at gmail.com transfire at gmail.com
Sun Feb 4 15:05:49 EST 2007



On Feb 4, 1:33 pm, transf... at gmail.com wrote:
> On Feb 4, 12:23 pm, "George Moschovitis"
>
> <george.moschovi... at gmail.com> wrote:
> > >  http://onestepback.org/index.cgi/Tech/Ruby/
> > > DependencyInjectionInRuby.rdoc
>
> > I don't really want to add DI, IOC etc to Nitro. I would like to simplify
> > Nitro and make it more accessible not include the most advanced concepts...
> > What do others think?
>
> Simple is good. I wouldn't want it if it's not so. Here's just a rough
> concept, maybe this can give you more of a concrete idea of how it
> could work and not be complex:
>
>   class Class
>     def needs *a
>       (@needs ||= []).concat(a.flatten.collect{|e|e.to_sym})
>     end
>     def need?(name)
>       @needs.include?(name.to_sym)
>     end
>   end
>
>   def Nitro.inject( services )
>     ObjectSpace.each_object(Class) do |k|
>       if k.needs?(:server)
>         k.class_eval do
>           define_method{:server){ services[:server] }
>         end
>       end
>     end
>   end
>
>   my_server = Nitro::Server.new
>
>   Nitro.inject( :server => my_server )
>
>  class Nitro::Element
>    needs :server
>  end

Hmm... I guess that is too complex. b/c why not just:

  $services = {}

  class Class
    def needs(name)
      define_method(name){ $services[name] }
    end
  end

  class Nitro::Element
    needs :server
  end

  my_server = Nitro::Server.new

  $services[:server] = my_server

Okay, i'll shut up now... ;-)

T.



More information about the Nitro-general mailing list