data-provider 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -2
- data/data-provider.gemspec +2 -5
- data/lib/data_provider/base.rb +149 -146
- data/lib/data_provider/container.rb +277 -0
- data/lib/data_provider.rb +1 -0
- data/spec/base_spec.rb +818 -0
- data/spec/container_spec.rb +792 -0
- data/spec/data_provider_spec.rb +85 -349
- metadata +4 -1
data/spec/data_provider_spec.rb
CHANGED
@@ -1,352 +1,5 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
describe DataProvider::Base do
|
4
|
-
# Example implementation of DataProvider::Base
|
5
|
-
class ProviderClass
|
6
|
-
include DataProvider::Base
|
7
|
-
|
8
|
-
provider :sum, :requires => [:array] do
|
9
|
-
sum = 0
|
10
|
-
|
11
|
-
given(:array).each do |number|
|
12
|
-
sum += number.to_i
|
13
|
-
end
|
14
|
-
|
15
|
-
sum
|
16
|
-
end
|
17
|
-
|
18
|
-
provider :static do
|
19
|
-
'StaticValue'
|
20
|
-
end
|
21
|
-
|
22
|
-
provider :billy do
|
23
|
-
take([:identification, :fullname])
|
24
|
-
end
|
25
|
-
|
26
|
-
provider [:identification, :firstname] do
|
27
|
-
'Billy'
|
28
|
-
end
|
29
|
-
|
30
|
-
provider [:identification, :lastname] do
|
31
|
-
'Bragg'
|
32
|
-
end
|
33
|
-
|
34
|
-
provider [:identification, :fullname] do
|
35
|
-
"#{scoped_take(:firstname)} #{scoped_take(:lastname)}"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
let(:provider){
|
40
|
-
ProviderClass.new(:data => {:array => [1,2,4]})
|
41
|
-
}
|
42
|
-
|
43
|
-
describe "#has_provider?" do
|
44
|
-
it 'provides the has_provider? instance method' do
|
45
|
-
expect(provider.has_provider?(:sum)).to be true
|
46
|
-
expect(provider.has_provider?(:static)).to be true
|
47
|
-
expect(provider.has_provider?(:modulus)).to be false
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'provides the has_provider? class method' do
|
51
|
-
expect(ProviderClass.respond_to?(:has_provider?)).to eq true
|
52
|
-
expect(ProviderClass.has_provider?(:sum)).to eq true
|
53
|
-
expect(ProviderClass.has_provider?(:divid)).to eq false
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe "#has_providers_with_scope?" do
|
58
|
-
let(:klass){
|
59
|
-
Class.new Object do
|
60
|
-
include DataProvider::Base
|
61
|
-
provider [:a, :b ,:c]
|
62
|
-
provider :unscoped
|
63
|
-
end
|
64
|
-
}
|
65
|
-
|
66
|
-
it "return true if there are providers defined with an array identifier that start with the given prefix" do
|
67
|
-
expect(klass.new.has_providers_with_scope?(:unscoped)).to eq false
|
68
|
-
# byebug
|
69
|
-
expect(klass.has_providers_with_scope?(:a)).to eq true
|
70
|
-
expect(klass.new.has_providers_with_scope?([:a, :b])).to eq true
|
71
|
-
# scope means prefix, identfier may not be exactly the given array
|
72
|
-
expect(klass.new.has_providers_with_scope?([:a, :b, :c])).to eq false
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "#take" do
|
77
|
-
it 'lets you take data from it' do
|
78
|
-
expect(provider.take(:sum)).to eq 7
|
79
|
-
expect(provider.take(:static)).to eq 'StaticValue'
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'raise a ProviderMissingException when attempting to take from unknown provider' do
|
83
|
-
expect{provider.take(:unknown)}.to raise_error(DataProvider::ProviderMissingException)
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
it 'works from within a provider block' do
|
88
|
-
expect(provider.take(:billy)).to eq 'Billy Bragg'
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe "#try_take" do
|
93
|
-
it "acts like #take when the specified provider is present" do
|
94
|
-
expect(provider.try_take(:sum)).to eq 7
|
95
|
-
end
|
96
|
-
|
97
|
-
it "returns nil when the specified provider is not found" do
|
98
|
-
expect(provider.try_take(:square_root)).to eq nil
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe "#scoped_take" do
|
103
|
-
it 'lets a provider call providers within its own scope' do
|
104
|
-
expect(provider.take([:identification, :fullname])).to eq 'Billy Bragg'
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "#give" do
|
109
|
-
it "lets you give data, creating a new data provider instance" do
|
110
|
-
updated_provider = provider.give :array => [1,80]
|
111
|
-
expect(provider.take(:sum)).to eq 7
|
112
|
-
expect(updated_provider.take(:sum)).to eq 81
|
113
|
-
end
|
114
|
-
|
115
|
-
it "allows for linked notation" do
|
116
|
-
expect(provider.give(:array => [7, -3]).take(:sum)).to eq 4
|
117
|
-
end
|
118
|
-
|
119
|
-
it "has an add_scope alias" do
|
120
|
-
expect(provider.add_scope(:array => [400, 20]).take(:sum)).to eq 420
|
121
|
-
end
|
122
|
-
|
123
|
-
it "has an add_data alias" do
|
124
|
-
expect(provider.add_data(:array => [400, 20]).take(:sum)).to eq 420
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
describe "#give!" do
|
129
|
-
it "lets you update the current provider with additional data" do
|
130
|
-
prov = ProviderClass.new(:data => {:array => [1,1,90]})
|
131
|
-
expect(prov.take(:sum)).to eq 92
|
132
|
-
prov.give!(:array => [3,90])
|
133
|
-
expect(prov.take(:sum)).to eq 93
|
134
|
-
end
|
135
|
-
|
136
|
-
it "allows for linked notation" do
|
137
|
-
expect(provider.give.give!(:array => [-1, -4]).take(:sum)).to eq -5
|
138
|
-
end
|
139
|
-
|
140
|
-
it "has an add_scope! alias" do
|
141
|
-
newprovider = provider.add_scope
|
142
|
-
newprovider.add_scope!(:array => [-1, -4])
|
143
|
-
expect(newprovider.given(:array)).to eq [-1,-4]
|
144
|
-
expect(newprovider.take(:sum)).to eq -5
|
145
|
-
end
|
146
|
-
|
147
|
-
it "has an add_data! alias" do
|
148
|
-
scoped_provider = provider.add_data(:array => []).add_data!(:array => [5, 5])
|
149
|
-
expect(scoped_provider.get_data(:array)).to eq [5,5]
|
150
|
-
expect(scoped_provider.take(:sum)).to eq 10
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
describe "#given" do
|
155
|
-
it "has a given method to get given data" do
|
156
|
-
expect(provider.given(:array)).to eq [1,2,4]
|
157
|
-
expect(provider.give(:array => 'array').given(:array)).to eq 'array'
|
158
|
-
end
|
159
|
-
|
160
|
-
it "has a get_data alias" do
|
161
|
-
expect(provider.get_data(:array)).to eq provider.given(:array)
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
describe "#provides" do
|
166
|
-
class SimpleProviders
|
167
|
-
include DataProvider::Base
|
168
|
-
provides({
|
169
|
-
:name => 'Paddy',
|
170
|
-
'instrument' => :bass,
|
171
|
-
})
|
172
|
-
end
|
173
|
-
|
174
|
-
it "lets you request all currently available simple providers when called without a parameter" do
|
175
|
-
expect(SimpleProviders.provides).to eq({
|
176
|
-
:name => 'Paddy',
|
177
|
-
'instrument' => :bass
|
178
|
-
})
|
179
|
-
end
|
180
|
-
|
181
|
-
it "lets you define simple providers" do
|
182
|
-
expect(SimpleProviders.new.take(:name)).to eq 'Paddy'
|
183
|
-
expect(SimpleProviders.new.take('instrument')).to eq :bass
|
184
|
-
end
|
185
|
-
|
186
|
-
it "works with has_provider?" do
|
187
|
-
expect(SimpleProviders.has_provider?(:name)).to eq true
|
188
|
-
expect(SimpleProviders.new.has_provider?('name')).to eq false
|
189
|
-
expect(SimpleProviders.has_provider?('instrument')).to eq true
|
190
|
-
expect(SimpleProviders.new.has_provider?(:instrument)).to eq false
|
191
|
-
end
|
192
|
-
|
193
|
-
it "lets you overwrite existing simple providers" do
|
194
|
-
SimpleProviders.provides({:name => 'Erik'})
|
195
|
-
expect(SimpleProviders.new.take(:name)).to eq 'Erik'
|
196
|
-
end
|
197
|
-
|
198
|
-
it "lets you write linked notation" do
|
199
|
-
expect(SimpleProviders.provides({:name => 'Lane'}).new.take(:name)).to eq 'Lane'
|
200
|
-
end
|
201
|
-
|
202
|
-
it "works with lambdas" do
|
203
|
-
expect(SimpleProviders.provides(:name => lambda{ 'Patrick' }).new.take(:name)).to eq 'Patrick'
|
204
|
-
end
|
205
|
-
|
206
|
-
it "works with Procs" do
|
207
|
-
expect(SimpleProviders.provides(:name => Proc.new{ 'St. Patrick' }).new.take(:name)).to eq 'St. Patrick'
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
describe "#add" do
|
212
|
-
module OddProviders
|
213
|
-
include DataProvider::Base
|
214
|
-
provides({1 => 'one'})
|
215
|
-
provider :three do 3 end
|
216
|
-
end
|
217
|
-
|
218
|
-
module OddOverwriteProviders
|
219
|
-
include DataProvider::Base
|
220
|
-
provides({1 => 'Uno', :five => 555})
|
221
|
-
provider :three do :tres end
|
222
|
-
end
|
223
|
-
|
224
|
-
class BasicProviders
|
225
|
-
include DataProvider::Base
|
226
|
-
provides({2 => '1'})
|
227
|
-
provider :four do '4' end
|
228
|
-
add OddProviders
|
229
|
-
end
|
230
|
-
|
231
|
-
it "lets you add providers from another module" do
|
232
|
-
expect(BasicProviders.has_provider?(1)).to eq true
|
233
|
-
expect(BasicProviders.has_provider?(2)).to eq true
|
234
|
-
expect(BasicProviders.has_provider?(:three)).to eq true
|
235
|
-
expect(BasicProviders.has_provider?(:four)).to eq true
|
236
|
-
# expect(BasicProviders.new.take(1)).to eq 'one'
|
237
|
-
expect(BasicProviders.new.take(:three)).to eq 3
|
238
|
-
end
|
239
|
-
|
240
|
-
it "lets you add providers from another module at runtime" do
|
241
|
-
expect(BasicProviders.has_provider?(:five)).to eq false
|
242
|
-
BasicProviders.add(OddOverwriteProviders)
|
243
|
-
expect(BasicProviders.has_provider?(:five)).to eq true
|
244
|
-
end
|
245
|
-
|
246
|
-
# for the following test the providers of OddOverwriteProviders
|
247
|
-
# have already been added (by the previous test)
|
248
|
-
it "lets overwrite providers" do
|
249
|
-
expect(BasicProviders.new.take(1)).to eq 'Uno'
|
250
|
-
expect(BasicProviders.new.take(:three)).to eq :tres
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
describe "#add_scoped" do
|
255
|
-
module ChildProviders
|
256
|
-
include DataProvider::Base
|
257
|
-
provider :name do "child" end
|
258
|
-
end
|
259
|
-
|
260
|
-
module GrandchildProviders
|
261
|
-
include DataProvider::Base
|
262
|
-
provider :name do "grandchild" end
|
263
|
-
provider [:age] do 1 end
|
264
|
-
provides({
|
265
|
-
:mommy => 'Wilma',
|
266
|
-
:daddy => 'Fret'
|
267
|
-
})
|
268
|
-
end
|
269
|
-
|
270
|
-
class ProviderKlass
|
271
|
-
include DataProvider::Base
|
272
|
-
provider :parent do 'parent' end
|
273
|
-
add_scoped ChildProviders, :scope => :child
|
274
|
-
add_scoped GrandchildProviders, :scope => [:child, :child]
|
275
|
-
end
|
276
|
-
|
277
|
-
it 'let you array-prefix the providers of an included module' do
|
278
|
-
expect(ProviderKlass.has_provider?(:parent)).to eq true
|
279
|
-
expect(ProviderKlass.has_provider?(:name)).to eq false
|
280
|
-
expect(ProviderKlass.has_provider?([:child, :name])).to eq true
|
281
|
-
expect(ProviderKlass.has_provider?([:child, :age])).to eq false
|
282
|
-
expect(ProviderKlass.has_provider?([:child, :child, :name])).to eq true
|
283
|
-
expect(ProviderKlass.has_provider?([:child, :child, :age])).to eq true
|
284
|
-
expect(ProviderKlass.has_provider?([:child, :child, :mommy])).to eq true
|
285
|
-
expect(ProviderKlass.has_provider?([:child, :child, :daddy])).to eq true
|
286
|
-
expect(ProviderKlass.new.take([:child, :name])).to eq 'child'
|
287
|
-
expect(ProviderKlass.new.take([:child, :child, :name])).to eq 'grandchild'
|
288
|
-
expect(ProviderKlass.new.take([:child, :child, :age])).to eq 1
|
289
|
-
expect(ProviderKlass.new.take([:child, :child, :mommy])).to eq 'Wilma'
|
290
|
-
expect(ProviderKlass.new.take([:child, :child, :daddy])).to eq 'Fret'
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
describe "provider_missing" do
|
295
|
-
it "lets you define a default fallback provider" do
|
296
|
-
klass = Class.new Object do
|
297
|
-
include DataProvider::Base
|
298
|
-
provider_missing do
|
299
|
-
"This provider don't exist!"
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
|
-
expect(klass.has_provider?(:message)).to eq false
|
304
|
-
expect(klass.new.take(:message)).to eq "This provider don't exist!"
|
305
|
-
end
|
306
|
-
|
307
|
-
it "provides the missing provider id through the private missing_provider method" do
|
308
|
-
klass = Class.new Object do
|
309
|
-
include DataProvider::Base
|
310
|
-
provider_missing do
|
311
|
-
"Missing #{missing_provider}"
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
expect(klass.new.take(:something)).to eq 'Missing something'
|
316
|
-
expect{klass.new.missing_provider}.to raise_error(NoMethodError)
|
317
|
-
end
|
318
|
-
|
319
|
-
it 'calls the fallback provider when using try_take with an unknown provider' do
|
320
|
-
klass = Class.new Object do
|
321
|
-
include DataProvider::Base
|
322
|
-
provider_missing do
|
323
|
-
"fallback_#{missing_provider}"
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
expect(klass.new.try_take(:cool)).to eq 'fallback_cool'
|
328
|
-
end
|
329
|
-
end
|
330
|
-
|
331
|
-
describe "fallback_provider?" do
|
332
|
-
it "lets you know if a fallback provider has been registered" do
|
333
|
-
klass = Class.new Object do
|
334
|
-
include DataProvider::Base
|
335
|
-
end
|
336
|
-
|
337
|
-
expect(klass.fallback_provider?).to eq false
|
338
|
-
expect(klass.new.fallback_provider?).to eq false
|
339
|
-
|
340
|
-
klass.provider_missing do
|
341
|
-
"New fallback!"
|
342
|
-
end
|
343
|
-
|
344
|
-
expect(klass.fallback_provider?).to eq true
|
345
|
-
expect(klass.new.fallback_provider?).to eq true
|
346
|
-
end
|
347
|
-
end
|
348
|
-
end
|
349
|
-
|
350
3
|
describe "Adding additional providers" do
|
351
4
|
module AdditionalProvider
|
352
5
|
include DataProvider::Base
|
@@ -413,8 +66,91 @@ describe "Array identifiers" do
|
|
413
66
|
|
414
67
|
it "lets you overwrite existing providers with Array-based identifiers" do
|
415
68
|
expect(provider.take([:some, 'Stuff'])).to eq 'SomeStuff'
|
416
|
-
|
417
69
|
provider.class.add(ArrayProviderModule)
|
418
|
-
|
70
|
+
# class got updated
|
71
|
+
expect(provider.class.new.take([:some, 'Stuff'])).to eq 'OtherStuff'
|
72
|
+
# already instatiated instances didn't get this memo
|
73
|
+
expect(provider.take([:some, 'Stuff'])).to eq 'SomeStuff'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "mixing regular ruby methods and data providers" do
|
78
|
+
module MixedModule
|
79
|
+
include DataProvider::Base
|
80
|
+
|
81
|
+
def func2
|
82
|
+
"More Normal Stuff"
|
83
|
+
end
|
84
|
+
|
85
|
+
provider :module_provider do
|
86
|
+
"#{take(:pro_vider)}, #{func}, #{func2}"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
class MixedClass
|
91
|
+
include DataProvider::Base
|
92
|
+
|
93
|
+
provider :pro_vider do
|
94
|
+
func
|
95
|
+
end
|
96
|
+
|
97
|
+
def func
|
98
|
+
"Something Normal Here"
|
99
|
+
end
|
100
|
+
|
101
|
+
add MixedModule
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "custom class methods" do
|
105
|
+
it "does not let providers access regular methods" do
|
106
|
+
obj = MixedClass.new
|
107
|
+
expect(obj.func).to eq 'Something Normal Here'
|
108
|
+
expect(obj.take(:pro_vider)).to eq 'Something Normal Here'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "does not let module providers access methods from the base class or vice-versa" do
|
112
|
+
obj = MixedClass.new
|
113
|
+
expect(obj.func2).to eq 'More Normal Stuff'
|
114
|
+
expect(obj.take(:module_provider)).to eq 'Something Normal Here, Something Normal Here, More Normal Stuff'
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "Exceptions" do
|
120
|
+
class ExceptionProvider
|
121
|
+
include DataProvider::Base
|
122
|
+
|
123
|
+
provider :runtime do
|
124
|
+
raise 'Whoops'
|
125
|
+
end
|
126
|
+
|
127
|
+
provider :missing do
|
128
|
+
take(:foo)
|
129
|
+
end
|
130
|
+
|
131
|
+
provider :nomethod do
|
132
|
+
bar
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
let(:provider){
|
137
|
+
ExceptionProvider.new
|
138
|
+
}
|
139
|
+
|
140
|
+
it "can go wrong, like everything else" do
|
141
|
+
expect { provider.take(:runtime)}.to raise_error(RuntimeError)
|
142
|
+
expect { provider.take(:runtime)}.to raise_error('Whoops')
|
143
|
+
end
|
144
|
+
|
145
|
+
it "can take from missing providers" do
|
146
|
+
expect { provider.take(:missing) }.to raise_error(DataProvider::ProviderMissingException)
|
147
|
+
expect { provider.take(:missing) }.to raise_error { |error|
|
148
|
+
expect( error.message ).to eq 'Tried to take data from missing provider: :foo'
|
149
|
+
expect( error.provider_id ).to eq :foo
|
150
|
+
}
|
151
|
+
end
|
152
|
+
|
153
|
+
it "can call missing methods" do
|
154
|
+
expect { provider.take(:nomethod) }.to raise_error(NameError)
|
419
155
|
end
|
420
156
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data-provider
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark van de Korput
|
@@ -39,7 +39,10 @@ files:
|
|
39
39
|
- data-provider.gemspec
|
40
40
|
- lib/data_provider.rb
|
41
41
|
- lib/data_provider/base.rb
|
42
|
+
- lib/data_provider/container.rb
|
42
43
|
- lib/data_provider/provider.rb
|
44
|
+
- spec/base_spec.rb
|
45
|
+
- spec/container_spec.rb
|
43
46
|
- spec/data_provider_spec.rb
|
44
47
|
- spec/spec_helper.rb
|
45
48
|
homepage: https://github.com/markkorput/data-provider
|