subroutine 0.2.0 → 0.2.1

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: 212d37192891dfad2e6fced663590d50f37302ee
4
- data.tar.gz: 2ded356bfcfe49f9b697aba9784325fe2ce4426a
3
+ metadata.gz: 4f44648d17e8af9de061d865e831e95811de6ff4
4
+ data.tar.gz: b5347035ebf89c8e17a45b49333fb9b1a0812555
5
5
  SHA512:
6
- metadata.gz: b4dc8ba20210de9eb5d2260f5cc98f95fe6a9c74e9a0ee625434a683a1773273efdc5461e2356238ca63bd7457c9554748f35fecad40a2153b665b81953ac5d1
7
- data.tar.gz: cf30411572f31668031cc96ac76cb0ce4edb6132b22f9990a38023d32e312b53468179a31dea8604e25e54e0ac69189a7df13f338559c000d8980bb4aada2a2e
6
+ metadata.gz: 9e8cb47942615ef6cee08df82ff32c026a1824aac18e3e07dacc74e71bb23c30f9959493491d3cb358ec6e4daccbe155c0562b4fa0794efd85b29a9ffa39a5cd
7
+ data.tar.gz: aedf7e3533db2a99570634e97a74cc82bb95673ed9f256153b7844a94638ce49436717c7912b7a552dbe13985c603078977270ff3f527128379b28cc03be5680
data/README.md CHANGED
@@ -213,6 +213,32 @@ class MyOp < ::Subroutine::Op
213
213
  end
214
214
  ```
215
215
 
216
+ All **provided** params are accessible via the `params` accessor. All default values are accessible via the `defaults` accessor. The combination of the two is available via `params_with_defaults`.
217
+
218
+ ```ruby
219
+ class MyOp < ::Subroutine::Op
220
+ string :name
221
+ string :status, default: "browsing"
222
+
223
+ def perform
224
+ puts params.inspect
225
+ puts defaults.inspect
226
+ puts params_with_defaults.inspect
227
+ true
228
+ end
229
+ end
230
+
231
+ MyOp.submit(name: "foobar", status: nil)
232
+ # => { name: "foobar" }
233
+ # => { status: "browsing" }
234
+ # => { name: "foobar", status: nil }
235
+
236
+ MyOp.submit(name: "foobar")
237
+ # => { name: "foobar" }
238
+ # => { status: "browsing" }
239
+ # => { name: "foobar", status: "browsing" }
240
+ ```
241
+
216
242
  #### Execution
217
243
 
218
244
  Every op must implement a `perform` instance method. This is the method which will be executed if all validations pass.
data/lib/subroutine/op.rb CHANGED
@@ -101,7 +101,7 @@ module Subroutine
101
101
  end
102
102
 
103
103
  def #{field_name}
104
- @params["#{field_name}"]
104
+ @params.has_key?("#{field_name}") ? @params["#{field_name}"] : @defaults["#{field_name}"]
105
105
  end
106
106
 
107
107
  def #{field_name}_config
@@ -129,12 +129,13 @@ module Subroutine
129
129
  self._error_ignores = {}
130
130
 
131
131
  attr_reader :original_params
132
- attr_reader :params
132
+ attr_reader :params, :defaults
133
133
 
134
134
 
135
135
  def initialize(inputs = {})
136
136
  @original_params = inputs.with_indifferent_access
137
137
  @params = sanitize_params(@original_params)
138
+ @defaults = sanitize_defaults
138
139
  end
139
140
 
140
141
  def errors
@@ -164,6 +165,10 @@ module Subroutine
164
165
  end
165
166
  end
166
167
 
168
+ def params_with_defaults
169
+ @defaults.merge(@params)
170
+ end
171
+
167
172
  protected
168
173
 
169
174
  def type_caster
@@ -236,16 +241,27 @@ module Subroutine
236
241
  self._fields.each_pair do |field, config|
237
242
  if inputs.has_key?(field)
238
243
  out[field] = type_caster.cast(inputs[field], config[:type])
239
- elsif config[:default]
244
+ end
245
+ end
246
+
247
+ out
248
+ end
249
+
250
+ def sanitize_defaults
251
+ defaults = {}.with_indifferent_access
252
+
253
+ self._fields.each_pair do |field, config|
254
+ if config[:default]
240
255
  deflt = config[:default]
241
256
  deflt = deflt.call if deflt.respond_to?(:call)
242
- out[field] = type_caster.cast(deflt, config[:type])
257
+ defaults[field] = type_caster.cast(deflt, config[:type])
243
258
  end
244
259
  end
245
260
 
246
- out
261
+ defaults
247
262
  end
248
263
 
264
+
249
265
  end
250
266
 
251
267
  end
@@ -2,7 +2,7 @@ module Subroutine
2
2
 
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- PATCH = 0
5
+ PATCH = 1
6
6
  PRE = nil
7
7
 
8
8
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
@@ -144,12 +144,38 @@ module Subroutine
144
144
  assert_equal [], op.errors[:whatever]
145
145
  end
146
146
 
147
- def test_it_sets_the_params_immediately_and_includes_defaults
147
+ def test_it_sets_the_params_and_defaults_immediately
148
148
  op = ::AdminSignupOp.new(email: "foo")
149
149
  assert_equal({
150
- "priveleges" => "min",
151
150
  "email" => "foo"
152
151
  }, op.params)
152
+
153
+ assert_equal({
154
+ "priveleges" => "min",
155
+ }, op.defaults)
156
+
157
+ assert_equal({
158
+ "email" => "foo",
159
+ "priveleges" => "min",
160
+ }, op.params_with_defaults)
161
+ end
162
+
163
+ def test_it_allows_defaults_to_be_overridden
164
+ op = ::AdminSignupOp.new(email: "foo", priveleges: nil)
165
+
166
+ assert_equal({
167
+ "email" => "foo",
168
+ "priveleges" => nil
169
+ }, op.params)
170
+
171
+ assert_equal({
172
+ "priveleges" => "min",
173
+ }, op.defaults)
174
+
175
+ assert_equal({
176
+ "email" => "foo",
177
+ "priveleges" => nil,
178
+ }, op.params_with_defaults)
153
179
  end
154
180
 
155
181
  def test_it_overrides_defaults_with_nils
@@ -162,10 +188,10 @@ module Subroutine
162
188
 
163
189
  def test_it_casts_params_on_the_way_in
164
190
  op = ::TypeCastOp.new(integer_input: "25")
165
- assert_equal(25, op.params["integer_input"])
191
+ assert_equal(25, op.params_with_defaults["integer_input"])
166
192
 
167
193
  op.decimal_input = "25.3"
168
- assert_equal(BigDecimal("25.3"), op.params["decimal_input"])
194
+ assert_equal(BigDecimal("25.3"), op.params_with_defaults["decimal_input"])
169
195
  end
170
196
 
171
197
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subroutine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-19 00:00:00.000000000 Z
11
+ date: 2016-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel