Class: Hypo::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/hypo/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, container) ⇒ Component

Returns a new instance of Component.



5
6
7
8
9
# File 'lib/hypo/component.rb', line 5

def initialize(type, container)
  @type = type
  @name = type.name.gsub(/(.)([A-Z](?=[a-z]))/,'\1_\2').delete('::').downcase.to_sym
  @container = container
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/hypo/component.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/hypo/component.rb', line 3

def type
  @type
end

Instance Method Details

#instanceObject



11
12
13
14
15
16
# File 'lib/hypo/component.rb', line 11

def instance
  dependencies = @type.instance_method(:initialize).parameters.map{|p| @container.resolve(p[1])}
  @instance = @type.new(*dependencies) if @instance.nil?

  @instance
end