verbalize 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28277ada3b560d5fd8ed11a9b568dd03f8c0156a
4
- data.tar.gz: 9ff1315685ffa11c9a997a15a07206fe0e21f112
3
+ metadata.gz: 906d5dc6f28ab649fe2d08a666726e7b6718957f
4
+ data.tar.gz: 5689edea2dbfa8a0789eb07622420ff3753fe506
5
5
  SHA512:
6
- metadata.gz: e4049ff78ca84f4e44f34c6525089edb96b74206ca6318f3b84c78779dd303863a612f0be86059e9e6bd1d8fce2f364a62e3d65b6269575ee13cd1dc7bbd44b9
7
- data.tar.gz: a6528d2fa2ec1dc97d9524f6feb75426a249cc6a8bd2b656d7491d2bb5698512ec9718c836f16b33c0bc85d65aba5551a6d135170e2e1ebfb5fdf37c86770948
6
+ metadata.gz: 2e80ec6ed639bf4e17cd88ac5c03074d79a2f38250193ccb7586c711c4fde1a84851228a93e8550f3af53cb8d7eeec966fff4a04ace5b41af7f4265dc8b16035
7
+ data.tar.gz: f25395a64d6255547806f218cec5c6c2615b82245865394c1fd1cf4af19f5f9c40c74fb75451e2cbff667aa8fa8387f8ddc76d49b8bc5c99767a16fe4b1655b0
data/README.md CHANGED
@@ -69,6 +69,33 @@ result = Divide.call(a: 1, b: 0) # => [:error, 'You can’t divide by 0']
69
69
  result.failed? # => true
70
70
  ```
71
71
 
72
+ ### Reflection
73
+ ```ruby
74
+ class Add
75
+ include Verbalize::Action
76
+
77
+ input :a, :b, optional: [:c, :d]
78
+
79
+ def call
80
+ a + b + c + d
81
+ end
82
+
83
+ private
84
+
85
+ def c
86
+ @c ||= 0
87
+ end
88
+
89
+ def d
90
+ @d ||= 0
91
+ end
92
+ end
93
+
94
+ Add.required_inputs # [:a, :b]
95
+ Add.optional_inputs # [:c, :d]
96
+ Add.inputs # [:a, :b, :c, :d]
97
+ ```
98
+
72
99
  ## Comparison/Benchmark
73
100
  ```ruby
74
101
  require 'verbalize'
@@ -155,15 +182,15 @@ Comparison:
155
182
  ### Happy Path
156
183
 
157
184
  When testing positive cases of a `Verbalize::Action`, it is recommended to test using the `call!` class method and
158
- assert on the result. This implicitly ensures a successful result, and your tests will fail with a bang! if
185
+ assert on the result. This implicitly ensures a successful result, and your tests will fail with a bang! if
159
186
  something goes wrong:
160
187
 
161
188
  ```ruby
162
189
  class MyAction
163
190
  include Verbalize::Action
164
-
191
+
165
192
  input :a
166
-
193
+
167
194
  def call
168
195
  fail!('#{a} is greater than than 100!') if a >= 100
169
196
  a + 1
@@ -178,28 +205,28 @@ end
178
205
 
179
206
  ### Sad Path
180
207
 
181
- When testing negative cases of a `Verbalize::Action`, it is recommended to test using the `call` non-bang
208
+ When testing negative cases of a `Verbalize::Action`, it is recommended to test using the `call` non-bang
182
209
  class method which will return a `Verbalize::Failure` on failure.
183
210
 
184
- Use of `call!` here is not advised as it will result in an exception being thrown. Set assertions on both
211
+ Use of `call!` here is not advised as it will result in an exception being thrown. Set assertions on both
185
212
  the failure outcome and value:
186
213
 
187
214
  ```ruby
188
215
  class MyAction
189
216
  include Verbalize::Action
190
-
217
+
191
218
  input :a
192
-
219
+
193
220
  def call
194
221
  fail!('#{a} is greater than 100!') if a >= 100
195
222
  a + 1
196
223
  end
197
224
  end
198
-
225
+
199
226
  # rspec:
200
227
  it 'fails when the input is out of bounds' do
201
228
  result = MyAction.call(a: 1000)
202
-
229
+
203
230
  expect(result).to be_failed
204
231
  expect(result.failure).to eq '1000 is greater than 100!'
205
232
  end
@@ -208,11 +235,11 @@ end
208
235
  ### Stubbing Responses
209
236
 
210
237
  When unit testing, it may be necessary to stub the responses of Verbalize actions. To correctly stub responses,
211
- you should __always__ stub the `MyAction.perform` class method on the action class being stubbed per the
238
+ you should __always__ stub the `MyAction.perform` class method on the action class being stubbed per the
212
239
  instructions below. __Never__ stub the `call` or `call!` methods directly.
213
240
 
214
241
  Stubbing `.perform` will enable `Verbalize` to wrap results correctly for references to either `call` or `call!`.
215
-
242
+
216
243
  #### Stubbing Successful Responses
217
244
 
218
245
  To simulate a successful response of the `Verbalize::Action` being stubbed, you should stub the `MyAction.perform`
@@ -225,7 +252,7 @@ class Foo
225
252
  def self.multiply_by(multiple)
226
253
  result = MyAction.call(a: 1)
227
254
  raise "I couldn't do the thing!" if result.failure?
228
-
255
+
229
256
  result.value * multiple
230
257
  end
231
258
  end
@@ -238,9 +265,9 @@ describe Foo do
238
265
  allow(MyAction).to receive(:perform)
239
266
  .with(a: 1)
240
267
  .and_return(123)
241
-
268
+
242
269
  result = described_class.multiply_by(100)
243
-
270
+
244
271
  expect(result).to eq 12300
245
272
  end
246
273
  end
@@ -266,7 +293,7 @@ describe Foo do
266
293
  allow(MyAction).to receive(:perform)
267
294
  .with(a: 1)
268
295
  .and_throw(::Verbalize::THROWN_SYMBOL, 'Y U NO!')
269
-
296
+
270
297
  expect {
271
298
  described_class.multiply_by(100)
272
299
  }.to raise_error "I couldn't do the thing!"
@@ -305,4 +332,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/taylor
305
332
  ## License
306
333
 
307
334
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
308
-
@@ -17,9 +17,24 @@ module Verbalize
17
17
 
18
18
  module ClassMethods
19
19
  def input(*required_keywords, optional: [])
20
+ @required_inputs = required_keywords
21
+ @optional_inputs = optional
22
+
20
23
  class_eval Build.call(required_keywords, Array(optional))
21
24
  end
22
25
 
26
+ def required_inputs
27
+ @required_inputs || []
28
+ end
29
+
30
+ def optional_inputs
31
+ @optional_inputs || []
32
+ end
33
+
34
+ def inputs
35
+ required_inputs + optional_inputs
36
+ end
37
+
23
38
  # Because call/call! are defined when Action.input is called, they would
24
39
  # not be defined when there is no input. So we pre-define them here, and
25
40
  # if there is any input, they are overwritten
@@ -30,6 +45,7 @@ module Verbalize
30
45
  def call!
31
46
  __proxied_call!
32
47
  end
48
+ alias_method :!, :call!
33
49
 
34
50
  private
35
51
 
@@ -10,13 +10,18 @@ module Verbalize
10
10
  end
11
11
 
12
12
  def call
13
+ # We have to re-alias `!` to `call!` here, otherwise it will be pointing
14
+ # to the original `call!` method
13
15
  <<-CODE
14
- def self.call(#{declaration_arguments_string})
15
- __proxied_call(#{forwarding_arguments_string})
16
- end
16
+ class << self
17
+ def call(#{declaration_arguments_string})
18
+ __proxied_call(#{forwarding_arguments_string})
19
+ end
17
20
 
18
- def self.call!(#{declaration_arguments_string})
19
- __proxied_call!(#{forwarding_arguments_string})
21
+ def call!(#{declaration_arguments_string})
22
+ __proxied_call!(#{forwarding_arguments_string})
23
+ end
24
+ alias_method :!, :call!
20
25
  end
21
26
 
22
27
  def initialize(#{declaration_arguments_string})
@@ -1,3 +1,3 @@
1
1
  module Verbalize
2
- VERSION = '2.0.1'.freeze
2
+ VERSION = '2.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verbalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-16 00:00:00.000000000 Z
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler