function_chain 0.0.2 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fdf003460177adf194c6282fc22e83659bb93c5a
4
- data.tar.gz: fcf5395ab9889a3ede9cfbbea020de00644ce4d8
3
+ metadata.gz: dc0b4ab079683f23b77447d2fb6e092f9795fe12
4
+ data.tar.gz: 04ff13d4a5d505b157cfce3e1db80074ee8a07b9
5
5
  SHA512:
6
- metadata.gz: 8b3821ebdc3b4234bbbcaae2bc502c832fa63f6a4e2df14f08b8baa18ea334add047f2e48724901c00609d63a81e193b91001d6e37fef9b9c253cf02fbf5d37a
7
- data.tar.gz: 2f9afcd3051e99adfd032558cea80309e09b2c09954f4a6f652fbf1eb721bcb3cf74b9518e37c12979995842b00359c01768c41fde149cc3402aeb2a7d558ee3
6
+ metadata.gz: 22bd3c36c5c1ca3e3428d91cbcd9f062684cf78d62c7af40545276e662aed4d8d0862cc1fdeb9556f2ad33eab603fa279c9ccc7cd50e7bf0ebd84dbefe6e0653
7
+ data.tar.gz: a9941e868b78c8de12146dcf099c3891c4683a7d7a74feacf76663e952467a04f5c9003b8eccc0f3bef2b7d709236c6b7ca47d679d98b5b68614f81672c570cc
data/README.md CHANGED
@@ -70,33 +70,87 @@ chain.call # => LOUIS
70
70
  PullChain.new(account, "user/name/upcase").call
71
71
  ```
72
72
 
73
- 2. **Use << operator**
73
+ 2. **Use <<**
74
74
  ```ruby
75
75
  chain = PullChain.new(account)
76
76
  chain << :user << :name << :upcase
77
77
  chain.call
78
78
  ```
79
79
 
80
- 3. **Use add method**
80
+ 3. **Use *add***
81
81
  ```ruby
82
+ chain = PullChain.new(account)
82
83
  chain.add(:user).add(:name).add(:upcase).call
83
84
  ```
84
85
 
85
- 4. **Use add_all method**
86
+ 4. **Use *add_all***
86
87
  ```ruby
88
+ chain = PullChain.new(account)
87
89
  chain.add_all(:user, :name, :upcase).call
88
90
  ```
89
91
 
92
+ 5. **Use *Proc***
93
+ ```ruby
94
+ chain = PullChain.new(account)
95
+ chain << Proc.new { user } << Proc.new { name } << Proc.new { upcase }
96
+ chain.call
97
+ ```
98
+ If you use *lambda* then can't omit block parameter.
99
+ block parameter is previous chain result.
100
+ ```ruby
101
+ chain = PullChain.new(account)
102
+ chain << lambda { |account| account.user } <<
103
+ lambda { |user| user.name } <<
104
+ lambda { |name| name.upcase }
105
+ chain.call
106
+ ```
107
+ *lambda* evaluate by previous chain result, so can call results method direct.
108
+ ```ruby
109
+ chain = PullChain.new(account)
110
+ chain << lambda { |_| user } << lambda { |_| name } << lambda { |_| upcase }
111
+ chain.call
112
+ ```
113
+ if want to omit block parameter, recommend *Proc* use.
114
+
115
+ 6. **Use *add* with block**
116
+ ```ruby
117
+ PullChain.new(account).add { user }.add { name }.add { upcase }.call
118
+ ```
119
+
90
120
  #### Can exist nil value on the way, like a following case
91
121
  ```ruby
92
122
  user.name = nil
93
123
  chain.call # => nil
94
124
  ```
95
125
 
96
- #### Insert, Delete, Clear
97
- insert, insert_all method is insert_all method to chain.
98
- delete_at method is delete method from chain.
99
- clear method is delete all method from chain.
126
+ #### Insert
127
+ *insert*, *insert_all* is insert method to chain.
128
+ ```ruby
129
+ chain = PullChain.new(account, :user, :upcase)
130
+ chain.insert(1, :name).call
131
+
132
+ chain = PullChain.new(account, :user)
133
+ chain.insert_all(1, :name, :upcase).call
134
+ ```
135
+ *insert* with block
136
+ ```ruby
137
+ chain = PullChain.new(account, :user, :upcase)
138
+ chain.insert(1) { name }.call
139
+ ```
140
+ #### Delete
141
+ *delete_at* is delete method from chain.
142
+ ```ruby
143
+ chain = PullChain.new(account, :user, :name, :upcase)
144
+ chain.delete_at(2)
145
+ chain.call # => Louis
146
+ ```
147
+ #### Clear
148
+ *clear* is delete all method from chain.
149
+ ```ruby
150
+ chain = PullChain.new(account, :user, :name, :upcase)
151
+ chain.clear
152
+ chain.call # => #<struct Account user=#<struct User name="Louis">>
153
+ ```
100
154
 
101
155
  ### Require arguments on method
102
156
  Following example's method is require two arguments.
@@ -109,18 +163,30 @@ class Foo
109
163
  end
110
164
  ```
111
165
  ###### Solution
112
- 1. **Array, format is [Symbol, [\*Args]]**
166
+ 1. **_Array_, format is [_Symbol_, [\*args]]**
113
167
  ```ruby
114
168
  chain = PullChain.new(Foo.new) << [:say, ["Andres", "Hello"]]
115
169
  chain.call # => Andres said 'Hello'
116
170
  ```
117
171
 
118
- 2. **String**
172
+ 2. ***String***
119
173
  ```ruby
120
- chain = PullChain.new(foo) << "say('John', 'Goodbye')"
174
+ chain = PullChain.new(Foo.new) << "say('John', 'Goodbye')"
121
175
  chain.call # => John said 'Goodbye'
122
176
  ```
123
177
 
178
+ 3. ***Proc***
179
+ ```ruby
180
+ chain = PullChain.new(Foo.new) << Proc.new { say('Julian', 'Nice to meet you') }
181
+ chain.call # => Julian said 'Nice to meet you'
182
+ ```
183
+
184
+ 4. **_add_ with block**
185
+ ```ruby
186
+ chain = PullChain.new(Foo.new).add { say('Narciso', 'How do you do?') }
187
+ chain.call # => Narciso said 'How do you do?'
188
+ ```
189
+
124
190
  ### Require block on method
125
191
  Following example's method is require Block.
126
192
  What should do in this case?
@@ -130,20 +196,34 @@ What should do in this case?
130
196
 
131
197
  ###### Solution
132
198
 
133
- 1. **Array, format is [Symbol, [\*Args, Proc]]**
199
+ 1. **_Array_, format is [_Symbol_, [\*args, _Proc_]]**
134
200
  ```ruby
135
201
  chain = PullChain.new([1,2,3,4,5])
136
202
  chain << [:inject, [3, lambda { |sum, n| sum + n }]]
137
203
  chain.call # => 18
138
204
  ```
139
205
 
140
- 2. **String**
206
+ 2. **_String_**
141
207
  ```ruby
142
208
  chain = PullChain.new([1,2,3,4,5])
143
209
  chain << "inject(3) { |sum, n| sum + n }"
144
210
  chain.call # => 18
145
211
  ```
146
212
 
213
+ 3. **_Proc_**
214
+ ```ruby
215
+ chain = PullChain.new([1,2,3,4,5])
216
+ chain << Proc.new { inject(3) { |sum, n| sum + n } }
217
+ chain.call # => 18
218
+ ```
219
+
220
+ 4. **_add_ with block**
221
+ ```ruby
222
+ chain = PullChain.new([1,2,3,4,5])
223
+ chain.add { inject(3) { |sum, n| sum + n } }
224
+ chain.call # => 18
225
+ ```
226
+
147
227
  ### Use result on chain
148
228
  Like a following example, can use result on chain.
149
229
  ```ruby
@@ -162,7 +242,7 @@ foo = Foo.new(Bar.new(Baz.new))
162
242
  ```
163
243
  ###### Example: use result on chain
164
244
 
165
- 1. **String**
245
+ 1. **_String_**
166
246
  Can use bar instance in backward!
167
247
  ```ruby
168
248
  chain = PullChain.new(foo) << "bar/baz/say(bar.speaker, 'Good!')"
@@ -175,14 +255,14 @@ foo = Foo.new(Bar.new(Baz.new))
175
255
  chain.call # => Julian said 'Cool'
176
256
  ```
177
257
 
178
- 2. **Array**
179
- Can access result by Proc.
258
+ 2. **_Array_**
259
+ Can access result by *Proc*.
180
260
  ```ruby
181
261
  chain = PullChain.new(foo) << :bar << :baz
182
262
  chain << [:say, Proc.new { next bar.speaker, "Oh" }]
183
263
  chain.call # => Julian said 'Oh'
184
264
  ```
185
- Case of use a lambda, can use result access object explicit.
265
+ Case of use a *lambda*, can use result access object explicit.
186
266
  ```ruby
187
267
  chain = PullChain.new(foo) << :bar << :baz
188
268
  arg_reader = lambda { |accessor| next accessor.bar.speaker, "Oh" }
@@ -198,7 +278,7 @@ foo = Foo.new(Bar.new(Baz.new))
198
278
  chain.call # => AC/DC
199
279
  ```
200
280
 
201
- 2. **Use return_nil_at_error= method, then can ignore error**
281
+ 2. **Use *return_nil_at_error=*, then can ignore error**
202
282
  ```ruby
203
283
  chain = PullChain.new("Test") << :xxx
204
284
  begin
@@ -210,14 +290,14 @@ foo = Foo.new(Bar.new(Baz.new))
210
290
  ```
211
291
 
212
292
  3. **Note:use operator in chain**
213
- * **String type chain**
293
+ * **_String_ type chain**
214
294
  ```ruby
215
295
  table = {name: %w(Bill Scott Paul)}
216
296
  PullChain.new(table, "[:name]").call # => [:name] NG
217
297
  PullChain.new(table, "self[:name]").call # => ["Bill", "Scott", "Paul"] OK
218
298
  ```
219
299
 
220
- * **Array type chain**
300
+ * **_Array_ type chain**
221
301
  ```ruby
222
302
  PullChain.new(table, [:[], [:name]]).call # OK
223
303
  ```
@@ -236,7 +316,7 @@ foo = Foo.new(Bar.new(Baz.new))
236
316
  PullChain.new(%w(Donald Walter), "self[1]").call # OK => Walter
237
317
  ```
238
318
 
239
- 4. **Some classes, such Fixnum and Bignum not supported**
319
+ 4. **Some classes, such *Fixnum* and *Bignum* not supported**
240
320
  ```ruby
241
321
  PullChain.new(999999999999999, "self % 2").call # NG
242
322
  ```
@@ -244,7 +324,7 @@ foo = Foo.new(Bar.new(Baz.new))
244
324
  ---
245
325
  ## RelayChain
246
326
  RelayChain is object like a connect to function's input from function's output.
247
- (methods well as can connect Proc.)
327
+ (methods well as can connect *Proc*.)
248
328
 
249
329
  ### Example
250
330
  ```ruby
@@ -274,27 +354,50 @@ chain.call("Hello") # => { ( Hello ) }
274
354
  chain.call("Hello")
275
355
  ```
276
356
 
277
- 3. **Use Method object**
357
+ 3. **Use *Method* object**
278
358
  ```ruby
279
359
  chain = RelayChain.new
280
360
  chain >> decorator.method(:decorate1) >> decorator.method(:decorate2)
281
361
  chain.call("Hello")
282
362
  ```
283
363
 
284
- 4. **Use add method**
364
+ 4. **Use _add_**
285
365
  ```ruby
366
+ chain = RelayChain.new(Decorator.new)
286
367
  chain.add(:decorate1).add(:decorate2).call("Hello")
287
368
  ```
288
369
 
289
- 5. **Use add_all method**
370
+ 5. **Use _add_all_**
290
371
  ```ruby
372
+ chain = RelayChain.new(Decorator.new)
291
373
  chain.add_all(:decorate1, :decorate2).call("Hello")
292
374
  ```
293
375
 
294
- #### Insert, Delete, Clear
295
- insert, insert_all method is insert function to chain.
296
- delete_at method is delete function from chain.
297
- clear method is delete all function from chain.
376
+ #### Insert
377
+ *insert*, *insert_all* is insert function to chain.
378
+ ```ruby
379
+ chain = RelayChain.new(Decorator.new, :decorate2)
380
+ chain.insert(0, :decorate1)
381
+ chain.call("Hello") # => { ( Hello ) }
382
+
383
+ chain = RelayChain.new(Decorator.new)
384
+ chain.insert_all(0, :decorate1, :decorate2)
385
+ chain.call("Hello") # => { ( Hello ) }
386
+ ```
387
+ #### Delete
388
+ *delete_at* is delete function from chain.
389
+ ```ruby
390
+ chain = RelayChain.new(Decorator.new, :decorate1, :decorate2)
391
+ chain.delete_at(0)
392
+ chain.call("Hello") # => { Hello }
393
+ ```
394
+ #### Clear
395
+ *clear* is delete all function from chain.
396
+ ```ruby
397
+ chain = RelayChain.new(Decorator.new, :decorate1, :decorate2)
398
+ chain.clear
399
+ chain.call("Hello") # => nil
400
+ ```
298
401
 
299
402
  ### How to connect method of differed instance
300
403
  Example, following two class.
@@ -316,7 +419,7 @@ class Decorator2
316
419
  end
317
420
  ```
318
421
  ###### Solution
319
- 1. **Array, format is [instance, Symbol or String of method]**
422
+ 1. **_Array_, format is [instance, _Symbol_ or _String_ of method]**
320
423
  ```ruby
321
424
  # Symbol ver.
322
425
  chain = RelayChain.new(Decorator.new)
@@ -329,7 +432,7 @@ end
329
432
  chain.call("Hello") # => [ { ( Hello ) } ]
330
433
  ```
331
434
 
332
- 2. **String, use registered instance**
435
+ 2. **_String_, use registered instance**
333
436
  ```ruby
334
437
  chain = RelayChain.new(Decorator.new)
335
438
 
@@ -370,7 +473,7 @@ end
370
473
  chain.call("Emerson, Lake") # => Emerson, Lake And Palmer
371
474
  ```
372
475
 
373
- 2. **Add lambda or Proc to between these methods.**
476
+ 2. **Add *lambda* or *Proc* to between these methods.**
374
477
  lambda's format is following.
375
478
  ```ruby
376
479
  # parameter: chain is chain object.
@@ -385,9 +488,15 @@ end
385
488
  chain >> :decorate >> arg_adder >> :union
386
489
  chain.call("Tom") # => Tom And Jerry
387
490
  ```
491
+ can write as follows by *add* with block.
492
+ ```ruby
493
+ chain = RelayChain.new(Decorator.new)
494
+ chain.add(:decorate).add { |chain, value| chain.call(value, "Jerry") }.add(:union)
495
+ chain.call("Tom") # => Tom And Jerry
496
+ ```
388
497
 
389
498
  ### Appendix
390
- **Chain stop by means of lambda.**
499
+ **Chain stop by means of *lambda*.**
391
500
  ```ruby
392
501
  class Decorator
393
502
  def decorate1(value)
@@ -9,11 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Kenji Suzuki"]
10
10
  spec.email = ["pujoheadsoft@gmail.com"]
11
11
  spec.summary = "FunctionChain objectifies of the method chain."
12
- spec.description = <<-EOF.gsub(/^\s+|\n/, "")
13
- FunctionChain objectifies of the method chain.
14
- chain objects can call later or add chain or insert chain or delete chain.
15
- supported chain type is following: foo.bar.baz, baz(bar(foo(value))).
16
- EOF
12
+ spec.description = "FunctionChain objectifies of the method chain. chain objects can call later or add chain or insert chain or delete chain. supported chain type is following: foo.bar.baz, baz(bar(foo(value)))."
17
13
  spec.homepage = "https://github.com/pujoheadsoft/function_chain"
18
14
  spec.license = "MIT"
19
15
 
@@ -8,8 +8,10 @@ module FunctionChain
8
8
  end
9
9
 
10
10
  # Add function to chain
11
- def add(function)
12
- insert(chain_elements.size, function)
11
+ #
12
+ # Example: add(value) or add { your code }
13
+ def add(function = nil, &block)
14
+ insert(chain_elements.size, function, &block)
13
15
  end
14
16
 
15
17
  # Insert functions to chain
@@ -19,11 +21,14 @@ module FunctionChain
19
21
  end
20
22
 
21
23
  # Insert function to chain
22
- def insert(index, function)
23
- if function.is_a? String
24
- do_insert_by_string(index, function)
25
- else
26
- do_insert(index, function)
24
+ #
25
+ # Example: insert(i, value) or insert(i) { your code }
26
+ def insert(index, function = nil, &block)
27
+ validate_exclusive_value(function, block)
28
+ case function
29
+ when String then do_insert_by_string(index, function)
30
+ when NilClass then do_insert(index, block)
31
+ else do_insert(index, function)
27
32
  end
28
33
  self
29
34
  end
@@ -37,6 +42,7 @@ module FunctionChain
37
42
  # Clear function chain
38
43
  def clear
39
44
  chain_elements.clear
45
+ self
40
46
  end
41
47
 
42
48
  def to_s
@@ -65,11 +71,11 @@ module FunctionChain
65
71
  when Symbol then create_chain_element_by_symbol(function)
66
72
  when Array then create_chain_element_by_array(function)
67
73
  when String then create_chain_element_by_string(function)
68
- else
69
- fail ArgumentError, <<-EOF.gsub(/^\s+|\n/, "")
74
+ when Proc then create_chain_element_by_proc(function)
75
+ else fail ArgumentError, <<-EOF.gsub(/^\s+|\n/, "")
70
76
  Not supported type #{function}(#{function.class}),
71
77
  supported type is #{supported_types}.
72
- EOF
78
+ EOF
73
79
  end
74
80
  end
75
81
 
@@ -104,7 +110,19 @@ module FunctionChain
104
110
  end
105
111
 
106
112
  def supported_types
107
- [Symbol, Array, String]
113
+ [Symbol, Array, String, Proc]
114
+ end
115
+
116
+ def validate_exclusive_value(function, block)
117
+ prefix = nil
118
+ if function.nil? && block.nil?
119
+ prefix = "Both value and the block is unspecified."
120
+ elsif function && block
121
+ prefix = "Both of value and block is specified."
122
+ end
123
+ if prefix
124
+ fail ArgumentError, "#{prefix} Please specify either value or block."
125
+ end
108
126
  end
109
127
  end
110
128
  end
@@ -247,6 +247,12 @@ module FunctionChain
247
247
  end
248
248
  end
249
249
 
250
+ def create_chain_element_by_proc(proc)
251
+ create_common_chain_element do |receiver|
252
+ next "#{proc}", receiver.instance_eval(&proc)
253
+ end
254
+ end
255
+
250
256
  def execute(receiver, name, *args, &block)
251
257
  receiver.__send__(name, *args, &block)
252
258
  end
@@ -193,7 +193,7 @@ module FunctionChain
193
193
  protected
194
194
 
195
195
  def supported_types
196
- super() | [Method, Proc]
196
+ super() | [Method]
197
197
  end
198
198
 
199
199
  private
@@ -201,7 +201,6 @@ module FunctionChain
201
201
  def create_chain_element(function)
202
202
  case function
203
203
  when Method then create_chain_element_by_method(function)
204
- when Proc then function
205
204
  else super(function)
206
205
  end
207
206
  end
@@ -242,6 +241,10 @@ module FunctionChain
242
241
  end
243
242
  end
244
243
 
244
+ def create_chain_element_by_proc(proc)
245
+ proc
246
+ end
247
+
245
248
  def receiver_table
246
249
  @receiver_table ||= {}
247
250
  end
@@ -1,3 +1,3 @@
1
1
  module FunctionChain
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -116,6 +116,21 @@ describe FunctionChain::PullChain do
116
116
  expect(chain.call).to eq("AC".concat("/DC"))
117
117
  end
118
118
 
119
+ it "[success case] proc type function" do
120
+ chain = PullChain.new(@city) << Proc.new { bookstore }
121
+ chain << Proc.new { shelves[:programing] } << Proc.new { books[1] }
122
+ chain << Proc.new { title }
123
+ title = @city.bookstore.shelves[:programing].books[1].title
124
+ expect(chain.call).to eq(title)
125
+ end
126
+
127
+ it "[success case] block type function" do
128
+ chain = PullChain.new(@city).add { bookstore }
129
+ chain.add { shelves[:programing] }.add { books[1] }.add { title }
130
+ title = @city.bookstore.shelves[:programing].books[1].title
131
+ expect(chain.call).to eq(title)
132
+ end
133
+
119
134
  it "[success case] mix type string, symbol, array" do
120
135
  chain = PullChain.new(@city)
121
136
  chain.add_all(:bookstore, "shelves[:programing]")
@@ -124,14 +139,21 @@ describe FunctionChain::PullChain do
124
139
  expect(chain.call).to eq(title)
125
140
  end
126
141
 
127
- it "[success case] insert_all" do
142
+ it "[success case] insert" do
128
143
  chain = PullChain.new(@city, "/bookstore/shelves[:programing]/title")
129
144
  chain.insert(2, "books[1]")
130
145
  title = @city.bookstore.shelves[:programing].books[1].title
131
146
  expect(chain.call).to eq(title)
132
147
  end
133
148
 
134
- it "[success case] inserts" do
149
+ it "[success case] insert block" do
150
+ chain = PullChain.new(@city, "/bookstore/shelves[:programing]/title")
151
+ chain.insert(2) { books[1] }
152
+ title = @city.bookstore.shelves[:programing].books[1].title
153
+ expect(chain.call).to eq(title)
154
+ end
155
+
156
+ it "[success case] insert_all" do
135
157
  chain = PullChain.new(@city, "/bookstore/title")
136
158
  chain.insert_all(1, "shelves[:programing]", "books[1]")
137
159
  title = @city.bookstore.shelves[:programing].books[1].title
@@ -224,6 +246,38 @@ describe FunctionChain::PullChain do
224
246
  end.to raise_error(ArgumentError)
225
247
  end
226
248
 
249
+ it "[fail case] add empty" do
250
+ msg = "Both value and the block is unspecified. " \
251
+ "Please specify either value or block."
252
+ expect do
253
+ PullChain.new(@city).add
254
+ end.to raise_error(ArgumentError, msg)
255
+ end
256
+
257
+ it "[fail case] add both arg & block" do
258
+ msg = "Both of value and block is specified. " \
259
+ "Please specify either value or block."
260
+ expect do
261
+ PullChain.new(@city).add(:bookstore) {}
262
+ end.to raise_error(ArgumentError, msg)
263
+ end
264
+
265
+ it "[fail case] insert empty" do
266
+ msg = "Both value and the block is unspecified. " \
267
+ "Please specify either value or block."
268
+ expect do
269
+ PullChain.new(@city).insert(0)
270
+ end.to raise_error(ArgumentError, msg)
271
+ end
272
+
273
+ it "[fail case] insert both arg & block" do
274
+ msg = "Both of value and block is specified. " \
275
+ "Please specify either value or block."
276
+ expect do
277
+ PullChain.new(@city).insert(0, :bookstore) {}
278
+ end.to raise_error(ArgumentError, msg)
279
+ end
280
+
227
281
  %w(! " ' # % & ( ) = ~ \\ ` @ [ ] * + < > ? 1 ; : . , ^).each do |e|
228
282
  it "[fail case] ArgumentError:wrong format variable define #{e}" do
229
283
  expect do
@@ -231,7 +285,6 @@ describe FunctionChain::PullChain do
231
285
  end.to raise_error(ArgumentError)
232
286
  end
233
287
  end
234
-
235
288
  end
236
289
 
237
290
  describe FunctionChain::RelayChain do
@@ -379,6 +432,15 @@ describe FunctionChain::RelayChain do
379
432
  expect(chain.call(array)).to eq(value)
380
433
  end
381
434
 
435
+ it "[success case] use block" do
436
+ chain = RelayChain.new
437
+ chain.add { |c, arr| c.call(arr.select(&:even?)) }
438
+ chain.add { |_, arr| arr.map { |num| num * 10 } }
439
+ array = [1, 2, 3, 4, 5]
440
+ value = array.select(&:even?).map { |num| num * 10 }
441
+ expect(chain.call(array)).to eq(value)
442
+ end
443
+
382
444
  it "[success case] stop of chain" do
383
445
  chain = RelayChain.new(@decorator)
384
446
  chain.add_all(:decorate1, :decorate2, [@enclose_decorator, :decorate])
@@ -388,6 +450,14 @@ describe FunctionChain::RelayChain do
388
450
  expect(chain.call("Montgomery")).to eq(decorated_value)
389
451
  end
390
452
 
453
+ it "[success case] stop of chain block ver" do
454
+ chain = RelayChain.new(@decorator)
455
+ chain.add_all(:decorate1, :decorate2, [@enclose_decorator, :decorate])
456
+ chain.insert(2) { |_, value| value }
457
+ decorated_value = @decorator.decorate2(@decorator.decorate1("Montgomery"))
458
+ expect(chain.call("Montgomery")).to eq(decorated_value)
459
+ end
460
+
391
461
  it "[success case] to_s" do
392
462
  chain = RelayChain.new
393
463
  stopper = lambda { |_, value| value }
@@ -414,4 +484,36 @@ describe FunctionChain::RelayChain do
414
484
  end.to raise_error(ArgumentError)
415
485
  end
416
486
 
487
+ it "[fail case] add empty" do
488
+ msg = "Both value and the block is unspecified. " \
489
+ "Please specify either value or block."
490
+ expect do
491
+ RelayChain.new(@decorator).add
492
+ end.to raise_error(ArgumentError, msg)
493
+ end
494
+
495
+ it "[fail case] add both arg & block" do
496
+ msg = "Both of value and block is specified. " \
497
+ "Please specify either value or block."
498
+ expect do
499
+ RelayChain.new(@decorator).add(:decorate1) {}
500
+ end.to raise_error(ArgumentError, msg)
501
+ end
502
+
503
+ it "[fail case] insert empty" do
504
+ msg = "Both value and the block is unspecified. " \
505
+ "Please specify either value or block."
506
+ expect do
507
+ RelayChain.new(@decorator).insert(0)
508
+ end.to raise_error(ArgumentError, msg)
509
+ end
510
+
511
+ it "[fail case] insert both arg & block" do
512
+ msg = "Both of value and block is specified. " \
513
+ "Please specify either value or block."
514
+ expect do
515
+ RelayChain.new(@decorator).insert(0, :decorate1) {}
516
+ end.to raise_error(ArgumentError, msg)
517
+ end
518
+
417
519
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: function_chain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenji Suzuki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-15 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: 'FunctionChain objectifies of the method chain.chain objects can call
14
- later or add chain or insert chain or delete chain.supported chain type is following:
13
+ description: 'FunctionChain objectifies of the method chain. chain objects can call
14
+ later or add chain or insert chain or delete chain. supported chain type is following:
15
15
  foo.bar.baz, baz(bar(foo(value))).'
16
16
  email:
17
17
  - pujoheadsoft@gmail.com
@@ -19,11 +19,11 @@ executables: []
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
- - .coveralls.yml
23
- - .gitignore
24
- - .rspec
25
- - .rubocop.yml
26
- - .travis.yml
22
+ - ".coveralls.yml"
23
+ - ".gitignore"
24
+ - ".rspec"
25
+ - ".rubocop.yml"
26
+ - ".travis.yml"
27
27
  - Gemfile
28
28
  - LICENSE.txt
29
29
  - README.md
@@ -49,12 +49,12 @@ require_paths:
49
49
  - lib
50
50
  required_ruby_version: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.9.3
55
55
  required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - '>='
57
+ - - ">="
58
58
  - !ruby/object:Gem::Version
59
59
  version: 1.3.5
60
60
  requirements: []