Class: Strudel
- Inherits:
-
Object
- Object
- Strudel
- Defined in:
- lib/strudel.rb,
lib/strudel/version.rb
Overview
Strudel
A tiny dependency injection container
Constant Summary collapse
- VERSION =
The current Strudel gem version
'1.0.2'
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ value?
Get a service by key.
-
#[]=(key, service) ⇒ self
Set a service by key and value.
-
#each {|key| ... } ⇒ Enumerable?
Iterates over the service keys.
-
#extend(key, extender = nil) {|old_value, self| ... } ⇒ self
Extends an existing service and overrides it.
-
#factory(key, factory = nil) {|self| ... } ⇒ self
Set a factory service by key and value.
-
#include?(key) ⇒ bool
Checks if a service for
key
exists. -
#initialize {|self| ... } ⇒ Strudel
constructor
Creates a new Strudel container.
-
#protect(key, service = nil) {|self| ... } ⇒ self
Set a protected service by name and value.
-
#set(key, service = nil) {|self| ... } ⇒ self
Set a service by key and value.
Constructor Details
#initialize {|self| ... } ⇒ Strudel
Creates a new Strudel container
12 13 14 15 16 17 |
# File 'lib/strudel.rb', line 12 def initialize @services = {} @procs = {} @factories = {} yield self if block_given? end |
Class Method Details
.version ⇒ Object
7 8 9 |
# File 'lib/strudel/version.rb', line 7 def self.version Gem::Version.new(VERSION) end |
Instance Method Details
#[](key) ⇒ value?
Get a service by key
If the service is not set, returns nil
.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/strudel.rb', line 25 def [](key) return @services[key].call(self) if @factories[key] if @procs[key] @services[key] = @services[key].call(self) @procs.delete(key) end @services[key] end |
#[]=(key, service) ⇒ self
Set a service by key and value
If service
is a Proc, its return value will be treated as a
singleton service meaning it will be initialized the first time it is
requested and its valud will be cached for subsequent requests.
Use the set
method to allow using a block instead of a Proc argument.
If service
is not a function, its value will be stored directly.
49 50 51 |
# File 'lib/strudel.rb', line 49 def []=(key, service) set(key, service) end |
#each {|key| ... } ⇒ Enumerable?
Iterates over the service keys
If a block is given, the block is called with each of the service keys
If a block is not given, returns an Enumerable
of the keys.
The key order is undefined.
141 142 143 144 145 |
# File 'lib/strudel.rb', line 141 def each return @services.each_key unless block_given? @services.each_key { |key| yield key } end |
#extend(key, extender = nil) {|old_value, self| ... } ⇒ self
Extends an existing service and overrides it.
The extender
block will be called with 2 arguments: old_value
and
self
. If there is no existing key
service, old_value
will be nil. It
should return the new value for the service that will override the existing
one.
If extend
is called for a service that was created with set
, the
resulting service will be a singleton.
If extend
is called for a service that was created with factory
, the
resulting service will be a factory.
If extend
is called for a service that was created with protect
, the
resulting service will also be protected.
If extender
is not a function, this method will override any existing
service like set
.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/strudel.rb', line 121 def extend(key, extender = nil, &block) extender ||= block return set(key, extender) unless extender.is_a?(Proc) && @services.key?(key) extended = @services[key] call = @factories[key] || @procs[key] send(@factories[key] ? :factory : :set, key) do extender.call(self, call ? extended.call(self) : extended) end end |
#factory(key, factory = nil) {|self| ... } ⇒ self
Set a factory service by key and value
If factory
is a function, it will be called every time the service is
requested. So if it returns an object, it will create a new object for
every request.
If factory
is not a function, this method acts like set
.
78 79 80 |
# File 'lib/strudel.rb', line 78 def factory(key, factory = nil, &block) create(key, factory || block, @factories) end |
#include?(key) ⇒ bool
Checks if a service for key
exists.
150 151 152 |
# File 'lib/strudel.rb', line 150 def include?(key) @services.key?(key) end |
#protect(key, service = nil) {|self| ... } ⇒ self
Set a protected service by name and value
If service
is a function, the function itself will be registered as a
service. So when it is requested with get
, the function will be returned
instead of the function's return value.
If service
is not a function, this method acts like set
.
94 95 96 |
# File 'lib/strudel.rb', line 94 def protect(key, service = nil, &block) create(key, service || block) end |
#set(key, service = nil) {|self| ... } ⇒ self
Set a service by key and value
Same as []=
except allows passing a block instead of a Proc argument.
62 63 64 |
# File 'lib/strudel.rb', line 62 def set(key, service = nil, &block) create(key, service || block, @procs) end |