simple_params 0.0.2.pre9 → 0.0.2.pre10

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
- NDY4OWUwYTdkOTc5NmYwYjZlZjdjMmY3YzU4MWRkYWQ5ZGZlZWRhNQ==
4
+ NDJmMjM3ZGEyNWMwYTFhMzYwODRkNDI2ZGU1ZjRhZGFlZjUzNmVlOA==
5
5
  data.tar.gz: !binary |-
6
- MDI5OGEzYjEzODU3ODJmNDMxMmExYzc1NGZlOGEwZmYyODUxNjg2Mw==
6
+ Y2Y2Y2I2ZWJlZTI1NTIwZTRkYTE0N2E5YmQ0NDkxNzk3MjRlNWUyOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTY3YTQzMTY2YzRlYTE0ZGRlM2FkNDJmNDdlYzI2Zjc5Mjg0NzQ5OWRlODE0
10
- NTAzM2NlYWE0NDFiZWYyY2M5NGUyNmYxOWI4ZDhhN2VmOTAxNzdkNTQ0MTYw
11
- NjNlNzE3YmFlNmM3NDkwMTQ1ZmFhZDQxMGRmM2ZjOTgxMTBhMTc=
9
+ Mjk3ZWYzZGZiMTVhMjJhYzM5Y2NhYjhiMGRiOTljNTM2ODBhYjI3YTVhMWFi
10
+ MTE0NDY2MWQ2NjJlODA3M2JjMjJkMjYwZjhiNDE2MGMzYmJiN2RjMzIyOTky
11
+ ODYxNTY2NjY4NTk1MDEwNjkxZDdhNWQ5N2UxMDQ1ZjU0NGNlZTk=
12
12
  data.tar.gz: !binary |-
13
- MTBlZTQ0YTQ4NjNhODg1ZTE3NjJkY2E2ODAyOTRjZjQ5ZDE5NWI3ZjIxM2Nh
14
- Mjg0MjdkNzI1MzRmZGRhNzhmYjZiNTk3YTZiOWUwNzg0N2NhMDQ2OWMxMjdi
15
- NjU5NDg0YTU4NDQ3OGE1YjYxNGJmMGExYjkyNTdmNmUxMjAwYzA=
13
+ N2NiYTA1ZTg1NTNhYWU3NzE1NzNkZDZhYzlhYmVjYzQ3NzlmMmU0ODcxZjRj
14
+ MWU3M2MwMzIwZTE5NWQ0ODMwMzU2ZWQ4MGU2Mzg3ZmZjNTA2MjNjNWMyYmQ5
15
+ ZjJlZDdiYWJjMmQ1OTMyNjIyNzkyODgyZjM1NWRlNjgyZTA2Zjg=
data/Gemfile.lock CHANGED
@@ -1,17 +1,18 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (0.0.2.pre8)
4
+ simple_params (0.0.2.pre9)
5
5
  activemodel (>= 3.0, < 5.0)
6
+ shoulda-matchers (~> 2.8)
6
7
  virtus (>= 1.0.0)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
11
- activemodel (4.2.0)
12
- activesupport (= 4.2.0)
12
+ activemodel (4.2.1)
13
+ activesupport (= 4.2.1)
13
14
  builder (~> 3.1)
14
- activesupport (4.2.0)
15
+ activesupport (4.2.1)
15
16
  i18n (~> 0.7)
16
17
  json (~> 1.7, >= 1.7.7)
17
18
  minitest (~> 5.1)
@@ -28,12 +29,12 @@ GEM
28
29
  descendants_tracker (0.0.4)
29
30
  thread_safe (~> 0.3, >= 0.3.1)
30
31
  diff-lcs (1.2.5)
31
- equalizer (0.0.9)
32
+ equalizer (0.0.11)
32
33
  i18n (0.7.0)
33
34
  ice_nine (0.11.1)
34
35
  json (1.8.2)
35
36
  method_source (0.8.2)
36
- minitest (5.5.1)
37
+ minitest (5.6.0)
37
38
  pry (0.10.1)
38
39
  coderay (~> 1.1.0)
39
40
  method_source (~> 0.8.1)
@@ -47,11 +48,13 @@ GEM
47
48
  rspec-expectations (2.99.2)
48
49
  diff-lcs (>= 1.1.3, < 2.0)
49
50
  rspec-mocks (2.99.3)
51
+ shoulda-matchers (2.8.0)
52
+ activesupport (>= 3.0.0)
50
53
  slop (3.6.0)
51
54
  thread_safe (0.3.5)
52
55
  tzinfo (1.2.2)
53
56
  thread_safe (~> 0.1)
54
- virtus (1.0.4)
57
+ virtus (1.0.5)
55
58
  axiom-types (~> 0.1)
56
59
  coercible (~> 1.0)
57
60
  descendants_tracker (~> 0.0, >= 0.0.3)
data/README.md CHANGED
@@ -227,6 +227,17 @@ class CreateObjectParams < SimpleParams::Params
227
227
  end
228
228
  ```
229
229
 
230
+ # RSpec Validation Matchers
231
+
232
+ If you would like to use Simple Params built in matchers, make sure you have RSpec
233
+ installed. Once RSpec is installed add this to your spec_helper.rb file
234
+
235
+ ```ruby
236
+ RSpec.configure do |config|
237
+ config.include(SimpleParams::ValidationMatchers)
238
+ end
239
+ ```
240
+
230
241
  ## Contributing
231
242
 
232
243
  1. Fork it
@@ -3,20 +3,6 @@ require "virtus"
3
3
 
4
4
  module SimpleParams
5
5
  class Attribute
6
- TYPE_MAPPINGS = {
7
- integer: Integer,
8
- string: String,
9
- decimal: BigDecimal,
10
- datetime: DateTime,
11
- date: Date,
12
- time: Time,
13
- float: Float,
14
- boolean: Axiom::Types::Boolean, # See note on Virtus
15
- array: Array,
16
- hash: Hash,
17
- object: Object
18
- }
19
-
20
6
  attr_reader :parent
21
7
  attr_reader :name
22
8
 
@@ -84,7 +84,7 @@ module SimpleParams
84
84
 
85
85
  def add_validations(name, opts = {})
86
86
  validations = opts[:validations] || {}
87
- validations.merge!(presence: true) unless opts[:optional]
87
+ opts[:optional] ? validations.merge!(presence: false, allow_nil: true) : validations.merge!(presence: true)
88
88
  validates name, validations unless validations.empty?
89
89
  end
90
90
 
@@ -0,0 +1,15 @@
1
+ module SimpleParams
2
+ TYPE_MAPPINGS = {
3
+ integer: Integer,
4
+ string: String,
5
+ decimal: BigDecimal,
6
+ datetime: DateTime,
7
+ date: Date,
8
+ time: Time,
9
+ float: Float,
10
+ boolean: Axiom::Types::Boolean, # See note on Virtus
11
+ array: Array,
12
+ hash: Hash,
13
+ object: Object
14
+ }
15
+ end
@@ -0,0 +1,84 @@
1
+ module SimpleParams
2
+ module ValidationMatchers
3
+ def coerce_param(attr)
4
+ CoercionMatcher.new(attr)
5
+ end
6
+
7
+ class CoercionMatcher < ValidationMatcher
8
+ attr_accessor :attribute
9
+
10
+ def initialize(attribute)
11
+ super(attribute)
12
+ @attribute = attribute
13
+ end
14
+
15
+ def into(value)
16
+ @expected_coerce = value
17
+ self
18
+ end
19
+
20
+ def matches?(subject)
21
+ super(subject)
22
+
23
+ case @expected_coerce
24
+ when :integer
25
+ @subject.send("#{@attribute}=", "100.02")
26
+ (@subject.send(attribute) == 100) &&
27
+ (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:integer]))
28
+ when :string
29
+ @subject.send("#{@attribute}=", 200)
30
+ (@subject.send(attribute) == "200") &&
31
+ (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:string]))
32
+ when :decimal
33
+ @subject.send("#{@attribute}=", "100")
34
+ (@subject.send(attribute) == 100.0) &&
35
+ (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:decimal]))
36
+ when :datetime
37
+ @subject.send("#{@attribute}=", DateTime.new(2014,2,3))
38
+ @subject.send(attribute).is_a?(TYPE_MAPPINGS[:datetime])
39
+ when :date
40
+ @subject.send("#{@attribute}=", Date.new(2014,2,3))
41
+ @subject.send(attribute).is_a?(TYPE_MAPPINGS[:date])
42
+ when :time
43
+ @subject.send("#{@attribute}=", Time.new(2007,11,5,11,21,0, "-05:00"))
44
+ @subject.send(attribute).is_a?(TYPE_MAPPINGS[:time])
45
+ when :float
46
+ @subject.send("#{@attribute}=", "20")
47
+ (@subject.send(attribute) == 20.0) &&
48
+ (@subject.send(attribute).is_a?(TYPE_MAPPINGS[:float]))
49
+ when :boolean
50
+ @subject.send("#{@attribute}=", 0)
51
+ (@subject.send(attribute) == false) &&
52
+ (@subject.send(attribute).is_a?(TrueClass) || @subject.send(attribute).is_a?(FalseClass))
53
+ when :array
54
+ @subject.send("#{@attribute}=", ["red, green, blue"])
55
+ @subject.send(attribute).is_a?(TYPE_MAPPINGS[:array])
56
+ when :hash
57
+ @subject.send("#{@attribute}=", { "dog"=>1, "cat"=>2, "fish"=>3 })
58
+ @subject.send(attribute).is_a?(TYPE_MAPPINGS[:hash])
59
+ when :object
60
+ @subject.send("#{@attribute}=", "programmer")
61
+ @subject.send(attribute).is_a?(TYPE_MAPPINGS[:object])
62
+ else
63
+ false
64
+ end
65
+
66
+
67
+ end
68
+
69
+ def description
70
+ "Expect #{@attribute} to coerce into #{@expected_coerce}"
71
+ end
72
+
73
+ def failure_message_for_should
74
+ "Expect #{@attribute} to coerce into #{@expected_coerce}"
75
+ end
76
+
77
+ def failure_message_for_should_not
78
+ "Expect #{@attribute} to not coerce into #{@expected_coerce}"
79
+ end
80
+
81
+ private
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,46 @@
1
+ module SimpleParams
2
+ module ValidationMatchers
3
+ def format(attr)
4
+ FormatMatcher.new(attr)
5
+ end
6
+
7
+ class FormatMatcher < ValidationMatcher
8
+ attr_accessor :default_value, :attribute
9
+
10
+ def initialize(attribute)
11
+ super(attribute)
12
+ @unformatted_value = nil
13
+ end
14
+
15
+ def with_value(value)
16
+ @unformatted_value = value
17
+ self
18
+ end
19
+
20
+ def into(value)
21
+ @expected_value = value
22
+ self
23
+ end
24
+
25
+ def matches?(subject)
26
+ super(subject)
27
+ @subject.send("#{@attribute}=", @unformatted_value)
28
+ @subject.send(attribute) == @expected_value
29
+ end
30
+
31
+ def description
32
+ "Expect #{@attribute} with_value #{@unformatted_value} to format into #{@expected_value}"
33
+ end
34
+
35
+ def failure_message_for_should
36
+ "Expect #{@attribute} with_value #{@unformatted_value} to format into #{@expected_value}"
37
+ end
38
+
39
+ def failure_message_for_should_not
40
+ "Expected #{@attribute} with_value #{@unformatted_value} to not format into #{@expected_value}"
41
+ end
42
+
43
+ private
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,65 @@
1
+ module SimpleParams
2
+ module ValidationMatchers
3
+ def have_optional_parameter(attr)
4
+ OptionalParameterMatcher.new(attr)
5
+ end
6
+
7
+ class OptionalParameterMatcher < ValidationMatcher
8
+ attr_accessor :default_value, :attribute, :allowed_values
9
+
10
+ def initialize(attribute)
11
+ super(attribute)
12
+ @default_value = nil
13
+ @attribute = attribute
14
+ @allowed_values = nil
15
+ end
16
+
17
+ def with_default(value)
18
+ @default_value = value
19
+ self
20
+ end
21
+
22
+ def with_allowed_values(*values)
23
+ @allowed_values = values
24
+ self
25
+ end
26
+
27
+ def matches?(subject)
28
+ super(subject)
29
+
30
+ if @default_value
31
+ matches_default_value?
32
+ elsif @allowed_values
33
+ allows_value_of(nil) && matches_allowed_values?
34
+ else
35
+ allows_value_of(nil)
36
+ end
37
+
38
+ end
39
+
40
+ def description
41
+ "allow #{@attribute} to be nil"
42
+ end
43
+
44
+ def failure_message_for_should
45
+ "Expected #{@default_value} either to be nil or one of #{@allowed_values}"
46
+ end
47
+
48
+ def failure_message_for_should_not
49
+ "Expected #{@default_value} not to be nil or to be one of #{@allowed_values}"
50
+ end
51
+
52
+ private
53
+
54
+ def matches_default_value?
55
+ @subject.send(@attribute) == @default_value
56
+ end
57
+
58
+ def matches_allowed_values?
59
+ allowed_values.all? do |value|
60
+ allows_value_of(value)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,65 @@
1
+ module SimpleParams
2
+ module ValidationMatchers
3
+ def have_required_parameter(attr)
4
+ RequiredParameterMatcher.new(attr)
5
+ end
6
+
7
+ class RequiredParameterMatcher < ValidationMatcher
8
+ attr_accessor :default_value, :attribute, :allowed_values
9
+
10
+ def initialize(attribute)
11
+ super(attribute)
12
+ @default_value = nil
13
+ @attribute = attribute
14
+ @allowed_values = nil
15
+ end
16
+
17
+ def with_default(value)
18
+ @default_value = value
19
+ self
20
+ end
21
+
22
+ def with_allowed_values(*values)
23
+ @allowed_values = values
24
+ self
25
+ end
26
+
27
+ def matches?(subject)
28
+ super(subject)
29
+ @expected_message ||= :blank
30
+
31
+ if @default_value
32
+ matches_default_value?
33
+ elsif @allowed_values
34
+ disallows_value_of(nil) && matches_allowed_values?
35
+ else
36
+ disallows_value_of(nil, @expected_message)
37
+ end
38
+ end
39
+
40
+ def description
41
+ "require #{@attribute} to be set"
42
+ end
43
+
44
+ def failure_message_for_should
45
+ "Expected #{@default_value} to be set and to be one of #{@allowed_values}"
46
+ end
47
+
48
+ def failure_message_for_should_not
49
+ "Expected #{@default_value} not to be set and not to be one of #{@allowed_values}"
50
+ end
51
+
52
+ private
53
+
54
+ def matches_default_value?
55
+ @subject.send(@attribute) == @default_value
56
+ end
57
+
58
+ def matches_allowed_values?
59
+ allowed_values.all? do |value|
60
+ allows_value_of(value)
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,111 @@
1
+ require "shoulda-matchers"
2
+
3
+ module SimpleParams
4
+ module ValidationMatchers
5
+ class ValidationMatcher < ::Shoulda::Matchers::ActiveModel::ValidationMatcher
6
+ end
7
+ end
8
+ end
9
+
10
+
11
+ # module Shoulda
12
+ # module Matchers
13
+ # module ActiveModel
14
+ # # @private
15
+ # class ValidationMatcher
16
+ # attr_reader :failure_message
17
+
18
+ # alias failure_message_for_should failure_message
19
+
20
+ # def initialize(attribute)
21
+ # @attribute = attribute
22
+ # @strict = false
23
+ # @failure_message = nil
24
+ # @failure_message_when_negated = nil
25
+ # end
26
+
27
+ # def on(context)
28
+ # @context = context
29
+ # self
30
+ # end
31
+
32
+ # def strict
33
+ # @strict = true
34
+ # self
35
+ # end
36
+
37
+ # def failure_message_when_negated
38
+ # @failure_message_when_negated || @failure_message
39
+ # end
40
+ # alias failure_message_for_should_not failure_message_when_negated
41
+
42
+ # def matches?(subject)
43
+ # @subject = subject
44
+ # false
45
+ # end
46
+
47
+ # private
48
+
49
+ # def allows_value_of(value, message = nil, &block)
50
+ # allow = allow_value_matcher(value, message)
51
+ # yield allow if block_given?
52
+
53
+ # if allow.matches?(@subject)
54
+ # @failure_message_when_negated = allow.failure_message_when_negated
55
+ # true
56
+ # else
57
+ # @failure_message = allow.failure_message
58
+ # false
59
+ # end
60
+ # end
61
+
62
+ # def disallows_value_of(value, message = nil, &block)
63
+ # disallow = disallow_value_matcher(value, message)
64
+ # yield disallow if block_given?
65
+
66
+ # if disallow.matches?(@subject)
67
+ # @failure_message_when_negated = disallow.failure_message_when_negated
68
+ # true
69
+ # else
70
+ # @failure_message = disallow.failure_message
71
+ # false
72
+ # end
73
+ # end
74
+
75
+ # def allow_value_matcher(value, message)
76
+ # matcher = AllowValueMatcher.new(value).for(@attribute).
77
+ # with_message(message)
78
+
79
+ # if defined?(@context)
80
+ # matcher.on(@context)
81
+ # end
82
+
83
+ # if strict?
84
+ # matcher.strict
85
+ # end
86
+
87
+ # matcher
88
+ # end
89
+
90
+ # def disallow_value_matcher(value, message)
91
+ # matcher = DisallowValueMatcher.new(value).for(@attribute).
92
+ # with_message(message)
93
+
94
+ # if defined?(@context)
95
+ # matcher.on(@context)
96
+ # end
97
+
98
+ # if strict?
99
+ # matcher.strict
100
+ # end
101
+
102
+ # matcher
103
+ # end
104
+
105
+ # def strict?
106
+ # @strict
107
+ # end
108
+ # end
109
+ # end
110
+ # end
111
+ # end
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "0.0.2.pre9"
2
+ VERSION = "0.0.2.pre10"
3
3
  end
data/lib/simple_params.rb CHANGED
@@ -5,7 +5,13 @@ require 'simple_params/errors'
5
5
  require 'simple_params/validations'
6
6
  require 'simple_params/params'
7
7
  require 'simple_params/simple_params_error'
8
+ require 'simple_params/type_mappings'
8
9
  require 'simple_params/api_pie_doc'
9
10
  require 'simple_params/api_pie_doc/attribute_base'
10
11
  require 'simple_params/api_pie_doc/attribute'
11
12
  require 'simple_params/api_pie_doc/nested_attribute'
13
+ require 'simple_params/validation_matchers/validation_matcher'
14
+ require 'simple_params/validation_matchers/coercion_matcher'
15
+ require 'simple_params/validation_matchers/format_matcher'
16
+ require 'simple_params/validation_matchers/required_parameter_matcher'
17
+ require 'simple_params/validation_matchers/optional_parameter_matcher'
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "activemodel", ">= 3.0", "< 5.0"
22
22
  spec.add_dependency "virtus", ">= 1.0.0"
23
+ spec.add_dependency "shoulda-matchers", "~> 2.8"
23
24
  spec.add_development_dependency "bundler", "~> 1.5"
24
25
  spec.add_development_dependency "rake", "~> 10.1"
25
26
  spec.add_development_dependency "rspec", "~> 2.6"
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  class AcceptanceParams < SimpleParams::Params
4
4
  param :reference, type: :object, optional: true
5
5
  param :name
6
- param :age, type: :integer, optional: true
6
+ param :age, type: :integer, optional: true, validations: { inclusion: { in: 18..100 } }
7
7
  param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}
8
8
 
9
9
  nested_hash :address do
@@ -164,7 +164,7 @@ describe SimpleParams::Params do
164
164
  describe "#validate!" do
165
165
  let(:params) { AcceptanceParams.new }
166
166
 
167
- it "raises error with valdiation descriptions" do
167
+ it "raises error with validation descriptions" do
168
168
  expect { params.validate! }.to raise_error(SimpleParamsError,
169
169
  "{:name=>[\"can't be blank\"], :address=>{:street=>[\"can't be blank\"], :city=>[\"is too short (minimum is 4 characters)\", \"can't be blank\"]}}"
170
170
  )
@@ -4,6 +4,7 @@ class DummyParams < SimpleParams::Params
4
4
  string_param :first_initial, default: lambda { |params, param| params.name[0] if params.name.present? }
5
5
  decimal_param :amount, optional: true, default: 0.10, formatter: lambda { |params, param| param.round(2) }
6
6
  param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}, formatter: :lower_case_colors
7
+ string_param :height, optional: true, validations: { inclusion: { in: ["tall","supertall"]} }
7
8
 
8
9
  nested_hash :address do
9
10
  string_param :street
data/spec/params_spec.rb CHANGED
@@ -72,7 +72,7 @@ describe SimpleParams::Params do
72
72
  describe "attributes", attributes: true do
73
73
  it "returns array of attribute symbols" do
74
74
  params = DummyParams.new
75
- params.attributes.should eq([:name, :age, :first_initial, :amount, :color, :address, :phone])
75
+ params.attributes.should eq([:name, :age, :first_initial, :amount, :color, :height, :address, :phone])
76
76
  end
77
77
  end
78
78
 
@@ -123,6 +123,23 @@ describe SimpleParams::Params do
123
123
  end
124
124
  end
125
125
 
126
+ describe "optional params with inclusion" do
127
+ let(:params) do
128
+ DummyParams.new(
129
+ name: "Bill",
130
+ age: 30,
131
+ address: {
132
+ city: "Greenville"
133
+ }
134
+ )
135
+ end
136
+
137
+ it "allows optional params to be nil with inclusion" do
138
+ params.should_not be_valid
139
+ params.errors[:height].should be_empty
140
+ end
141
+ end
142
+
126
143
  describe "coercion", coercion: true do
127
144
  it "coerces values on initialization" do
128
145
  params = DummyParams.new(age: "42")
@@ -297,6 +314,7 @@ describe SimpleParams::Params do
297
314
  param :first_initial, String, desc: '', required: true
298
315
  param :amount, desc: '', required: false
299
316
  param :color, String, desc: '', required: true
317
+ param :height, String, desc: '', required: false
300
318
  param :address, Hash, desc: '', required: true do
301
319
  param :street, String, desc: '', required: true
302
320
  param :city, String, desc: '', required: true
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
  require 'simple_params'
4
+ require 'shoulda/matchers'
4
5
  require 'pry'
5
6
 
6
7
  Dir[File.join('.', 'spec', 'support', '**', '*.rb')].each {|f| require f}
@@ -9,4 +10,5 @@ I18n.config.enforce_available_locales = true
9
10
 
10
11
  RSpec.configure do |config|
11
12
  config.mock_with :rspec
13
+ config.include(SimpleParams::ValidationMatchers)
12
14
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ 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 }
21
+
22
+ 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) }
29
+ it { should coerce_param(:weight).into(:decimal) }
30
+ 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) }
36
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ 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
9
+
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") }
16
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ 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
+ end
11
+
12
+ subject { OptionalParameterMatcher.new }
13
+
14
+ it { should_not have_optional_parameter(:name) }
15
+ it { should have_optional_parameter(:age).with_default(37) }
16
+ it { should have_optional_parameter(:title).with_default("programmer") }
17
+ it { should have_optional_parameter(:account_status).with_allowed_values("active", "inactive") }
18
+ it { should have_optional_parameter(:account_type).with_default("checking").with_allowed_values("checking", "savings") }
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ 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
+ end
11
+
12
+ subject { RequiredParameterMatcherTestClass.new }
13
+
14
+ it { should have_required_parameter(:name) }
15
+ it { should_not have_required_parameter(:age) }
16
+ it { should_not have_required_parameter(:name).with_default("Matthew") }
17
+ it { should have_required_parameter(:title).with_default("programmer") }
18
+ it { should have_required_parameter(:account_type).with_allowed_values("checking", "savings") }
19
+ it { should have_required_parameter(:account_status).with_default("active").with_allowed_values("active", "inactive") }
20
+ end
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.2.pre9
4
+ version: 0.0.2.pre10
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-08 00:00:00.000000000 Z
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -44,6 +44,20 @@ dependencies:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: 1.0.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoulda-matchers
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.8'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '2.8'
47
61
  - !ruby/object:Gem::Dependency
48
62
  name: bundler
49
63
  requirement: !ruby/object:Gem::Requirement
@@ -123,6 +137,12 @@ files:
123
137
  - lib/simple_params/formatter.rb
124
138
  - lib/simple_params/params.rb
125
139
  - lib/simple_params/simple_params_error.rb
140
+ - lib/simple_params/type_mappings.rb
141
+ - lib/simple_params/validation_matchers/coercion_matcher.rb
142
+ - lib/simple_params/validation_matchers/format_matcher.rb
143
+ - lib/simple_params/validation_matchers/optional_parameter_matcher.rb
144
+ - lib/simple_params/validation_matchers/required_parameter_matcher.rb
145
+ - lib/simple_params/validation_matchers/validation_matcher.rb
126
146
  - lib/simple_params/validations.rb
127
147
  - lib/simple_params/version.rb
128
148
  - simple_params.gemspec
@@ -137,6 +157,10 @@ files:
137
157
  - spec/params_spec.rb
138
158
  - spec/spec_helper.rb
139
159
  - spec/support/shared_examples/base_attribute.rb
160
+ - spec/validation_matchers/coercion_matcher_spec.rb
161
+ - spec/validation_matchers/format_matcher_spec.rb
162
+ - spec/validation_matchers/optional_parameter_matcher_spec.rb
163
+ - spec/validation_matchers/required_parameter_matcher_spec.rb
140
164
  homepage: https://github.com/brycesenz/simple_params
141
165
  licenses:
142
166
  - MIT
@@ -173,3 +197,7 @@ test_files:
173
197
  - spec/params_spec.rb
174
198
  - spec/spec_helper.rb
175
199
  - spec/support/shared_examples/base_attribute.rb
200
+ - spec/validation_matchers/coercion_matcher_spec.rb
201
+ - spec/validation_matchers/format_matcher_spec.rb
202
+ - spec/validation_matchers/optional_parameter_matcher_spec.rb
203
+ - spec/validation_matchers/required_parameter_matcher_spec.rb