yadic 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -0
- data/lib/yadic.rb +53 -0
- metadata +68 -0
data/README.rdoc
ADDED
data/lib/yadic.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module Yadic
|
2
|
+
class Container
|
3
|
+
def add name, klass=nil, &initializer
|
4
|
+
add_initializer(name, klass, initializer)
|
5
|
+
end
|
6
|
+
|
7
|
+
def decorate name, klass=nil, &initializer
|
8
|
+
add_initializer(name, klass, initializer) do |initializer|
|
9
|
+
existing_init = find_initializer(name)
|
10
|
+
lambda { initializer.call.decorating(existing_init.call) }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def [] name
|
15
|
+
find_initializer(name).call
|
16
|
+
end
|
17
|
+
|
18
|
+
class ObjectNotFoundError < StandardError
|
19
|
+
def initialize(name) @name=name end
|
20
|
+
def message() "No object named #{@name}." end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def initializers() @initializers ||= {} end
|
25
|
+
|
26
|
+
def find_initializer name
|
27
|
+
initializers[name] or raise ObjectNotFoundError.new(name)
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_initializer name, klass, initializer, &wrap
|
31
|
+
initializer = initializer_from(klass, initializer)
|
32
|
+
wrap and initializer = wrap.call(initializer)
|
33
|
+
initializers[name] = memoize initializer
|
34
|
+
end
|
35
|
+
|
36
|
+
def memoize initializer
|
37
|
+
value = nil
|
38
|
+
lambda { value ||= initializer.call }
|
39
|
+
end
|
40
|
+
|
41
|
+
def initializer_from(klass, initializer)
|
42
|
+
initializer ? lambda { initializer.call(self) } : lambda { klass.new }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module Decorator
|
47
|
+
attr_reader :wrapped
|
48
|
+
def decorating(wrapped)
|
49
|
+
@wrapped = wrapped
|
50
|
+
self
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yadic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ben Butler-Cole
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-08 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description:
|
23
|
+
email: ben@bridesmere.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README.rdoc
|
32
|
+
- lib/yadic.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/benbc/yadic.rb
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: A lightweight dependency injection container.
|
67
|
+
test_files: []
|
68
|
+
|