simple_params 1.0.6 → 1.1.0

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
- MDJmOTE0MzM0NTQzZmNiMTkzMjFiNjc3ZTdlNzc3MjhkNWViODQ1Ng==
4
+ MzZjOTgzY2VmMTVhNjE3MGU5NDA2NjFkMDZlYzQwN2JjMDg3NGRlZA==
5
5
  data.tar.gz: !binary |-
6
- OGExMjA2YTNlMDhkYzM3MjE5YjQ1OWJmNWU5NGZkYzE5ZWUyY2E1OA==
6
+ YjUzM2UzNzM1OGVkZGNhM2UzNWQ3M2I1OGViMjAzOWQyMjAwY2U1Mg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjgxYzY5N2YyYjZmMjdmOWJhN2Q5OTNjNDY0YzBmM2VhZWNlNjc1ZGY0NzRk
10
- ODE5OTJhMTQ1ZmI2MTRiZWI3YjBmODZlMGVhYWVhODM5NGYzY2M4MWY1ODBm
11
- NmMxODBhYWQ3M2RiMzQzMzViYWFjZmUxOGQ4NmIwN2Q5Nzk0NDc=
9
+ ODMwNzg2OThlODdmMDRiZGE0YmY5NTY0M2NkZmVjYzRlMzFiYTljODY1ZDNk
10
+ ZmJkYzRjZDQzYTNjOGM3ODRmNTgxMjI1YTUyYmQwYjIzMDFiYjRmNWNhZGU2
11
+ YjNhYmYxOTFhOTk2NDBmM2Q4YTVlMTM4MzE3OTEwMjc2ZDRiNzQ=
12
12
  data.tar.gz: !binary |-
13
- NWE4MDdjNTg0ODgyYzAwMmM5MTcxMTFiMDY3ZTAyNjMxNmFlZTJiM2Y0YjRh
14
- NTM1MWU2YTQ0ZWM0NTYzNmY3NjAxNmQ3ZjBmODliY2M2NmVlMmViMDJmZTFj
15
- MzFhY2UwY2Y0ODIyNDI2YWMwMWI0YzdkMzI2NWY3ZWQxMjg3M2E=
13
+ ZmNhYjM2ZTNlZDA4ZTc2ZmNlN2ZjZDk4MzBiOTA3NTI4ZDIzOTZkMTJlZjY3
14
+ ZDE5YmMzYmI4ZjRhNjcyNTBhYWYyYWNiY2E1Y2IyOWQyODRkZTFiMDdmMjVi
15
+ NjVjMjA4ZTc0YTIxNTM0ZDNhN2MxODFlYjU3YTNlNDhmYjY4NDg=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (1.0.5)
4
+ simple_params (1.0.6)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  shoulda-matchers (~> 2.8)
7
7
  virtus (>= 1.0.0)
@@ -25,11 +25,13 @@ module SimpleParams
25
25
  def type_description
26
26
  value = options[:type]
27
27
  case value
28
- when :string, :integer, :array, :hash, :object
28
+ when :string, :integer, :array, :hash, :object, :date
29
29
  "#{value.to_s.capitalize.constantize}"
30
- when 'String', 'Integer', 'Array', 'Hash'
30
+ when 'String', 'Integer', 'Array', 'Hash', 'Date'
31
31
  "#{value}"
32
- when :decimal, :datetime, :date, :time, :float, :boolean
32
+ when :boolean
33
+ 'Boolean'
34
+ when :decimal, :datetime, :time, :float
33
35
  ""
34
36
  else
35
37
  raise NotValidValueError.new("Must be one of #{SimpleParams::Params::TYPES}")
@@ -101,6 +101,12 @@ module SimpleParams
101
101
  attribute = send("#{name}_attribute")
102
102
  attribute.send("value=", val)
103
103
  end
104
+
105
+ if opts[:type].to_sym == :date
106
+ define_date_helper_methods(name)
107
+ elsif [:time, :datetime].include?(opts[:type].to_sym)
108
+ define_datetime_helper_methods(name)
109
+ end
104
110
  end
105
111
 
106
112
  def add_validations(name, opts = {})
@@ -130,6 +136,64 @@ module SimpleParams
130
136
  klass.class_eval("self.options = #{options}")
131
137
  end
132
138
  end
139
+
140
+ def define_date_helper_methods(name)
141
+ define_method("#{name}(3i)=") do |day|
142
+ attribute = send("#{name}_attribute")
143
+ value = attribute.send("value") || Date.today
144
+ attribute.send("value=", Date.new(value.year, value.month, day.to_i))
145
+ end
146
+
147
+ define_method("#{name}(2i)=") do |month|
148
+ attribute = send("#{name}_attribute")
149
+ value = attribute.send("value") || Date.today
150
+ attribute.send("value=", Date.new(value.year, month.to_i, value.day))
151
+ end
152
+
153
+ define_method("#{name}(1i)=") do |year|
154
+ attribute = send("#{name}_attribute")
155
+ value = attribute.send("value") || Date.today
156
+ attribute.send("value=", Date.new(year.to_i, value.month, value.day))
157
+ end
158
+ end
159
+
160
+ def define_datetime_helper_methods(name)
161
+ define_method("#{name}(6i)=") do |sec|
162
+ attribute = send("#{name}_attribute")
163
+ value = attribute.send("value") || Time.now.utc
164
+ attribute.send("value=", Time.new(value.year, value.month, value.day, value.hour, value.min, sec.to_i, value.utc_offset))
165
+ end
166
+
167
+ define_method("#{name}(5i)=") do |minute|
168
+ attribute = send("#{name}_attribute")
169
+ value = attribute.send("value") || Time.now.utc
170
+ attribute.send("value=", Time.new(value.year, value.month, value.day, value.hour, minute.to_i, value.sec, value.utc_offset))
171
+ end
172
+
173
+ define_method("#{name}(4i)=") do |hour|
174
+ attribute = send("#{name}_attribute")
175
+ value = attribute.send("value") || Time.now.utc
176
+ attribute.send("value=", Time.new(value.year, value.month, value.day, hour.to_i, value.min, value.sec, value.utc_offset))
177
+ end
178
+
179
+ define_method("#{name}(3i)=") do |day|
180
+ attribute = send("#{name}_attribute")
181
+ value = attribute.send("value") || Time.now.utc
182
+ attribute.send("value=", Time.new(value.year, value.month, day.to_i, value.hour, value.min, value.sec, value.utc_offset))
183
+ end
184
+
185
+ define_method("#{name}(2i)=") do |month|
186
+ attribute = send("#{name}_attribute")
187
+ value = attribute.send("value") || Time.now.utc
188
+ attribute.send("value=", Time.new(value.year, month.to_i, value.day, value.hour, value.min, value.sec, value.utc_offset))
189
+ end
190
+
191
+ define_method("#{name}(1i)=") do |year|
192
+ attribute = send("#{name}_attribute")
193
+ value = attribute.send("value") || Time.now.utc
194
+ attribute.send("value=", Time.new(year.to_i, value.month, value.day, value.hour, value.min, value.sec, value.utc_offset))
195
+ end
196
+ end
133
197
  end
134
198
 
135
199
  def initialize(params={}, parent = nil)
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "1.0.6"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,6 +4,8 @@ class AcceptanceParams < SimpleParams::Params
4
4
  allow_undefined_params
5
5
  param :reference, type: :object, optional: true
6
6
  param :name
7
+ param :date_of_birth, type: :date, optional: true
8
+ param :current_time, type: :datetime, optional: true
7
9
  param :age, type: :integer, optional: true, validations: { inclusion: { in: 18..100 } }
8
10
  param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}
9
11
  param :sibling_names, type: :array, optional: true
@@ -75,6 +77,8 @@ describe SimpleParams::Params do
75
77
  params.to_hash.should eq({
76
78
  reference: nil,
77
79
  name: "Tom",
80
+ date_of_birth: nil,
81
+ current_time: nil,
78
82
  age: nil,
79
83
  color: "red",
80
84
  sibling_names: nil,
@@ -157,10 +161,34 @@ describe SimpleParams::Params do
157
161
  end
158
162
  end
159
163
 
164
+ describe "datetime setters", datetime_accessors: true do
165
+ it "can set date through Rails style date setters" do
166
+ params = AcceptanceParams.new(
167
+ "date_of_birth(3i)" => "5",
168
+ "date_of_birth(2i)" => "6",
169
+ "date_of_birth(1i)" => "1984"
170
+ )
171
+ params.date_of_birth.should eq(Date.new(1984, 6, 5))
172
+ end
173
+
174
+ it "can set datetime through Rails style date setters" do
175
+ params = AcceptanceParams.new(
176
+ "current_time(6i)" => "56",
177
+ "current_time(5i)" => "11",
178
+ "current_time(4i)" => "9",
179
+ "current_time(3i)" => "31",
180
+ "current_time(2i)" => "10",
181
+ "current_time(1i)" => "2015"
182
+ )
183
+ params.current_time.should eq(DateTime.new(2015, 10, 31, 9, 11, 56))
184
+ end
185
+ end
186
+
187
+
160
188
  describe "attributes", attributes: true do
161
189
  it "returns array of attribute symbols" do
162
190
  params = AcceptanceParams.new
163
- params.attributes.should eq([:reference, :name, :age, :color, :sibling_names, :address, :dogs])
191
+ params.attributes.should eq([:reference, :name, :date_of_birth, :current_time, :age, :color, :sibling_names, :address, :dogs])
164
192
  end
165
193
 
166
194
  it "returns array of attribute symbols for nested class" do
@@ -328,6 +356,8 @@ describe SimpleParams::Params do
328
356
  api_docs = <<-API_PIE_DOCS
329
357
  param:reference, Object, desc:'', required: false
330
358
  param :name, String, desc: '', required: true
359
+ param :date_of_birth, Date, desc: '', required: false
360
+ param :current_time, desc: '', required: false
331
361
  param :age, Integer, desc: '', required: false
332
362
  param :color, String, desc: '', required: false
333
363
  param :sibling_names, Array, desc: '', required: false
data/spec/params_spec.rb CHANGED
@@ -406,7 +406,7 @@ describe SimpleParams::Params do
406
406
  param :state, String, desc: '', required: false
407
407
  end
408
408
  param :phone, Hash, desc: '', required: true do
409
- param :cell_phone, desc: '', required: false
409
+ param :cell_phone, Boolean, desc: '', required: false
410
410
  param :phone_number, String, desc: '', required: true
411
411
  param :area_code, String, desc: '', required: false
412
412
  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: 1.0.6
4
+ version: 1.1.0
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-08 00:00:00.000000000 Z
11
+ date: 2015-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel