Module: Arrow::Injectable
- Defined in:
- lib/arrow/mixins.rb
Overview
Adds dependency-injection bejavior to a class. Classes which are Injectable are loadable by name, making it easier to refer to them from a configuration file or other symbolic source. Instead of classes explicitly referring to one another to satisfy their associations, these dependencies can be “injected” at runtime.
Some references for the Dependency Injection pattern:
* http://www.martinfowler.com/articles/injection.html
* http://en.wikipedia.org/wiki/Dependency_injection
Usage
# in myclass.rb require ‘arrow/mixins’
class MyClass
include Arrow::Injectable
end
# somewhere else myclass = Arrow::Injectable.load_class( “myclass” )
Class Method Summary collapse
- .derivatives ⇒ Object
-
.extend_object(obj) ⇒ Object
Make the given object (which must be a Class) injectable.
-
.included(mod) ⇒ Object
Mixin hook: extend including classes.
-
.load_class(classname) ⇒ Object
Return the Class object for the given derivative
classname
, attempting to load it if it hasn’t been already.
Instance Method Summary collapse
-
#inherited(klass) ⇒ Object
Classes which inherit from Injectable classes should be Injectable, too.
Class Method Details
.derivatives ⇒ Object
340 |
# File 'lib/arrow/mixins.rb', line 340 def self::derivatives; @derivatives; end |
.extend_object(obj) ⇒ Object
Make the given object (which must be a Class) injectable.
343 344 345 346 347 348 |
# File 'lib/arrow/mixins.rb', line 343 def self::extend_object( obj ) raise ArgumentError, "can't make a #{obj.class} Injectable" unless obj.is_a?( Class ) super @derivatives[ obj.name ] = obj end |
.included(mod) ⇒ Object
Mixin hook: extend including classes
352 353 354 355 356 |
# File 'lib/arrow/mixins.rb', line 352 def self::included( mod ) Arrow::Logger[self].debug "%s included Injectable" % [ mod.name ] mod.extend( self ) super end |
.load_class(classname) ⇒ Object
Return the Class object for the given derivative classname
, attempting to load it if it hasn’t been already.
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/arrow/mixins.rb', line 361 def self::load_class( classname ) Arrow::Logger[self].debug "Loading injectable class '#{classname}'" unless Arrow::Injectable.derivatives.include?( classname ) modname = classname.downcase.gsub( /::/, '/' ) Arrow::Logger[self].debug "Class not loaded yet. Trying to " + "load it from #{modname}" require modname or raise "%s didn't register with Injectable for some reason" % [ classname ] Arrow::Logger[self].debug "Loaded injectable class %s (%d classes loaded)" % [ classname, Arrow::Injectable.derivatives.length ] end Arrow::Injectable.derivatives[ classname ] end |
Instance Method Details
#inherited(klass) ⇒ Object
Classes which inherit from Injectable classes should be Injectable, too.
384 385 386 387 388 |
# File 'lib/arrow/mixins.rb', line 384 def inherited( klass ) Arrow::Logger[self].debug "making %s Injectable" % [ klass.name ] klass.extend( Arrow::Injectable ) super end |