Class: Codependent::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/codependent/container.rb

Constant Summary collapse

CONFIG_BLOCK_MISSING_ERROR =
'You must provide a config block.'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, &block) ⇒ Container

Returns a new instance of Container.



8
9
10
11
12
# File 'lib/codependent/container.rb', line 8

def initialize(args = {}, &block)
  @injectables = {}

  instance_exec(args, &block) if block
end

Instance Method Details

#injectable(id) ⇒ Object



26
27
28
# File 'lib/codependent/container.rb', line 26

def injectable(id)
  injectables[id]
end

#injectable?(id) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/codependent/container.rb', line 30

def injectable?(id)
  injectables.key?(id)
end

#instance(id, &config_block) ⇒ Object



14
15
16
17
18
# File 'lib/codependent/container.rb', line 14

def instance(id, &config_block)
  validate_config_arguments(config_block)

  add_injectable!(id, :instance, config_block)
end

#resolve(id) ⇒ Object



34
35
36
37
38
# File 'lib/codependent/container.rb', line 34

def resolve(id)
  return unless injectable?(id)

  resolver.resolve(id)
end

#singleton(id, &config_block) ⇒ Object



20
21
22
23
24
# File 'lib/codependent/container.rb', line 20

def singleton(id, &config_block)
  validate_config_arguments(config_block)

  add_injectable!(id, :singleton, config_block)
end