simple_params 0.0.1pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +60 -0
- data/README.md +161 -0
- data/lib/simple_params/errors.rb +133 -0
- data/lib/simple_params/params.rb +129 -0
- data/lib/simple_params/validations.rb +28 -0
- data/lib/simple_params/version.rb +3 -0
- data/lib/simple_params.rb +4 -0
- data/simple_params.gemspec +26 -0
- data/spec/acceptance_spec.rb +129 -0
- data/spec/errors_spec.rb +453 -0
- data/spec/params_spec.rb +215 -0
- data/spec/spec_helper.rb +12 -0
- metadata +140 -0
data/spec/params_spec.rb
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class DummyParams < SimpleParams::Params
|
4
|
+
string_param :name
|
5
|
+
integer_param :age, optional: true
|
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"] }}
|
9
|
+
|
10
|
+
nested_hash :address do
|
11
|
+
string_param :street
|
12
|
+
string_param :city, validations: { length: { in: 4..40 } }
|
13
|
+
string_param :zip_code, optional: true, validations: { length: { in: 5..9 } }
|
14
|
+
param :state, default: "North Carolina"
|
15
|
+
end
|
16
|
+
|
17
|
+
nested_hash :phone do
|
18
|
+
boolean_param :cell_phone, default: true
|
19
|
+
string_param :phone_number, validations: { length: { in: 7..10 } }
|
20
|
+
string_param :area_code, default: lambda { |params, param|
|
21
|
+
if params.phone_number.present?
|
22
|
+
params.phone_number[0..2]
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe SimpleParams::Params do
|
29
|
+
describe "accessors", accessors: true do
|
30
|
+
let(:params) { DummyParams.new }
|
31
|
+
|
32
|
+
it "has getter and setter methods for required param" do
|
33
|
+
params.should respond_to(:name)
|
34
|
+
params.name.should be_nil
|
35
|
+
params.name = "Tom"
|
36
|
+
params.name.should eq("Tom")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "has getter and setter methods for optional param" do
|
40
|
+
params.should respond_to(:age)
|
41
|
+
params.name.should be_nil
|
42
|
+
params.age = 19
|
43
|
+
params.age.should eq(19)
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "nested params", nested: true do
|
47
|
+
it "has getter and setter methods for required param" do
|
48
|
+
params.address.should respond_to(:street)
|
49
|
+
params.address.street.should be_nil
|
50
|
+
params.address.street = "1 Main St."
|
51
|
+
params.address.street.should eq("1 Main St.")
|
52
|
+
end
|
53
|
+
|
54
|
+
it "has getter and setter methods for optional param" do
|
55
|
+
params.address.should respond_to(:zip_code)
|
56
|
+
params.address.zip_code.should be_nil
|
57
|
+
params.address.zip_code = "20165"
|
58
|
+
params.address.zip_code.should eq("20165")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "array syntax", array_syntax: true do
|
64
|
+
let(:params) do
|
65
|
+
DummyParams.new(
|
66
|
+
name: "Bill",
|
67
|
+
age: 30,
|
68
|
+
address: {
|
69
|
+
city: "Greenville"
|
70
|
+
}
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "can access 'name' through array syntax" do
|
75
|
+
params[:name].should eq("Bill")
|
76
|
+
params["name"].should eq("Bill")
|
77
|
+
end
|
78
|
+
|
79
|
+
it "can set 'name' through array syntax" do
|
80
|
+
params[:name] = "Tom"
|
81
|
+
params[:name].should eq("Tom")
|
82
|
+
params["name"].should eq("Tom")
|
83
|
+
end
|
84
|
+
|
85
|
+
it "can access 'age' through array syntax" do
|
86
|
+
params[:age].should eq(30)
|
87
|
+
params["age"].should eq(30)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "can set 'age' through array syntax" do
|
91
|
+
params[:age] = 42
|
92
|
+
params[:age].should eq(42)
|
93
|
+
params["age"].should eq(42)
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "nested params", nested: true do
|
97
|
+
it "can access 'city' through array syntax" do
|
98
|
+
params[:address][:city].should eq("Greenville")
|
99
|
+
params["address"]["city"].should eq("Greenville")
|
100
|
+
end
|
101
|
+
|
102
|
+
it "can set 'city' through array syntax" do
|
103
|
+
params[:address][:city] = "Asheville"
|
104
|
+
params[:address][:city].should eq("Asheville")
|
105
|
+
params["address"]["city"].should eq("Asheville")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "coercion", coercion: true do
|
111
|
+
it "coerces values on initialization" do
|
112
|
+
params = DummyParams.new(age: "42")
|
113
|
+
params.age.should eq(42)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "coerces values from setters" do
|
117
|
+
params = DummyParams.new
|
118
|
+
params.age = "42"
|
119
|
+
params.age.should eq(42)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "coerces nested attributes on initialization" do
|
123
|
+
params = DummyParams.new(address: { zip_code: 90210 })
|
124
|
+
params.address.zip_code.should eq("90210")
|
125
|
+
end
|
126
|
+
|
127
|
+
it "coerces nested attributes from setters" do
|
128
|
+
params = DummyParams.new
|
129
|
+
params.address.zip_code = 90210
|
130
|
+
params.address.zip_code.should eq("90210")
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe "defaults", defaults: true do
|
135
|
+
describe "simple defaults" do
|
136
|
+
it "sets default values on initialization without key" do
|
137
|
+
params = DummyParams.new
|
138
|
+
params.amount.should eq(0.10)
|
139
|
+
end
|
140
|
+
|
141
|
+
it "sets default values on initialization with nil value" do
|
142
|
+
params = DummyParams.new(amount: nil)
|
143
|
+
params.amount.should eq(0.10)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "sets default values on initialization with blank value" do
|
147
|
+
params = DummyParams.new(amount: "")
|
148
|
+
params.amount.should eq(0.10)
|
149
|
+
end
|
150
|
+
|
151
|
+
describe "nested params" do
|
152
|
+
it "sets default values on initialization without key" do
|
153
|
+
params = DummyParams.new
|
154
|
+
params.phone.cell_phone.should be_truthy
|
155
|
+
end
|
156
|
+
|
157
|
+
it "sets default values on initialization with nil value" do
|
158
|
+
params = DummyParams.new(phone: { cell_phone: nil })
|
159
|
+
params.phone.cell_phone.should be_truthy
|
160
|
+
end
|
161
|
+
|
162
|
+
it "sets default values on initialization with blank value" do
|
163
|
+
params = DummyParams.new(phone: { cell_phone: "" })
|
164
|
+
params.phone.cell_phone.should be_truthy
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "Proc defaults" do
|
170
|
+
it "sets default values on initialization without key" do
|
171
|
+
params = DummyParams.new
|
172
|
+
params.first_initial.should be_nil
|
173
|
+
params = DummyParams.new(name: "Tom")
|
174
|
+
params.first_initial.should eq("T")
|
175
|
+
end
|
176
|
+
|
177
|
+
it "sets default values on initialization with nil value" do
|
178
|
+
params = DummyParams.new
|
179
|
+
params.first_initial.should be_nil
|
180
|
+
params = DummyParams.new(name: "Tom", first_initial: nil)
|
181
|
+
params.first_initial.should eq("T")
|
182
|
+
end
|
183
|
+
|
184
|
+
it "sets default values on initialization with blank value" do
|
185
|
+
params = DummyParams.new
|
186
|
+
params.first_initial.should be_nil
|
187
|
+
params = DummyParams.new(name: "Tom", first_initial: "")
|
188
|
+
params.first_initial.should eq("T")
|
189
|
+
end
|
190
|
+
|
191
|
+
describe "nested params", failing: true do
|
192
|
+
it "sets default values on initialization without key" do
|
193
|
+
params = DummyParams.new
|
194
|
+
params.phone.area_code.should be_nil
|
195
|
+
params = DummyParams.new(phone: { phone_number: "8185559988" })
|
196
|
+
params.phone.area_code.should eq("818")
|
197
|
+
end
|
198
|
+
|
199
|
+
it "sets default values on initialization with nil value" do
|
200
|
+
params = DummyParams.new
|
201
|
+
params.phone.area_code.should be_nil
|
202
|
+
params = DummyParams.new(phone: { phone_number: "8185559988", area_code: nil })
|
203
|
+
params.phone.area_code.should eq("818")
|
204
|
+
end
|
205
|
+
|
206
|
+
it "sets default values on initialization with blank value" do
|
207
|
+
params = DummyParams.new
|
208
|
+
params.phone.area_code.should be_nil
|
209
|
+
params = DummyParams.new(phone: { phone_number: "8185559988", area_code: "" })
|
210
|
+
params.phone.area_code.should eq("818")
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'simple_params'
|
4
|
+
|
5
|
+
spec = Gem::Specification.find_by_name("simple_params")
|
6
|
+
gem_root = spec.gem_dir
|
7
|
+
Dir[("#{gem_root}/spec/support/**/*.rb")].each {|f| require f}
|
8
|
+
I18n.config.enforce_available_locales = true
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.mock_with :rspec
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1pre2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- brycesenz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: virtus
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 1.0.0
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.0.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.5'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.5'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.1'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.1'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ~>
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '2.6'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '2.6'
|
89
|
+
description: Simple way to specify API params
|
90
|
+
email:
|
91
|
+
- bryce.senz@gmail.com
|
92
|
+
executables: []
|
93
|
+
extensions: []
|
94
|
+
extra_rdoc_files: []
|
95
|
+
files:
|
96
|
+
- .rspec
|
97
|
+
- .ruby-gemset
|
98
|
+
- .ruby-version
|
99
|
+
- Gemfile
|
100
|
+
- Gemfile.lock
|
101
|
+
- README.md
|
102
|
+
- lib/simple_params.rb
|
103
|
+
- lib/simple_params/errors.rb
|
104
|
+
- lib/simple_params/params.rb
|
105
|
+
- lib/simple_params/validations.rb
|
106
|
+
- lib/simple_params/version.rb
|
107
|
+
- simple_params.gemspec
|
108
|
+
- spec/acceptance_spec.rb
|
109
|
+
- spec/errors_spec.rb
|
110
|
+
- spec/params_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
homepage: https://github.com/brycesenz/simple_params
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ! '>'
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: 1.3.1
|
130
|
+
requirements: []
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 2.2.2
|
133
|
+
signing_key:
|
134
|
+
specification_version: 4
|
135
|
+
summary: A DSL for specifying params, including type coercion and validation
|
136
|
+
test_files:
|
137
|
+
- spec/acceptance_spec.rb
|
138
|
+
- spec/errors_spec.rb
|
139
|
+
- spec/params_spec.rb
|
140
|
+
- spec/spec_helper.rb
|