Class: Hypo::Component
- Inherits:
-
Object
- Object
- Hypo::Component
- Includes:
- LifetimeFriendly, ScopeFriendly
- Defined in:
- lib/hypo/component.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#lifetime ⇒ Object
readonly
Returns the value of attribute lifetime.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #dependencies ⇒ Object
-
#initialize(type, container, name = nil) ⇒ Component
constructor
A new instance of Component.
- #instance(attrs = {}) ⇒ Object
Methods included from LifetimeFriendly
Methods included from ScopeFriendly
Constructor Details
#initialize(type, container, name = nil) ⇒ Component
Returns a new instance of Component.
12 13 14 15 16 17 18 19 20 |
# File 'lib/hypo/component.rb', line 12 def initialize(type, container, name = nil) @container = container @type = type @name = name || type.name.gsub(/(.)([A-Z](?=[a-z]))/, '\1_\2').delete('::').downcase.to_sym @lifetime = container.lifetimes[:transient] @dependency_names = @type.instance_method(:initialize).parameters .map{|p| p[1]} .select{|name| ![:attrs, :attributes].include?(name)} end |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
10 11 12 |
# File 'lib/hypo/component.rb', line 10 def container @container end |
#lifetime ⇒ Object (readonly)
Returns the value of attribute lifetime.
10 11 12 |
# File 'lib/hypo/component.rb', line 10 def lifetime @lifetime end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/hypo/component.rb', line 10 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/hypo/component.rb', line 10 def type @type end |
Instance Method Details
#dependencies ⇒ Object
32 33 34 |
# File 'lib/hypo/component.rb', line 32 def dependencies @dependency_names.map { |dependency| @container.resolve(dependency) } end |
#instance(attrs = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/hypo/component.rb', line 22 def instance(attrs = {}) instance = @lifetime.instance(self, attrs) @dependency_names.each do |dependency| instance.instance_variable_set "@#{dependency}".to_sym, @container.resolve(dependency) end instance end |