pimple 0.0.1.alpha → 0.0.2.beta
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pimple.rb +62 -3
- data/spec/pimple_spec.rb +81 -16
- data/spec/spec_helper.rb +7 -0
- metadata +2 -2
data/lib/pimple.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
1
|
class Pimple < Hash
|
2
2
|
|
3
|
-
VERSION = '0.0.
|
3
|
+
VERSION = '0.0.2.beta'
|
4
4
|
|
5
|
+
# Initialize a container with some default parameters
|
5
6
|
def initialize(parameters={})
|
6
7
|
self.replace(parameters)
|
7
8
|
end
|
8
9
|
|
10
|
+
# Get an object.
|
11
|
+
# If the fetched object is a lambda, it returns its value.
|
12
|
+
#
|
13
|
+
# @params [Symbol, String] key Identifier of the searched elements in the container.
|
14
|
+
#
|
15
|
+
# @return [Object]
|
9
16
|
def [](key)
|
10
17
|
obj = self.fetch key
|
11
18
|
obj.kind_of?(Proc) ? obj.call(self) : obj
|
@@ -13,11 +20,29 @@ class Pimple < Hash
|
|
13
20
|
raise KeyError, "Identifier \"#{key}\" is not defined."
|
14
21
|
end
|
15
22
|
|
23
|
+
# Create a protected parameter.
|
24
|
+
#
|
25
|
+
# @return [Proc]
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# container[:rand] = container.protect { rand(100) }
|
29
|
+
# container[:rand] # => 34
|
30
|
+
# container[:rand] # => 67
|
16
31
|
def protect
|
17
32
|
raise ArgumentError, "Missing block for protected function" unless block_given?
|
18
33
|
Proc.new { yield }
|
19
34
|
end
|
20
35
|
|
36
|
+
# Create a shared service definition
|
37
|
+
# This method require to called with a block that contains service definition
|
38
|
+
#
|
39
|
+
# @return [Proc]
|
40
|
+
#
|
41
|
+
# @example
|
42
|
+
# container[:session] = container.share { |c| Session.new }
|
43
|
+
# container[:session] # => #<Session:0x007>
|
44
|
+
# # Returns the same object later...
|
45
|
+
# container[:session] # => #<Session:0x007>
|
21
46
|
def share
|
22
47
|
raise ArgumentError, "Missing block for shared service" unless block_given?
|
23
48
|
lambda do |c|
|
@@ -26,8 +51,42 @@ class Pimple < Hash
|
|
26
51
|
end
|
27
52
|
end
|
28
53
|
|
29
|
-
|
30
|
-
|
54
|
+
# Get the raw data from the container
|
55
|
+
# If you call this method by specifying a service as the key, it will return the Proc.
|
56
|
+
#
|
57
|
+
# @return [Object, Proc]
|
58
|
+
#
|
59
|
+
# @example
|
60
|
+
# container[:session] = lambda { |c| Service.new }
|
61
|
+
# container.raw(:service) # => #<Proc:0x007>
|
62
|
+
def raw(key)
|
63
|
+
self.fetch key
|
64
|
+
rescue
|
65
|
+
raise KeyError, "Identifier \"#{key}\" is not defined."
|
66
|
+
end
|
67
|
+
|
68
|
+
# Extend a service definition
|
69
|
+
#
|
70
|
+
# @param [Symbol, String] key Identifier of the service definition to extend
|
71
|
+
#
|
72
|
+
# @return [Proc]
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
# container[:service] = container.share { |c| Service.new }
|
76
|
+
# container[:service] = container.extend(:service) { |s, c|
|
77
|
+
# c.configure (:foo, 'bar')
|
78
|
+
# return c # need to return the service object.
|
79
|
+
# }
|
80
|
+
def extends(key)
|
81
|
+
factory = self.raw(key)
|
82
|
+
|
83
|
+
unless factory.kind_of?(Proc)
|
84
|
+
raise ArgumentError, "Identifier #{key} does not contain an object definition."
|
85
|
+
end
|
86
|
+
|
87
|
+
lambda do |c|
|
88
|
+
yield(factory.call(c), c)
|
89
|
+
end
|
31
90
|
end
|
32
91
|
|
33
92
|
end
|
data/spec/pimple_spec.rb
CHANGED
@@ -17,16 +17,21 @@ describe Pimple do
|
|
17
17
|
|
18
18
|
describe '.[]' do
|
19
19
|
let(:container) { Pimple.new }
|
20
|
-
before { class Facebook; end; } # Simulate Facebook Api Client class
|
21
20
|
|
22
21
|
it 'should inject service' do
|
23
|
-
container[:
|
24
|
-
container[:
|
22
|
+
container[:service] = lambda { |c| Service.new }
|
23
|
+
container[:service].class.should equal(Service)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should inject configuring service from container parameters' do
|
27
|
+
container[:foo] = 'bar'
|
28
|
+
container[:service] = lambda { |c| Service.new(c[:foo]) }
|
29
|
+
container[:service].param.should == 'bar'
|
25
30
|
end
|
26
31
|
|
27
32
|
it 'should return a new instance each time the [](key) method is invoked' do
|
28
|
-
container[:
|
29
|
-
container[:
|
33
|
+
container[:service] = lambda { |c| Service.new }
|
34
|
+
container[:service].should_not equal(container[:service])
|
30
35
|
end
|
31
36
|
|
32
37
|
it 'should raise KeyError if service not found' do
|
@@ -39,13 +44,13 @@ describe Pimple do
|
|
39
44
|
let(:container) { Pimple.new }
|
40
45
|
|
41
46
|
it 'should define anonymous function as parameter' do
|
42
|
-
container[:
|
43
|
-
container[:
|
47
|
+
container[:protected] = container.protect { rand(1000) }
|
48
|
+
container[:protected].should_not equal(container[:protected])
|
44
49
|
end
|
45
50
|
|
46
51
|
it 'should define anonymous function as parameter by Proc.new way' do
|
47
|
-
container[:
|
48
|
-
container[:
|
52
|
+
container[:protected] = Proc.new { rand(1000) }
|
53
|
+
container[:protected].should_not equal(container[:protected])
|
49
54
|
end
|
50
55
|
|
51
56
|
it 'should raise ArgumentError when block is missing' do
|
@@ -56,21 +61,81 @@ describe Pimple do
|
|
56
61
|
|
57
62
|
describe '.share' do
|
58
63
|
let(:container) { Pimple.new }
|
59
|
-
before { class Facebook; end; } # Simulate Facebook Api Client class
|
60
64
|
|
61
|
-
it 'should define shared
|
62
|
-
container[:
|
63
|
-
container[:
|
65
|
+
it 'should define shared service' do
|
66
|
+
container[:service] = container.share { |c| Service.new }
|
67
|
+
container[:service].class.should equal(Service)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should inject configuring shared service from container parameters' do
|
71
|
+
container[:foo] = 'bar'
|
72
|
+
container[:service] = container.share { |c| Service.new(c[:foo]) }
|
73
|
+
container[:service].param.should == 'bar'
|
64
74
|
end
|
65
75
|
|
66
76
|
it 'should get the same instance each time [](key) method is invoked' do
|
67
|
-
container[:
|
68
|
-
container[:
|
77
|
+
container[:service] = container.share { |c| Service.new }
|
78
|
+
container[:service].should equal(container[:service])
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '.raw' do
|
84
|
+
let(:container) { Pimple.new }
|
85
|
+
|
86
|
+
it 'should return the anonymous function for the defined serivce' do
|
87
|
+
container[:service] = lambda { |c| Service.new }
|
88
|
+
container.raw(:service).kind_of?(Proc).should be_true
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should raise KeyError exception if service not defined' do
|
92
|
+
lambda { container.raw :notfound }.should raise_error(KeyError)
|
69
93
|
end
|
70
94
|
|
71
95
|
end
|
72
96
|
|
73
|
-
describe '.
|
97
|
+
describe '.extends' do
|
98
|
+
let(:container) { Pimple.new }
|
99
|
+
|
100
|
+
it 'should extend a service definition' do
|
101
|
+
container[:service] = lambda { |c| Service.new }
|
102
|
+
container[:service] = container.extends(:service) do |service, c|
|
103
|
+
service.param = 'foo'
|
104
|
+
service
|
105
|
+
end
|
106
|
+
container[:service].param.should == 'foo'
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should extend a shared service definition' do
|
110
|
+
container[:service] = container.share { |c| Service.new }
|
111
|
+
container[:service] = container.extends(:service) do |service, c|
|
112
|
+
service.param = 'foo'
|
113
|
+
service
|
114
|
+
end
|
115
|
+
container[:service].should equal(container[:service])
|
116
|
+
container[:service].param.should == 'foo'
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'should extend protected parameter' do
|
120
|
+
container[:protected] = container.protect { rand(100) }
|
121
|
+
container[:protected] = container.extends(:protected) do |p, c|
|
122
|
+
p + 200
|
123
|
+
end
|
124
|
+
container[:protected].should_not == container[:protected]
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'should not extend parameter' do
|
128
|
+
container[:foo] = 'bar'
|
129
|
+
lambda {
|
130
|
+
container.extends(:foo) { |p, c| 'foo' }
|
131
|
+
}.should raise_error(ArgumentError)
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should raise KeyError if service not found' do
|
135
|
+
lambda {
|
136
|
+
container.extends(:notfound) { |p, c| }
|
137
|
+
}.should raise_error(KeyError)
|
138
|
+
end
|
74
139
|
|
75
140
|
end
|
76
141
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pimple
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2.beta
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|