Class: Jerry::Config Abstract

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

Overview

This class is abstract.

Subclass to define a config

Base class for all jerry configs.

A config is a class that tells jerry about a set of available components and how those should be created

Examples:

class MyConfig < Jerry::Config
  component(:service) { MyService.new }
  component(:app) { MyApp.new rig(:service) }
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jerry=(value) ⇒ Object (writeonly)

Jerry instance the config is part of

This gets set by Jerry when it loads a config



62
63
64
# File 'lib/jerry/config.rb', line 62

def jerry=(value)
  @jerry = value
end

Class Method Details

.component(name, options = {}) { ... } ⇒ Object

Defines a component

Parameters:

  • name (Symbol)

    name of the component

  • options (Hash) (defaults to: {})

    options hash see supported options

Options Hash (options):

  • (:single) (Symbol)

    The scope of the component. Can be either :single or :instance. When the scope is :single, only one instance of the component will be created and every call to Jerry#rig will return the same instance. When the scope is :instance, every call to Jerry#rig will return a new instance.

Yields:

  • Block used to instantiate the component. This block in only called when Jerry#rig is called.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jerry/config.rb', line 33

def component(name, options={}, &block)
  raise Jerry::ComponentError, "could not define component #{name}, block is missing" if block.nil?

  scope = options[:scope] || :single
  unless [:single, :instance].include? scope
    raise Jerry::ComponentError, "could not define component #{name}, scope #{scope} is unknown"
  end

  define_method name do
    case scope
      when :single
        cache[name] ||= instance_eval(&block)
      when :instance
        instance_eval(&block)
    end
  end

  components << name
end

.componentsArray<Symbol>

Returns list of the components defined by the config.

Returns:

  • (Array<Symbol>)

    list of the components defined by the config



19
20
21
# File 'lib/jerry/config.rb', line 19

def components
  @components ||= []
end

Instance Method Details

#cacheObject (protected)

Used internally to cache single instance components



81
82
83
# File 'lib/jerry/config.rb', line 81

def cache
  @cache ||= {}
end

#componentsArray<Symbol>

Returns list of components defined by the config.

Returns:

  • (Array<Symbol>)

    list of components defined by the config



55
56
57
# File 'lib/jerry/config.rb', line 55

def components
  self.class.components
end

#knows?(component) ⇒ Boolean (protected)

Check if given component exists

This should be used inside the block passed to Config::component

Returns:

  • (Boolean)


76
77
78
# File 'lib/jerry/config.rb', line 76

def knows?(component)
  @jerry.knows? component
end

#rig(component) ⇒ Object (protected)

Creates a component

This should be used inside the block passed to Config::component



69
70
71
# File 'lib/jerry/config.rb', line 69

def rig(component)
  @jerry.rig component
end