Class: Ruice::Container
- Inherits:
-
Object
- Object
- Ruice::Container
- Defined in:
- lib/ruice/container.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Instance Method Summary collapse
-
#initialize(properties = {}, env = 'default') ⇒ Container
constructor
A new instance of Container.
- #lookup_property(name, default = nil) ⇒ Object
- #request(name) ⇒ Object
- #request_new(name) ⇒ Object
- #with(name, subject) ⇒ Object
Constructor Details
#initialize(properties = {}, env = 'default') ⇒ Container
Returns a new instance of Container.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ruice/container.rb', line 21 def initialize(properties = {}, env = 'default') raise ArgumentError, 'Container properties can not be nil' if properties.nil? raise ArgumentError, 'Container properties is not a Hash' unless properties.is_a? Hash raise ArgumentError, 'Environment can not be nil' if env.nil? raise ArgumentError, 'Environment must be a string' unless env.is_a? String properties[:env] = env @properties = properties @env = env.to_sym @bindings = {} @instances = {} end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
36 37 38 |
# File 'lib/ruice/container.rb', line 36 def env @env end |
Instance Method Details
#lookup_property(name, default = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ruice/container.rb', line 38 def lookup_property(name, default = nil) path = name.split '.' current = @properties path.each do |key_part| break if current.nil? raise Exception, 'Can not access value subkey for non-hash ' + current unless current.is_a? Hash sym_part = key_part.to_sym current = current.fetch(sym_part, nil) || current.fetch(key_part, nil) end current || default end |
#request(name) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ruice/container.rb', line 54 def request(name) return self if name == Ruice::Container return @instances[name] if @instances.key? name instance = request_new name @instances[name] = instance instance end |
#request_new(name) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ruice/container.rb', line 66 def request_new(name) return self if name == Ruice::Container return @bindings[name].call self if @bindings.key? name raise ArgumentError, 'Dependency name is not class, and no bindings are present' unless name.respond_to? :new instance = name.new vars = instance.instance_variables vars.each do |it| value = instance.instance_variable_get it next unless value.is_a?(Dependency) || value.is_a?(Property) replacement = nil replacement = lookup_property value.name, value.default if value.is_a? Property if value.is_a? Dependency replacement = if value.is_fresh request_new value.target else request value.target end end instance.instance_variable_set it, replacement end instance.dic_ready if instance.methods.include? :dic_ready instance end |
#with(name, subject) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruice/container.rb', line 100 def with(name, subject) if subject.is_a? Proc raise ArgumentError, 'Duplicate provider - ' + name if @bindings.key? name @bindings[name] = subject return end raise ArgumentError, 'Duplicate instance - ' + name if @instances.key? name @instances[name] = subject self end |