Class: Hypo::Component

Inherits:
Object
  • Object
show all
Includes:
LifetimeFriendly, ScopeFriendly
Defined in:
lib/hypo/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LifetimeFriendly

#use_lifetime

Methods included from ScopeFriendly

#bind_to, #scope

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

#containerObject (readonly)

Returns the value of attribute container.



10
11
12
# File 'lib/hypo/component.rb', line 10

def container
  @container
end

#lifetimeObject (readonly)

Returns the value of attribute lifetime.



10
11
12
# File 'lib/hypo/component.rb', line 10

def lifetime
  @lifetime
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/hypo/component.rb', line 10

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/hypo/component.rb', line 10

def type
  @type
end

Instance Method Details

#dependenciesObject



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