Class: Jerry::Config Abstract

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

Overview

This class is abstract.

Subclass this class in order to create a configuration

A configuration specifies how to wire parts of an application

Examples:

Basic usage

class Door; end
class Window; end

class House
  def initialize(door, window)
    # ...
  end
end

class MyConfig < Jerry::Config
  bind House, [Door, Window]
  bind Door
  bind Window
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jerry=(value) ⇒ Object (writeonly)

The jerry instance this config is part of



67
68
69
# File 'lib/jerry/config.rb', line 67

def jerry=(value)
  @jerry = value
end

Class Method Details

.bind(klass, ctor_args = []) ⇒ Class

Specify how to wire the dependencies of a given class

Parameters:

  • klass (Class)

    The class to wire the dependencies for

  • ctor_args (Array<Class, Symbol, Proc>) (defaults to: [])

    specifies the arguments to be given to the constructor

Returns:

  • (Class)

    the class that will be instanciated



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

def bind(klass, ctor_args = [])
  named_bind klass, klass, ctor_args
end

.named_bind(name, klass, ctor_args = []) ⇒ Symbol

Specify how to wire the dependencies of a given class giving it a name

Parameters:

  • name (Symbol)

    The name used to identify this way of building the given class

  • klass (Class)

    The class to wire the dependencies for

  • ctor_args (Array<Class, Symbol, Proc>) (defaults to: [])

    specifies the arguments to be given to the constructor

Returns:

  • (Symbol)

    the name of the class that will be instanciated



43
44
45
46
# File 'lib/jerry/config.rb', line 43

def named_bind(name, klass, ctor_args = [])
  providers[name] = ClassProvider.new klass, ctor_args
  name
end

.providersObject



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

def providers
  @providers ||= {}
end

.singleton(key) ⇒ Object

Specifies that a class should only be instanciated once

Parameters:

  • key (Class, Symbol)

    the class or the name of the class that should only be instanciated once



52
53
54
55
56
57
58
59
# File 'lib/jerry/config.rb', line 52

def singleton(key)
  return unless providers.key? key

  provider = providers[key]

  instance = nil
  providers[key] = ->(*args) { instance ||= provider.call(*args) }
end

Instance Method Details

#[](key) ⇒ Object

Returns an instance of an object wired by the config.

Returns:

  • an instance of an object wired by the config



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jerry/config.rb', line 70

def [](key)
  provider = self.class.providers[key]

  if provider
    provider.call @jerry, self
  else
    raise InstantiationError,
          "Failed to instanciate #{key}. Can't find provider for it"
  end
rescue RuntimeError
  raise InstantiationError, "Provider for #{key} raised an error"
end

#knows?(key) ⇒ Boolean

Returns true if this config can provide the given key, false otherwise.

Returns:

  • (Boolean)

    true if this config can provide the given key, false otherwise



84
85
86
# File 'lib/jerry/config.rb', line 84

def knows?(key)
  self.class.providers.key? key
end