Class: Jerry

Inherits:
Object
  • Object
show all
Defined in:
lib/jerry.rb,
lib/jerry/config.rb,
lib/jerry/version.rb

Overview

Inversion of Control container.

This class is in charge of bootstrapping your application. This is done by defining configs.

Examples:

class MyConfig < Jerry::Config
  component(:app) { MyApp.new }
end
jerry = Jerry.new MyConfig.new
jerry.rig :app #=> #<MyApp:...>

Defined Under Namespace

Classes: ComponentError, Config, RigError

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(*configs) ⇒ Jerry

Returns a new instance of Jerry.

Parameters:

  • configs (Jerry::Config)

    Configs used to rig components. Multiple config can be given. If two configs define the same component, the later config will have priority.



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

def initialize(*configs)
  @index = {}

  configs.each { |config| self << config }
end

Instance Method Details

#<<(config) ⇒ Object

Load a config

Parameters:

  • config (Jerry::Config)

    Config to be loaded. If the loaded config defines a component already defined by another config, the component from the new config will take priority.



30
31
32
33
34
# File 'lib/jerry.rb', line 30

def <<(config)
  components = config.components
  components.each { |component| @index[component] = config }
  config.jerry = self
end

#knows?(component) ⇒ Boolean

Checks if a component exists

Parameters:

  • component (Symbol)

    component to check

Returns:

  • (Boolean)


50
51
52
# File 'lib/jerry.rb', line 50

def knows?(component)
  @index.has_key? component
end

#rig(component) ⇒ Object

Rigs a component

Parameters:

  • component (Symbol)

    Component to rig.

Returns:

  • The component requested

Raises:



41
42
43
44
45
# File 'lib/jerry.rb', line 41

def rig(component)
  raise RigError, "could not find component #{component}" unless knows? component

  @index[component].public_send component
end