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.0'
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
10 11 12 13 14 15 |
# File 'lib/strudel.rb', line 10 def initialize @services = {} @procs = {} @factories = {} yield self if block_given? end |
Instance Method Details
#[](key) ⇒ value?
Get a service by key
If the service is not set, returns nil
.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/strudel.rb', line 23 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.
47 48 49 |
# File 'lib/strudel.rb', line 47 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.
139 140 141 142 143 |
# File 'lib/strudel.rb', line 139 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
.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/strudel.rb', line 119 def extend(key, extender = nil) extender ||= Proc.new 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
.
76 77 78 |
# File 'lib/strudel.rb', line 76 def factory(key, factory = nil) create(key, factory || Proc.new, @factories) end |
#include?(key) ⇒ bool
Checks if a service for key
exists.
148 149 150 |
# File 'lib/strudel.rb', line 148 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
.
92 93 94 |
# File 'lib/strudel.rb', line 92 def protect(key, service = nil) create(key, service || Proc.new) 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.
60 61 62 |
# File 'lib/strudel.rb', line 60 def set(key, service = nil) create(key, service || Proc.new, @procs) end |