simple_params 1.0.5 → 1.0.6

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
- MDJjYTQ5MTFmNzEyZmRlYjViMjQyZDY5YzlmNDQ2ZmJiYzU4ZjQyNg==
4
+ MDJmOTE0MzM0NTQzZmNiMTkzMjFiNjc3ZTdlNzc3MjhkNWViODQ1Ng==
5
5
  data.tar.gz: !binary |-
6
- MjYwZTM3OWE3ZjczODI3NzM5YzhhODFiYzU3ZDA5NTRjYTdkZWU4Mg==
6
+ OGExMjA2YTNlMDhkYzM3MjE5YjQ1OWJmNWU5NGZkYzE5ZWUyY2E1OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjVlNjMxNDlkZTkwOTdiYTllZTg4MGRmYTZhYTE1OWIzN2Y5NjMzNmE0NzUy
10
- YzQ2YjM4NGM2ZDc2OTAxNjc2NmU3MjA5YTQzMGI5ZWUyYzU4MDQ0NDhiMDcw
11
- MmI2Mjg0ZWI3ZDEzOTY1ZGJmMWQyYzI3ZTA1OWJmYzc1Mzk1N2Q=
9
+ YjgxYzY5N2YyYjZmMjdmOWJhN2Q5OTNjNDY0YzBmM2VhZWNlNjc1ZGY0NzRk
10
+ ODE5OTJhMTQ1ZmI2MTRiZWI3YjBmODZlMGVhYWVhODM5NGYzY2M4MWY1ODBm
11
+ NmMxODBhYWQ3M2RiMzQzMzViYWFjZmUxOGQ4NmIwN2Q5Nzk0NDc=
12
12
  data.tar.gz: !binary |-
13
- NWQxMzE3ZjRlNDM3MmQ0NzU3MmY5NzRjN2Y4Zjk4YzYzZTQxNTc0MWM2ZDgx
14
- NTliYjIyYTkxMDY4MzE2MGVkZTdmYThmMmQwYmEwMTUwMjIyYThkMzdlZGU4
15
- ZWZiMzBmYjQyOGRhYWNhNTIwYzAwMjM0NzQ3MzhhY2Y5NmZmMTI=
13
+ NWE4MDdjNTg0ODgyYzAwMmM5MTcxMTFiMDY3ZTAyNjMxNmFlZTJiM2Y0YjRh
14
+ NTM1MWU2YTQ0ZWM0NTYzNmY3NjAxNmQ3ZjBmODliY2M2NmVlMmViMDJmZTFj
15
+ MzFhY2UwY2Y0ODIyNDI2YWMwMWI0YzdkMzI2NWY3ZWQxMjg3M2E=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (1.0.4)
4
+ simple_params (1.0.5)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
@@ -92,6 +92,11 @@ module SimpleParams
92
92
  attribute.send("value")
93
93
  end
94
94
 
95
+ define_method("raw_#{name}") do
96
+ attribute = send("#{name}_attribute")
97
+ attribute.send("raw_value")
98
+ end
99
+
95
100
  define_method("#{name}=") do |val|
96
101
  attribute = send("#{name}_attribute")
97
102
  attribute.send("value=", val)
@@ -24,41 +24,41 @@ module SimpleParams
24
24
  when :integer
25
25
  @subject.send("#{@attribute}=", "100.02")
26
26
  (@subject.send(attribute) == 100) &&
27
- (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:integer]))
27
+ @subject.send("raw_#{attribute}").is_a?(TYPE_MAPPINGS[:integer])
28
28
  when :string
29
29
  @subject.send("#{@attribute}=", 200)
30
30
  (@subject.send(attribute) == "200") &&
31
- (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:string]))
31
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:string])
32
32
  when :decimal
33
33
  @subject.send("#{@attribute}=", "100")
34
34
  (@subject.send(attribute) == 100.0) &&
35
- (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:decimal]))
35
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:decimal])
36
36
  when :datetime
37
37
  @subject.send("#{@attribute}=", DateTime.new(2014,2,3))
38
- @subject.send(attribute).is_a?(TYPE_MAPPINGS[:datetime])
38
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:datetime])
39
39
  when :date
40
- @subject.send("#{@attribute}=", Date.new(2014,2,3))
41
- @subject.send(attribute).is_a?(TYPE_MAPPINGS[:date])
40
+ @subject.send("#{@attribute}=", DateTime.new(2014,2,3,12,0,0))
41
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:date])
42
42
  when :time
43
43
  @subject.send("#{@attribute}=", Time.new(2007,11,5,11,21,0, "-05:00"))
44
- @subject.send(attribute).is_a?(TYPE_MAPPINGS[:time])
44
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:time])
45
45
  when :float
46
46
  @subject.send("#{@attribute}=", "20")
47
- (@subject.send(attribute) == 20.0) &&
48
- (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:float]))
47
+ (@subject.send("raw_#{attribute}") == 20.0) &&
48
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:float])
49
49
  when :boolean
50
50
  @subject.send("#{@attribute}=", 0)
51
51
  (@subject.send(attribute) == false) &&
52
- (@subject.send(attribute).is_a?(TrueClass) || @subject.send(attribute).is_a?(FalseClass))
52
+ (@subject.send("raw_#{attribute}").is_a?(TrueClass) || @subject.send("raw_#{attribute}").is_a?(FalseClass))
53
53
  when :array
54
54
  @subject.send("#{@attribute}=", ["red, green, blue"])
55
- @subject.send(attribute).is_a?(TYPE_MAPPINGS[:array])
55
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:array])
56
56
  when :hash
57
57
  @subject.send("#{@attribute}=", { "dog"=>1, "cat"=>2, "fish"=>3 })
58
- @subject.send(attribute).is_a?(TYPE_MAPPINGS[:hash])
58
+ class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:hash])
59
59
  when :object
60
60
  @subject.send("#{@attribute}=", "programmer")
61
- @subject.send(attribute).is_a?(TYPE_MAPPINGS[:object])
61
+ @subject.send("raw_#{attribute}").is_a?(TYPE_MAPPINGS[:object])
62
62
  else
63
63
  false
64
64
  end
@@ -79,6 +79,9 @@ module SimpleParams
79
79
  end
80
80
 
81
81
  private
82
+ def class_matches(target, comparison)
83
+ target.class.name.to_s == comparison.name.to_s
84
+ end
82
85
  end
83
86
  end
84
87
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.0.5"
2
+ VERSION = "1.0.6"
3
3
  end
@@ -28,7 +28,7 @@ class DummyParams < SimpleParams::Params
28
28
  end
29
29
 
30
30
  nested_array :dogs do
31
- param :name
31
+ param :name, formatter: lambda { |params, name| name.upcase }
32
32
  param :age, type: :integer, validations: { inclusion: { in: 1..20 } }
33
33
  end
34
34
 
@@ -0,0 +1,51 @@
1
+ class ValidatorParams < SimpleParams::Params
2
+ string_param :name
3
+ integer_param :age, optional: true, default: 37
4
+ string_param :first_initial, default: lambda { |params, param| params.name[0] if params.name.present? }
5
+ decimal_param :amount, optional: true, default: 0.10, formatter: lambda { |params, param| param.round(2) }
6
+ param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}, formatter: :lower_case_colors
7
+ param :height, optional: true, validations: { inclusion: { in: ["tall","supertall"]} }
8
+ param :birth_date, type: :date
9
+ param :born_on, type: :datetime
10
+ param :bank_balance, type: :float, formatter: lambda { |params, amt| sprintf('$%.2f', amt) }
11
+ param :weight, type: :decimal
12
+ param :favorite_colors, type: :array
13
+ param :pets, type: :hash
14
+ param :car, type: :object
15
+ param :submitted_at, type: :time
16
+ param :has_cellphone, type: :boolean
17
+ param :title, optional: true, default: "programmer"
18
+ param :account_type, default: "checking", validations: { inclusion: { in: ["checking", "savings"] }}
19
+ param :account_status, default: "active", validations: { inclusion: { in: ["active", "inactive"] }}
20
+ param :username, type: :string, validations: { exclusion: { in: ['admin', 'demo'] } }
21
+
22
+ nested_hash :address do
23
+ string_param :street
24
+ string_param :city, validations: { length: { in: 4..40 } }
25
+ string_param :zip_code, optional: true, validations: { length: { in: 5..9 } }
26
+ param :state, default: "North Carolina", formatter: :transform_state_code
27
+
28
+ def transform_state_code(val)
29
+ val == "SC" ? "South Carolina" : val
30
+ end
31
+ end
32
+
33
+ nested_hash :phone do
34
+ boolean_param :cell_phone, default: true
35
+ string_param :phone_number, validations: { length: { in: 7..10 } }, formatter: lambda { |params, attribute| attribute.gsub(/\D/, "") }
36
+ string_param :area_code, default: lambda { |params, param|
37
+ if params.phone_number.present?
38
+ params.phone_number[0..2]
39
+ end
40
+ }
41
+ end
42
+
43
+ nested_array :dogs do
44
+ param :name
45
+ param :age, type: :integer, validations: { inclusion: { in: 1..20 } }
46
+ end
47
+
48
+ def lower_case_colors(val)
49
+ val.downcase
50
+ end
51
+ end
data/spec/params_spec.rb CHANGED
@@ -74,11 +74,43 @@ describe SimpleParams::Params do
74
74
  params.dogs.first.should be_nil
75
75
  end
76
76
 
77
- it "can access nested arrays as arrays with data", failing: true do
77
+ it "can access nested arrays as arrays with data" do
78
78
  params = DummyParams.new(dogs: [{ name: "Spot", age: 20 }])
79
79
  params.dogs.should respond_to(:first)
80
80
  params.dogs.first.should_not be_nil
81
- params.dogs.first.name.should eq("Spot")
81
+ params.dogs.first.name.should eq("SPOT")
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "accessors", accessors: true do
87
+ let(:params) { DummyParams.new(dogs: [{}]) }
88
+
89
+ it "can access raw values for non-formatted param" do
90
+ params.name = "Tom"
91
+ params.name.should eq("Tom")
92
+ params.raw_name.should eq("Tom")
93
+ end
94
+
95
+ it "can access raw values for formatted param" do
96
+ params.amount = 1.095
97
+ params.amount.should eq(1.10)
98
+ params.raw_amount.should eq(1.095)
99
+ end
100
+
101
+ describe "nested params", nested: true do
102
+ it "can access raw values for formatted param" do
103
+ params.address.state = "SC"
104
+ params.address.state.should eq("South Carolina")
105
+ params.address.raw_state.should eq("SC")
106
+ end
107
+ end
108
+
109
+ describe "nested arrays", nested: true do
110
+ it "can access raw values for formatted param" do
111
+ params.dogs.first.name = "Fido"
112
+ params.dogs.first.name.should eq("FIDO")
113
+ params.dogs.first.raw_name.should eq("Fido")
82
114
  end
83
115
  end
84
116
  end
@@ -1,36 +1,24 @@
1
1
  require 'spec_helper'
2
+ require 'fixtures/validator_params'
2
3
 
3
4
  describe SimpleParams::ValidationMatchers::CoercionMatcher do
4
- class CoercionMatcherTestClass < SimpleParams::Params
5
- param :name, type: :string
6
- param :expiration_date, type: :date
7
- param :amount, type: :integer
8
- param :beginning_date, type: :datetime
9
- param :cost, type: :float
10
- param :weight, type: :decimal
11
- param :colors, type: :array
12
- param :animals, type: :hash
13
- param :title, type: :object
14
- param :office_hours, type: :time
15
- param :cellphone, type: :boolean
16
-
17
-
18
- end
19
-
20
- subject { CoercionMatcherTestClass.new }
5
+ subject { ValidatorParams.new }
21
6
 
22
7
  it { should coerce_param(:name).into(:string) }
23
- it { should coerce_param(:expiration_date).into(:date) }
24
- it { should_not coerce_param(:expiration_date).into(:datetime) }
25
- it { should coerce_param(:amount).into(:integer) }
26
- it { should coerce_param(:beginning_date).into(:datetime) }
27
- it { should coerce_param(:cost).into(:float) }
28
- it { should_not coerce_param(:cost).into(:integer) }
8
+ it { should coerce_param(:birth_date).into(:date) }
9
+ it { should_not coerce_param(:birth_date).into(:datetime) }
10
+ it { should coerce_param(:age).into(:integer) }
11
+ it { should coerce_param(:born_on).into(:datetime) }
12
+ it { should_not coerce_param(:born_on).into(:date) }
13
+ it { should coerce_param(:bank_balance).into(:float) }
14
+ it { should_not coerce_param(:bank_balance).into(:integer) }
29
15
  it { should coerce_param(:weight).into(:decimal) }
30
16
  it { should_not coerce_param(:weight).into(:float) }
31
- it { should coerce_param(:colors).into(:array) }
32
- it { should coerce_param(:animals).into(:hash) }
33
- it { should coerce_param(:title).into(:object) }
34
- it { should coerce_param(:office_hours).into(:time) }
35
- it { should coerce_param(:cellphone).into(:boolean) }
17
+ it { should coerce_param(:favorite_colors).into(:array) }
18
+ it { should coerce_param(:pets).into(:hash) }
19
+ it { should coerce_param(:car).into(:object) }
20
+ it { should coerce_param(:submitted_at).into(:time) }
21
+ it { should_not coerce_param(:submitted_at).into(:date) }
22
+ it { should_not coerce_param(:submitted_at).into(:datetime) }
23
+ it { should coerce_param(:has_cellphone).into(:boolean) }
36
24
  end
@@ -1,16 +1,11 @@
1
1
  require 'spec_helper'
2
+ require 'fixtures/validator_params'
2
3
 
3
4
  describe SimpleParams::ValidationMatchers::FormatMatcher do
4
- class FormatMatcher < SimpleParams::Params
5
- param :amount, type: :float, formatter: lambda { |params, amt| sprintf('%.2f', amt) }
6
- param :expiration_date, type: :date, formatter: lambda { |params, date| date.strftime("%Y-%m")}
7
- param :cost, type: :float, formatter: lambda { |params, amt| sprintf('%.2f', amt) }
8
- end
5
+ subject { ValidatorParams.new }
9
6
 
10
- subject { FormatMatcher.new }
11
-
12
- it { should format(:amount).with_value(10).into("10.00") }
13
- it { should format(:expiration_date).with_value(Date.new(2014, 2, 4)).into("2014-02") }
14
- it { should_not format(:cost).with_value(12).into("14.00") }
15
- it { should_not format(:expiration_date).with_value(Date.new(2016, 2, 4)).into("2014-02") }
7
+ it { should format(:color).with_value("RED").into("red") }
8
+ it { should format(:amount).with_value(1.2345).into(1.23) }
9
+ it { should format(:bank_balance).with_value(1100.4).into("$1100.40") }
10
+ it { should_not format(:birth_date).with_value(Date.new(2014, 1, 1)).into("1/1/2014") }
16
11
  end
@@ -1,17 +1,9 @@
1
1
  require 'spec_helper'
2
+ require 'fixtures/validator_params'
2
3
 
3
4
  describe SimpleParams::ValidationMatchers::NestedArrayMatcher do
4
- class NestedArrayMatcherTestClass < SimpleParams::Params
5
- param :name
6
- param :age, optional: true, default: 37
7
- nested_array :dogs do
8
- param :name
9
- param :age, type: :integer
10
- end
11
- end
12
-
13
- subject { NestedArrayMatcherTestClass.new }
5
+ subject { ValidatorParams.new }
14
6
 
15
7
  it { should have_nested_array(:dogs) }
16
- it { should_not have_nested_array(:broken) }
8
+ it { should_not have_nested_array(:address) }
17
9
  end
@@ -1,26 +1,10 @@
1
1
  require 'spec_helper'
2
+ require 'fixtures/validator_params'
2
3
 
3
4
  describe SimpleParams::ValidationMatchers::NestedParameterMatcher do
4
- class NestedParameterMatcherTestClass < SimpleParams::Params
5
- param :name
6
- param :age, optional: true, default: 37
7
- param :title, optional: true, default: "programmer"
8
- param :account_type, default: "checking", validations: { inclusion: { in: ["checking", "savings"] }}
9
- param :account_status, optional: true, validations: { inclusion: { in: ["active", "inactive"] }}
10
- nested_param :billing_address do
11
- param :first_name
12
- param :last_name
13
- param :company, optional: true
14
- param :street
15
- param :city
16
- param :state
17
- param :zip_code
18
- param :country
19
- end
20
- end
5
+ subject { ValidatorParams.new }
21
6
 
22
- subject { NestedParameterMatcherTestClass.new }
23
-
24
- it { should have_nested_parameter(:billing_address) }
25
- it { should_not have_nested_parameter(:broken) }
7
+ it { should have_nested_parameter(:address) }
8
+ it { should have_nested_parameter(:phone) }
9
+ it { should_not have_nested_parameter(:dogs) }
26
10
  end
@@ -1,16 +1,8 @@
1
1
  require 'spec_helper'
2
+ require 'fixtures/validator_params'
2
3
 
3
4
  describe SimpleParams::ValidationMatchers::OptionalParameterMatcher do
4
- class OptionalParameterMatcher < SimpleParams::Params
5
- param :name
6
- param :age, optional: true, default: 37
7
- param :title, optional: true, default: "programmer"
8
- param :account_type, default: "checking", validations: { inclusion: { in: ["checking", "savings"] }}
9
- param :account_status, optional: true, validations: { inclusion: { in: ["active", "inactive"] }}
10
- param :username, type: :string, default: "test", validations: { exclusion: { in: ['admin', 'demo'] } }
11
- end
12
-
13
- subject { OptionalParameterMatcher.new }
5
+ subject { ValidatorParams.new }
14
6
 
15
7
  it { should_not have_optional_parameter(:name) }
16
8
  it { should have_optional_parameter(:age).with_default(37) }
@@ -18,6 +10,5 @@ describe SimpleParams::ValidationMatchers::OptionalParameterMatcher do
18
10
  it { should have_optional_parameter(:account_status).with_allowed_values("active", "inactive") }
19
11
  it { should have_optional_parameter(:account_type).with_default("checking").with_allowed_values("checking", "savings") }
20
12
  it { should have_optional_parameter(:account_type).with_disallowed_values("admin", "demo") }
21
- it { should have_optional_parameter(:username).with_default("test").with_allowed_values("myuser", "kitten") }
22
- it { should have_optional_parameter(:username).with_disallowed_values("admin", "demo") }
13
+ it { should_not have_optional_parameter(:username) }
23
14
  end
@@ -1,22 +1,14 @@
1
1
  require 'spec_helper'
2
+ require 'fixtures/validator_params'
2
3
 
3
4
  describe SimpleParams::ValidationMatchers::RequiredParameterMatcher do
4
- class RequiredParameterMatcherTestClass < SimpleParams::Params
5
- param :name
6
- param :age, optional: true
7
- param :title, default: "programmer"
8
- param :account_type, validations: { inclusion: { in: ["checking", "savings"] }}
9
- param :account_status, default: "active", validations: { inclusion: { in: ["active", "inactive"] }}
10
- param :username, type: :string, validations: { exclusion: { in: ['admin', 'demo'] } }
11
- end
5
+ subject { ValidatorParams.new }
12
6
 
13
- subject { RequiredParameterMatcherTestClass.new }
14
-
15
7
  it { should have_required_parameter(:name) }
16
8
  it { should_not have_required_parameter(:age) }
17
9
  it { should_not have_required_parameter(:name).with_default("Matthew") }
18
10
  it { should have_required_parameter(:title).with_default("programmer") }
19
- it { should have_required_parameter(:account_type).with_allowed_values("checking", "savings") }
11
+ it { should have_required_parameter(:account_type).with_default("checking").with_allowed_values("checking", "savings") }
20
12
  it { should have_required_parameter(:account_status).with_default("active").with_allowed_values("active", "inactive") }
21
13
  it { should have_required_parameter(:account_status).with_disallowed_values("admin", "demo") }
22
14
  it { should have_required_parameter(:username).with_allowed_values("myuser", "kitten") }
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: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-07 00:00:00.000000000 Z
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -157,6 +157,7 @@ files:
157
157
  - spec/attribute_spec.rb
158
158
  - spec/errors_spec.rb
159
159
  - spec/fixtures/dummy_params.rb
160
+ - spec/fixtures/validator_params.rb
160
161
  - spec/formatter_spec.rb
161
162
  - spec/multiple_classes_spec.rb
162
163
  - spec/params_spec.rb
@@ -201,6 +202,7 @@ test_files:
201
202
  - spec/attribute_spec.rb
202
203
  - spec/errors_spec.rb
203
204
  - spec/fixtures/dummy_params.rb
205
+ - spec/fixtures/validator_params.rb
204
206
  - spec/formatter_spec.rb
205
207
  - spec/multiple_classes_spec.rb
206
208
  - spec/params_spec.rb