simple_params 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTg5ZjU4NGE5ODRkMmYwMDNlY2E5OTM2ZjgwYTcyYjJlYzk2YTM1MQ==
4
+ ZWM1MGE4MTFhODA2YzgzZDFiNDFmNGIxZjFiNTIxMzI4ZDNjOTg5OQ==
5
5
  data.tar.gz: !binary |-
6
- OWQ1ODFkYzljM2I5ZmNlOGYwZGU5Mzc3ZTY1Y2I5ZjRiNGMxMzJjMw==
6
+ MGJlNGI0MDM4MGFlZTA5NDUwZTBhYTdhNDRhMmI5YzYwYTRhMTBhNw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjhjZTMzNmUwNzc5NTg5NmNhODQxYjhlYzBiZThjNGVkMWExZDc2NzM4NDI5
10
- NzUwNjhhMzExNGFjYzg0NjVlOWE0MzRhNDEwNzAyNzZhZGYyY2ZlMDdmMDFm
11
- M2NjZWI2OGM0NWU0ZmYxMWVlYTMzZmM5YTcxZDJkZjQ1YzhjMWU=
9
+ NGJmZDhjNDYxOWYxZjc0ODk5OGMwNjdmNDQwMGE4NDgyZjg4NGU3MDkwODdl
10
+ NmQ4YTFhNGNiNzUyYTMzODFmODVlYzgzYmU4MzdkYjY5MmIxNGMxMDJiMzZk
11
+ OWYyZDVmNDc5N2JjZjAwMzEwMWEzZjZkOTdlNjY4ZTA2NzNmNDY=
12
12
  data.tar.gz: !binary |-
13
- MjhkZWI0OWVkYTQyMjIwYzk0OTRhNGNjNjM5NTMwMTFmYmYzZjA1ODdlZDRj
14
- MmU4ZGZkZTAwN2IwZTcyNTgyMDQ1YTY2ODNhMjFiYTViZWFlMDA2NjAzOWFl
15
- YTE3OWZkZmNhMGE4ZTIyMjk0Nzk2ZjNhY2JmYmYyMzIzYTc4MWQ=
13
+ MWY2OGU5NmUyM2Y5ZWY1YmExYWY5NTkxOTc2ZDk2ZjUwNTNjZGRlNTFlMGJm
14
+ NjVmN2ZkZTdhNDM1M2RjNDY1YjY2ZjYyMWJiY2M5MmVmY2YyYjk0YTA1YzI5
15
+ ODczNjgzMjhmNjIwNzBmNDY0ZmYwNDVlMmEyZmE5ZTY0NWY1ZDE=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (0.0.3)
4
+ simple_params (0.0.4)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
data/README.md CHANGED
@@ -96,6 +96,20 @@ params.errors.as_json #=> {:name=>["can't be blank"], :address=>{:street=>["can'
96
96
  params.address.errors.as_json #=> {:street=>["can't be blank"]}
97
97
  ```
98
98
 
99
+ Custom validate methods can also be added, just as with an ActiveModel object
100
+ ```ruby
101
+ class MyParams < SimpleParams::Params
102
+ param :name
103
+ validate :name_has_letters
104
+
105
+ def name_has_letters
106
+ if name.present? && !(name =~ /^[a-zA-Z]*$/)
107
+ errors.add(:name, "must only contain letters")
108
+ end
109
+ end
110
+ end
111
+ ```
112
+
99
113
  ## Defaults
100
114
 
101
115
  It is easy to set simple or complex defaults, with either a static value or a Proc
@@ -238,6 +252,153 @@ RSpec.configure do |config|
238
252
  end
239
253
  ```
240
254
 
255
+ # Testing with Validation Matchers
256
+
257
+ Simple Params includes the following validation matchers:
258
+ CoercionMatcher, FormatMatcher, NestedParameterMatcher, OptionalParameterMatcher, and RequiredParameterMatcher
259
+
260
+ #CoercionMatcher
261
+
262
+ Example:
263
+
264
+ Class to test
265
+
266
+ ```ruby
267
+ class YourClass < SimpleParams::Params
268
+ param :name, type: :string
269
+ param :expiration_date, type: :date
270
+ param :amount, type: :integer
271
+ end
272
+ ```
273
+ Test using Coercion Matcher
274
+
275
+ ```ruby
276
+ describe YourClass do
277
+ it { should coerce_param(:name).into(:string) }
278
+ it { should_not coerce_param(:name).into(:integer) }
279
+ it { should coerce_param(:expiration_date).into(:date) }
280
+ it { should_not coerce_param(:expiration_date).into(:string) }
281
+ it { should coerce_param(:amount).into(:integer) }
282
+ it { should_not coerce_param(:amount).into(:float) }
283
+ end
284
+ ```
285
+
286
+ #FormatMatcher
287
+
288
+ Example:
289
+
290
+ Class to test
291
+
292
+ ```ruby
293
+ class YourClass < SimpleParams::Params
294
+ param :amount, type: :float, formatter: lambda { |params, amt| sprintf('%.2f', amt) }
295
+ param :expiration_date, type: :date, formatter: lambda { |params, date| date.strftime("%Y-%m")}
296
+ param :cost, type: :float, formatter: lambda { |params, amt| sprintf('%.2f', amt) }
297
+ end
298
+ ```
299
+ Test using FormatMatcher
300
+
301
+ ```ruby
302
+ describe YourClass do
303
+ it { should format(:amount).with_value(10).into("10.00") }
304
+ it { should format(:expiration_date).with_value(Date.new(2014, 2, 4)).into("2014-02") }
305
+ it { should_not format(:cost).with_value(12).into("14.00") }
306
+ end
307
+ ```
308
+
309
+ #NestedParameterMatcher
310
+
311
+ Example:
312
+
313
+ Class to test
314
+
315
+ ```ruby
316
+ class YourClass < SimpleParams::Params
317
+ param :name
318
+ param :age, optional: true, default: 37
319
+ param :title, optional: true, default: "programmer"
320
+ param :account_type, default: "checking", validations: { inclusion: { in: ["checking", "savings"] }}
321
+ param :account_status, optional: true, validations: { inclusion: { in: ["active", "inactive"] }}
322
+ nested_param :billing_address do
323
+ param :first_name
324
+ param :last_name
325
+ param :company, optional: true
326
+ param :street
327
+ param :city
328
+ param :state
329
+ param :zip_code
330
+ param :country
331
+ end
332
+ ```
333
+
334
+ Test using NestedParameterMatcher
335
+
336
+ ```ruby
337
+ describe YourClass do
338
+ it { should have_nested_parameter(:billing_address) }
339
+ it { should_not have_nested_parameter(:broken) }
340
+ end
341
+ ```
342
+
343
+ Note that OptionalParameterMatcher and RequiredParameterMatcher have with_default and with_allowed_values options
344
+
345
+ #OptionalParameterMatcher
346
+
347
+ Example:
348
+
349
+ Class to test
350
+
351
+ ```ruby
352
+ class YourClass < SimpleParams::Params
353
+ param :name
354
+ param :age, optional: true, default: 37
355
+ param :title, optional: true, default: "programmer"
356
+ param :account_type, default: "checking", validations: { inclusion: { in: ["checking", "savings"] }}
357
+ param :account_status, optional: true, validations: { inclusion: { in: ["active", "inactive"] }}
358
+ end
359
+ ```
360
+
361
+ Test using OptionalParameterMatcher
362
+
363
+ ```ruby
364
+ describe YourClass do
365
+ it { should_not have_optional_parameter(:name) }
366
+ it { should have_optional_parameter(:age).with_default(37) }
367
+ it { should have_optional_parameter(:title).with_default("programmer") }
368
+ it { should have_optional_parameter(:account_status).with_allowed_values("active", "inactive") }
369
+ it { should have_optional_parameter(:account_type).with_default("checking").with_allowed_values("checking", "savings") }
370
+ end
371
+ ```
372
+
373
+ #RequiredParameterMatcher
374
+
375
+ Example:
376
+
377
+ Class to test
378
+
379
+ ```ruby
380
+ class YourClass < SimpleParams::Params
381
+ param :name
382
+ param :age, optional: true
383
+ param :title, default: "programmer"
384
+ param :account_type, validations: { inclusion: { in: ["checking", "savings"] }}
385
+ param :account_status, default: "active", validations: { inclusion: { in: ["active", "inactive"] }}
386
+ end
387
+ ```
388
+
389
+ Test using RequiredParameterMatcher
390
+
391
+ ```ruby
392
+ describe YourClass do
393
+ it { should have_required_parameter(:name) }
394
+ it { should_not have_required_parameter(:age) }
395
+ it { should_not have_required_parameter(:name).with_default("Matthew") }
396
+ it { should have_required_parameter(:title).with_default("programmer") }
397
+ it { should have_required_parameter(:account_type).with_allowed_values("checking", "savings") }
398
+ it { should have_required_parameter(:account_status).with_default("active").with_allowed_values("active", "inactive") }
399
+ end
400
+ ```
401
+
241
402
  ## Contributing
242
403
 
243
404
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -5,6 +5,7 @@ class AcceptanceParams < SimpleParams::Params
5
5
  param :name
6
6
  param :age, type: :integer, optional: true, validations: { inclusion: { in: 18..100 } }
7
7
  param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}
8
+ validate :name_has_letters
8
9
 
9
10
  nested_hash :address do
10
11
  param :street
@@ -12,6 +13,12 @@ class AcceptanceParams < SimpleParams::Params
12
13
  param :zip_code, optional: true
13
14
  param :state, default: "North Carolina"
14
15
  end
16
+
17
+ def name_has_letters
18
+ if name.present? && !(name =~ /^[a-zA-Z]*$/)
19
+ errors.add(:name, "must only contain letters")
20
+ end
21
+ end
15
22
  end
16
23
 
17
24
  describe SimpleParams::Params do
@@ -138,6 +145,12 @@ describe SimpleParams::Params do
138
145
  params.errors[:name].should eq(["can't be blank"])
139
146
  end
140
147
 
148
+ it "runs custom validate methods" do
149
+ params.name = "!!!"
150
+ params.should_not be_valid
151
+ params.errors[:name].should eq(["must only contain letters"])
152
+ end
153
+
141
154
  it "does not validate presence of optional param" do
142
155
  params.should_not be_valid
143
156
  params.errors[:age].should be_empty
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
11
+ date: 2015-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel