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
# 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]}
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



30
31
32
# File 'lib/hypo/component.rb', line 30

def dependencies
  @dependency_names.map { |dependency| @container.resolve(dependency) }
end

#instanceObject



20
21
22
23
24
25
26
27
28
# File 'lib/hypo/component.rb', line 20

def instance
  instance = @lifetime.instance(self)

  @dependency_names.each do |dependency|
    instance.instance_variable_set "@#{dependency}".to_sym, @container.resolve(dependency)
  end

  instance
end