simple_params 0.0.1pre2 → 0.0.2.pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzljZWZmNDczMjI4ZDk5ZDg2NmYxNzRlNzQ5ZjhjNDU4YjJhMmVjNw==
4
+ MzEzZDlkYmYyYWJmZjI3NWI4OTg3ZTFhMjJiYmQzYjAwYjIwNmY1YQ==
5
5
  data.tar.gz: !binary |-
6
- ZWQ0MGE5MjE4NmQ4MDUxYzMxNmUwMjM4ZWU4NDk0YWQyYTJkNWZmNw==
6
+ MzViNGIyYmIxY2EyMDU4MzY0MmU1NTA3N2FiYzY1MWIxODJhYzNmNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ODJkOTE3YzRjNTUzOGQ3ZGE0MDFlYzg5Mzc3YzNjMmI4NDJlZTA4OGZlYzkw
10
- MDkzZDlmMmNkZjY1YmZlNjc0OGJhZTk2MGJiNTEwMjc0NDcyZWFmNzJiMmZi
11
- MjVlNzFjZTU0NWY0NGY5ODg5M2RmYmQ0MTQ0ZjNhMTgzMTQ3ZjU=
9
+ MTlhMzg1OTVlZTE2YjQ0YzcxOWY5YTYzZjg5NDQzYmJkZTU1YjY5Y2NlM2Vm
10
+ NjgwMzEyYjA2Mjg5ZTQ0MTJjYTgyN2JkOWNlMDI2MDgxMTc2ZjMzNzI5ZTMz
11
+ YTg5MzUyMmRmYzc2N2RmODk2MDZlOWM5NzcyZGViZTUzMGRmMmQ=
12
12
  data.tar.gz: !binary |-
13
- NDhlYzBmNDhlOGYzYjIyNzY2NDQ2ZmViNmFmOTA2YzQ1NmViNGJlOTEwOTg1
14
- OTc4ODg2NDlmYzRkZGRlY2M4NTdjYTBhZjE0NWMzMWRmNTgwZWRmNjI5ZGY5
15
- ZjFkY2I1MzliODY4Y2Q2N2I5ODVmYWM5ZDBmNDc4MzRiOWY2OTQ=
13
+ NDZmZmUzMWI4NGRhZmFjM2IxZTczOTNhMWVkODBhZTFkM2Y3Y2ZkMjU3YThi
14
+ NWQxOWNkYzQwYjhhZGFhMDYwMjM3MWNmMTU1ZmIxNTJiOTUwM2FlM2I0NWJm
15
+ ZDVlMjIzMGYyN2UyYTc0Y2FkZjI3ZDcxZmJkMjYxYmU2NWM4MDQ=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_params (0.0.1pre)
4
+ simple_params (0.0.1pre2)
5
5
  activemodel (>= 3.0, < 5.0)
6
6
  virtus (>= 1.0.0)
7
7
 
@@ -0,0 +1,41 @@
1
+ require "active_model"
2
+
3
+ module SimpleParams
4
+ module Formatters
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def _formatters
9
+ @_formatters || {}
10
+ end
11
+
12
+ def format(attribute, formatter)
13
+ @_formatters ||= {}
14
+ @_formatters[attribute.to_sym] = formatter
15
+ end
16
+ end
17
+
18
+ def run_formatters
19
+ _formatters.each do |attribute, method|
20
+ value = send(attribute.to_sym)
21
+ unless value.blank?
22
+ out = evaluate_proc_or_method(attribute, method, value)
23
+ send("#{attribute.to_sym}=", out)
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+ def evaluate_proc_or_method(attribute, method, value)
30
+ if method.is_a?(Proc)
31
+ method.call(self, value)
32
+ else
33
+ self.send(method)
34
+ end
35
+ end
36
+
37
+ def _formatters
38
+ self.class._formatters || {}
39
+ end
40
+ end
41
+ end
@@ -6,6 +6,7 @@ module SimpleParams
6
6
  include Virtus.model
7
7
  include ActiveModel::Validations
8
8
  include SimpleParams::Validations
9
+ include SimpleParams::Formatters
9
10
 
10
11
  class << self
11
12
  TYPE_MAPPINGS = {
@@ -16,8 +17,7 @@ module SimpleParams
16
17
  date: Time,
17
18
  time: DateTime,
18
19
  float: Float,
19
- # See note on Virtus
20
- boolean: Axiom::Types::Boolean,
20
+ boolean: Axiom::Types::Boolean, # See note on Virtus
21
21
  array: Array,
22
22
  hash: Hash
23
23
  }
@@ -31,6 +31,7 @@ module SimpleParams
31
31
  def param(name, opts={})
32
32
  define_attribute(name, opts)
33
33
  add_validations(name, opts)
34
+ add_formatters(name, opts)
34
35
  end
35
36
 
36
37
  def nested_hash(name, opts={}, &block)
@@ -63,6 +64,13 @@ module SimpleParams
63
64
  validates name, validations unless validations.empty?
64
65
  end
65
66
 
67
+ def add_formatters(name, opts = {})
68
+ formatter = opts[:format]
69
+ unless formatter.nil?
70
+ format name, formatter
71
+ end
72
+ end
73
+
66
74
  def define_nested_class(&block)
67
75
  Class.new(Params).tap do |klass|
68
76
  name_function = Proc.new {
@@ -83,6 +91,7 @@ module SimpleParams
83
91
  @errors = SimpleParams::Errors.new(self, @nested_params)
84
92
  initialize_nested_classes
85
93
  set_accessors(params)
94
+ run_formatters
86
95
  # This method comes from Virtus
87
96
  # virtus/lib/virtus/instance_methods.rb
88
97
  set_default_attributes
@@ -1,3 +1,3 @@
1
1
  module SimpleParams
2
- VERSION = "0.0.1pre2"
2
+ VERSION = "0.0.2.pre1"
3
3
  end
data/lib/simple_params.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'simple_params/version'
2
+ require 'simple_params/formatters'
2
3
  require 'simple_params/errors'
3
4
  require 'simple_params/validations'
4
5
  require 'simple_params/params'
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleParams::Formatters do
4
+ class Racecar
5
+ include SimpleParams::Formatters
6
+
7
+ def initialize(params={})
8
+ params.each { |k,v| send("#{k}=",v) }
9
+ run_formatters
10
+ end
11
+
12
+ attr_accessor :make, :model
13
+
14
+ format :make, :strip_make_whitespace
15
+ format :model, lambda { |car, model| model.upcase }
16
+
17
+ def strip_make_whitespace
18
+ self.make.strip
19
+ end
20
+ end
21
+
22
+ describe "formatting with Procs" do
23
+ it "does not break if initial attribute is nil" do
24
+ car = Racecar.new
25
+ car.model.should be_nil
26
+ end
27
+
28
+ it "formats attribute on initialize" do
29
+ car = Racecar.new(make: "porsche", model: "boxster")
30
+ car.model.should eq("BOXSTER")
31
+ end
32
+ end
33
+
34
+ describe "formatting with methods" do
35
+ it "does not break if initial attribute is nil" do
36
+ car = Racecar.new
37
+ car.make.should be_nil
38
+ end
39
+
40
+ it "formats attribute on initialize" do
41
+ car = Racecar.new(make: " Porsche ")
42
+ car.make.should eq("Porsche")
43
+ end
44
+ end
45
+ end
data/spec/params_spec.rb CHANGED
@@ -4,25 +4,33 @@ class DummyParams < SimpleParams::Params
4
4
  string_param :name
5
5
  integer_param :age, optional: true
6
6
  string_param :first_initial, default: lambda { |params, param| params.name[0] if params.name.present? }
7
- decimal_param :amount, optional: true, default: 0.10
8
- param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}
7
+ decimal_param :amount, optional: true, default: 0.10, format: lambda { |params, param| param.round(2) }
8
+ param :color, default: "red", validations: { inclusion: { in: ["red", "green"] }}, format: :lower_case_colors
9
9
 
10
10
  nested_hash :address do
11
11
  string_param :street
12
12
  string_param :city, validations: { length: { in: 4..40 } }
13
13
  string_param :zip_code, optional: true, validations: { length: { in: 5..9 } }
14
- param :state, default: "North Carolina"
14
+ param :state, default: "North Carolina", format: :transform_state_code
15
+
16
+ def transform_state_code
17
+ state == "SC" ? "South Carolina" : state
18
+ end
15
19
  end
16
20
 
17
21
  nested_hash :phone do
18
22
  boolean_param :cell_phone, default: true
19
- string_param :phone_number, validations: { length: { in: 7..10 } }
23
+ string_param :phone_number, validations: { length: { in: 7..10 } }, format: lambda { |params, attribute| attribute.gsub(/\D/, "") }
20
24
  string_param :area_code, default: lambda { |params, param|
21
25
  if params.phone_number.present?
22
26
  params.phone_number[0..2]
23
27
  end
24
28
  }
25
29
  end
30
+
31
+ def lower_case_colors
32
+ color.downcase
33
+ end
26
34
  end
27
35
 
28
36
  describe SimpleParams::Params do
@@ -188,7 +196,7 @@ describe SimpleParams::Params do
188
196
  params.first_initial.should eq("T")
189
197
  end
190
198
 
191
- describe "nested params", failing: true do
199
+ describe "nested params" do
192
200
  it "sets default values on initialization without key" do
193
201
  params = DummyParams.new
194
202
  params.phone.area_code.should be_nil
@@ -212,4 +220,34 @@ describe SimpleParams::Params do
212
220
  end
213
221
  end
214
222
  end
223
+
224
+ describe "formatters", formatters: true do
225
+ describe "Proc formatters" do
226
+ it "formats on initialization" do
227
+ params = DummyParams.new(amount: 0.1234)
228
+ params.amount.should eq(0.12)
229
+ end
230
+
231
+ describe "nested params" do
232
+ it "formats on initialization" do
233
+ params = DummyParams.new(phone: { phone_number: "818-555-9988" })
234
+ params.phone.phone_number.should eq("8185559988")
235
+ end
236
+ end
237
+ end
238
+
239
+ describe "method formatters" do
240
+ it "formats on initialization" do
241
+ params = DummyParams.new(color: "BLUE")
242
+ params.color.should eq("blue")
243
+ end
244
+
245
+ describe "nested params" do
246
+ it "formats on initialization" do
247
+ params = DummyParams.new(address: { state: "SC" })
248
+ params.address.state.should eq("South Carolina")
249
+ end
250
+ end
251
+ end
252
+ end
215
253
  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.1pre2
4
+ version: 0.0.2.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - brycesenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-20 00:00:00.000000000 Z
11
+ date: 2015-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -101,12 +101,14 @@ files:
101
101
  - README.md
102
102
  - lib/simple_params.rb
103
103
  - lib/simple_params/errors.rb
104
+ - lib/simple_params/formatters.rb
104
105
  - lib/simple_params/params.rb
105
106
  - lib/simple_params/validations.rb
106
107
  - lib/simple_params/version.rb
107
108
  - simple_params.gemspec
108
109
  - spec/acceptance_spec.rb
109
110
  - spec/errors_spec.rb
111
+ - spec/formatters_spec.rb
110
112
  - spec/params_spec.rb
111
113
  - spec/spec_helper.rb
112
114
  homepage: https://github.com/brycesenz/simple_params
@@ -136,5 +138,6 @@ summary: A DSL for specifying params, including type coercion and validation
136
138
  test_files:
137
139
  - spec/acceptance_spec.rb
138
140
  - spec/errors_spec.rb
141
+ - spec/formatters_spec.rb
139
142
  - spec/params_spec.rb
140
143
  - spec/spec_helper.rb