explicit-parameters 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/explicit_parameters/parameters.rb +17 -6
- data/lib/explicit_parameters/version.rb +1 -1
- data/spec/parameters_spec.rb +53 -8
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95d2d462b480a386d2b59bd71ae9619c09c7906b
|
4
|
+
data.tar.gz: 81a74d049c309d8f2286a8b7f46e2567f765420d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac43195c730d525dc5c713cca9ca2332528204d785981e734a6668d916aa5d489f3f2f0914ed834a1baace08cb7003ef48690e38ea7eb10df2e99c3d4f39d874
|
7
|
+
data.tar.gz: a837c8cc1181542b576dfc3363a2b45f6544b523c4718f144fc8466f57afcce46a9b140186a5a2cb6d5d5377efd0210c0e860eedc6517eb8ec0ad4754e255e0e
|
@@ -24,15 +24,16 @@ module ExplicitParameters
|
|
24
24
|
new(params).validate!
|
25
25
|
end
|
26
26
|
|
27
|
-
def define(&block)
|
28
|
-
Class.new(self, &block)
|
27
|
+
def define(name = nil, &block)
|
28
|
+
name_class(Class.new(self, &block), name)
|
29
29
|
end
|
30
30
|
|
31
|
-
def requires(name, type, options = {}, &block)
|
32
|
-
accepts(name, type, options.merge(required: true))
|
31
|
+
def requires(name, type = nil, options = {}, &block)
|
32
|
+
accepts(name, type, options.merge(required: true), &block)
|
33
33
|
end
|
34
34
|
|
35
|
-
def accepts(name, type, options = {}, &block)
|
35
|
+
def accepts(name, type = nil, options = {}, &block)
|
36
|
+
type = define(name, &block) if block_given?
|
36
37
|
attribute(name, type, options.slice(:default, :required))
|
37
38
|
validations = options.except(:default)
|
38
39
|
validations[:coercion] = true
|
@@ -42,6 +43,16 @@ module ExplicitParameters
|
|
42
43
|
def optional_attributes
|
43
44
|
@optional_attributes ||= []
|
44
45
|
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def name_class(klass, name)
|
50
|
+
if name.present?
|
51
|
+
name = name.to_s.camelize
|
52
|
+
klass.singleton_class.send(:define_method, :name) { name }
|
53
|
+
end
|
54
|
+
klass
|
55
|
+
end
|
45
56
|
end
|
46
57
|
|
47
58
|
def initialize(attributes = {})
|
@@ -70,7 +81,7 @@ module ExplicitParameters
|
|
70
81
|
super.except(*missing_attributes)
|
71
82
|
end
|
72
83
|
|
73
|
-
delegate :each, to: :to_hash
|
84
|
+
delegate :each, :stringify_keys, to: :to_hash
|
74
85
|
delegate :[], to: :@original_attributes
|
75
86
|
|
76
87
|
private
|
data/spec/parameters_spec.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe ExplicitParameters::Parameters do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
let :definition do
|
5
|
+
ExplicitParameters::Parameters.define(:test) do
|
6
|
+
requires :id, Integer, numericality: {greater_than: 0}
|
7
|
+
accepts :name, String
|
8
|
+
accepts :title, String, default: 'Untitled'
|
9
|
+
end
|
10
10
|
end
|
11
11
|
|
12
12
|
let :parameters do
|
@@ -17,7 +17,7 @@ RSpec.describe ExplicitParameters::Parameters do
|
|
17
17
|
}
|
18
18
|
end
|
19
19
|
|
20
|
-
let(:params) {
|
20
|
+
let(:params) { definition.parse!(parameters.with_indifferent_access) }
|
21
21
|
|
22
22
|
it 'casts parameters to the declared type' do
|
23
23
|
expect(params.id).to be == 42
|
@@ -33,6 +33,15 @@ RSpec.describe ExplicitParameters::Parameters do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
it 'ignores unexpected parameters when converted to hash' do
|
37
|
+
expect(params.to_hash.keys).to be == %i(id name)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'ignores unexpected parameters when converted to hash with string keys' do
|
41
|
+
expect(params.stringify_keys.keys).to be == %w(id name)
|
42
|
+
end
|
43
|
+
|
44
|
+
|
36
45
|
it 'allows access to raw parameters when accessed like a Hash' do
|
37
46
|
expect(params[:id]).to be == '42'
|
38
47
|
expect(params[:unexpected]).to be == 'parameter'
|
@@ -41,7 +50,43 @@ RSpec.describe ExplicitParameters::Parameters do
|
|
41
50
|
it 'can perform any type of active model validations' do
|
42
51
|
message = {errors: {id: ['must be greater than 0']}}.to_json
|
43
52
|
expect {
|
44
|
-
|
53
|
+
definition.parse!('id' => -1)
|
45
54
|
}.to raise_error(ExplicitParameters::InvalidParameters, message)
|
46
55
|
end
|
56
|
+
|
57
|
+
context 'with nested parameters' do
|
58
|
+
let :definition do
|
59
|
+
ExplicitParameters::Parameters.define(:nested) do
|
60
|
+
requires :address do
|
61
|
+
requires :street, String
|
62
|
+
requires :city, String
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
let :parameters do
|
68
|
+
{
|
69
|
+
'address' => {
|
70
|
+
'street' => '3575 St-Laurent',
|
71
|
+
'city' => 'Montréal',
|
72
|
+
}
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'parses expose the nested hash as a `Parameters` instance' do
|
77
|
+
expect(params.address).to be_an ExplicitParameters::Parameters
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'parses nested hashes' do
|
81
|
+
expect(params.address.street).to be == '3575 St-Laurent'
|
82
|
+
expect(params.address.city).to be == 'Montréal'
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'reports missing attributes' do
|
86
|
+
message = {errors: {address: ['is required']}}.to_json
|
87
|
+
expect {
|
88
|
+
definition.parse!({})
|
89
|
+
}.to raise_error(ExplicitParameters::InvalidParameters, message)
|
90
|
+
end
|
91
|
+
end
|
47
92
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: explicit-parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -138,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: '0'
|
139
139
|
requirements: []
|
140
140
|
rubyforge_project:
|
141
|
-
rubygems_version: 2.
|
141
|
+
rubygems_version: 2.4.3
|
142
142
|
signing_key:
|
143
143
|
specification_version: 4
|
144
144
|
summary: Explicit parameters validation and casting for Rails APIs
|