Class: Jerry::Config Abstract
- Inherits:
-
Object
- Object
- Jerry::Config
- Defined in:
- lib/jerry/config.rb
Overview
Subclass this class in order to create a configuration
A configuration specifies how to wire parts of an application
Instance Attribute Summary collapse
-
#jerry ⇒ Object
writeonly
The jerry instance this config is part of.
Class Method Summary collapse
-
.bind(klass, ctor_args = []) ⇒ Class
Specify how to wire the dependencies of a given class.
-
.named_bind(name, klass, ctor_args = []) ⇒ Symbol
Specify how to wire the dependencies of a given class giving it a name.
- .providers ⇒ Object
-
.singleton(key) ⇒ Object
Specifies that a class should only be instanciated once.
Instance Method Summary collapse
-
#[](key) ⇒ Object
An instance of an object wired by the config.
-
#knows?(key) ⇒ Boolean
True if this config can provide the given key, false otherwise.
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
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
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 |
.providers ⇒ Object
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
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.
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.
84 85 86 |
# File 'lib/jerry/config.rb', line 84 def knows?(key) self.class.providers.key? key end |