params_deserializers 1.0.1 → 1.1.0
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,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 439197e5a7619e734f93320fc7dfe45e98e914dd
|
4
|
+
data.tar.gz: d1d989f1fc9aad81b4fefda1eb9369c83e13af16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69288262461ece3dcdbe8e6018b8c6dda8cade6750c42df8e04c8324faed1056670367ffc9b1e25e3341b5ba4cfc048308089ca05f7bfa45b9567bb9e66021fa
|
7
|
+
data.tar.gz: 2926ce18788b7c61c20d0a2cec61eed27c3e4705875e17ae09191f2029e1c7f52876a4ba0bee8ce424381aec4bd635e210593b006cc0ec17ba47bf77b27a8b15
|
data/README.md
CHANGED
@@ -45,6 +45,29 @@ class UserParamsDeserializer < ParamsDeserializer
|
|
45
45
|
end
|
46
46
|
```
|
47
47
|
|
48
|
+
## Strict Mode
|
49
|
+
|
50
|
+
Call the class method `strict` to specify that only explicitly defined keys be allowed. An invalid key will raise a `ParamsDeserializer::InvalidKeyError`.
|
51
|
+
If you want to use strict mode, but still allow a specific key that will not be automatically transformed in the output, you can use the class method `ignore`.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class UserParamsDeserializer < ParamsDeserializer
|
55
|
+
strict true
|
56
|
+
attribute :firstName
|
57
|
+
ignore :lastName
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
# Incoming hash: { :firstName => "Grace" }
|
62
|
+
# Deserialized params: { :first_name => "Grace" }
|
63
|
+
|
64
|
+
# Incoming hash: { :firstName => "Grace", :lastName => "Hopper" ]
|
65
|
+
# Deserialized params: { :first_name => "Grace" }
|
66
|
+
|
67
|
+
# Incoming hash: { :firstName => "Grace", :lastName => "Hopper", :birthday => "12/9/1906" }
|
68
|
+
# Result: ParamsDeserializer::InvalidKeyError: Invalid keys in params: :birthday.
|
69
|
+
```
|
70
|
+
|
48
71
|
## Renaming params
|
49
72
|
|
50
73
|
You can pass an options hash to `attribute`. If you include the `:rename_to` key in the options hash, your deserialized params will contain the renamed key instead of the original:
|
@@ -56,8 +79,8 @@ class UserParamsDeserializer < ParamsDeserializer
|
|
56
79
|
attribute :birthday
|
57
80
|
end
|
58
81
|
|
59
|
-
# Incoming hash: { :firstName => "Grace", :lastName => "Hopper, :birthday => "12/9/1906" }
|
60
|
-
# Deserialized params: { :first_name => "Grace", :last_name => "Hopper, :birthday => "12/9/1906" }
|
82
|
+
# Incoming hash: { :firstName => "Grace", :lastName => "Hopper", :birthday => "12/9/1906" }
|
83
|
+
# Deserialized params: { :first_name => "Grace", :last_name => "Hopper", :birthday => "12/9/1906" }
|
61
84
|
```
|
62
85
|
|
63
86
|
## Changing params case
|
@@ -70,8 +93,8 @@ class UserParamsDeserializer < ParamsDeserializer
|
|
70
93
|
format_keys :snake_case # (or :camel_case or :lower_camel)
|
71
94
|
end
|
72
95
|
|
73
|
-
# Incoming hash: { :firstName => "Grace", :lastName => "Hopper, :birthday => "12/9/1906" }
|
74
|
-
# Deserialized params: { :first_name => "Grace", :last_name => "Hopper, :birthday => "12/9/1906" }
|
96
|
+
# Incoming hash: { :firstName => "Grace", :lastName => "Hopper", :birthday => "12/9/1906" }
|
97
|
+
# Deserialized params: { :first_name => "Grace", :last_name => "Hopper", :birthday => "12/9/1906" }
|
75
98
|
```
|
76
99
|
|
77
100
|
## Dealing with a root key
|
@@ -267,7 +290,7 @@ Now, when an API user hits `UsersController#create` with the following JSON payl
|
|
267
290
|
|
268
291
|
## License
|
269
292
|
|
270
|
-
Copyright (c) 2015, Groupon, Inc.
|
293
|
+
Copyright (c) 2015, Groupon, Inc.
|
271
294
|
All rights reserved.
|
272
295
|
|
273
296
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
@@ -29,11 +29,13 @@
|
|
29
29
|
|
30
30
|
class ParamsDeserializer
|
31
31
|
class Attribute
|
32
|
-
attr_reader :name, :present_if
|
32
|
+
attr_reader :ignored, :original_name, :name, :present_if
|
33
33
|
|
34
|
-
def initialize(original_name,
|
35
|
-
@original_name
|
36
|
-
@
|
34
|
+
def initialize(original_name, options = {})
|
35
|
+
@original_name = original_name
|
36
|
+
@name = options[:rename_to] || @original_name
|
37
|
+
@ignored = options[:ignored]
|
38
|
+
@present_if = options[:present_if] || -> { params_root.has_key?(original_name) }
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
@@ -32,15 +32,17 @@ require 'active_support/core_ext/string/inflections'
|
|
32
32
|
|
33
33
|
class ParamsDeserializer
|
34
34
|
class MissingRootKeyError < StandardError; end
|
35
|
+
class InvalidKeyError < StandardError; end
|
35
36
|
|
36
37
|
def initialize(params)
|
37
38
|
@params = params
|
38
39
|
verify_root_key_exists
|
40
|
+
verify_valid_keys
|
39
41
|
end
|
40
42
|
|
41
43
|
def deserialize
|
42
44
|
deserialized_params = {}
|
43
|
-
self.class.attrs.each do |attr|
|
45
|
+
self.class.attrs.unignored.each do |attr|
|
44
46
|
next unless instance_exec(&attr.present_if)
|
45
47
|
deserialized_params[attr.name] = self.send(attr.name)
|
46
48
|
end
|
@@ -86,10 +88,23 @@ class ParamsDeserializer
|
|
86
88
|
end
|
87
89
|
end
|
88
90
|
|
91
|
+
def verify_valid_keys
|
92
|
+
invalid_params = params_root.keys - self.class.attrs.map(&:original_name)
|
93
|
+
if self.class.strict_mode && !invalid_params.blank?
|
94
|
+
raise InvalidKeyError, "Invalid keys in params: #{invalid_params.map(&:inspect).join(",")}."
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
89
98
|
class << self
|
90
99
|
attr_reader :root_key
|
91
100
|
attr_accessor :key_format
|
101
|
+
attr_accessor :strict_mode
|
92
102
|
alias_method :format_keys, :key_format=
|
103
|
+
alias_method :strict, :strict_mode=
|
104
|
+
|
105
|
+
def deserialize(params)
|
106
|
+
new(params).deserialize
|
107
|
+
end
|
93
108
|
|
94
109
|
def attrs
|
95
110
|
@attrs ||= AttributeCollection.new
|
@@ -101,6 +116,10 @@ class ParamsDeserializer
|
|
101
116
|
end
|
102
117
|
end
|
103
118
|
|
119
|
+
def ignore(attr)
|
120
|
+
attrs << Attribute.new(attr, ignored: true)
|
121
|
+
end
|
122
|
+
|
104
123
|
def attributes(*args)
|
105
124
|
args.each do |attr|
|
106
125
|
attribute(attr)
|
@@ -130,7 +149,7 @@ class ParamsDeserializer
|
|
130
149
|
|
131
150
|
def define_getter_method(attr, options = {}, &block)
|
132
151
|
options[:rename_to] ||= attr
|
133
|
-
attrs << Attribute.new(attr, options
|
152
|
+
attrs << Attribute.new(attr, options)
|
134
153
|
define_method(options[:rename_to], &block) unless method_defined?(options[:rename_to])
|
135
154
|
end
|
136
155
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: params_deserializers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesse Pinho
|
@@ -10,82 +10,82 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-01-05 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
21
|
version: 3.2.16
|
22
|
-
- - <
|
22
|
+
- - "<"
|
23
23
|
- !ruby/object:Gem::Version
|
24
24
|
version: '5.0'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
27
|
version_requirements: !ruby/object:Gem::Requirement
|
28
28
|
requirements:
|
29
|
-
- -
|
29
|
+
- - ">="
|
30
30
|
- !ruby/object:Gem::Version
|
31
31
|
version: 3.2.16
|
32
|
-
- - <
|
32
|
+
- - "<"
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '5.0'
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: guard-rspec
|
37
37
|
requirement: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
42
|
type: :development
|
43
43
|
prerelease: false
|
44
44
|
version_requirements: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
- !ruby/object:Gem::Dependency
|
50
50
|
name: sqlite3
|
51
51
|
requirement: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
58
|
version_requirements: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
64
|
name: rails
|
65
65
|
requirement: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '4.2'
|
70
70
|
type: :development
|
71
71
|
prerelease: false
|
72
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
73
|
requirements:
|
74
|
-
- -
|
74
|
+
- - ">="
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '4.2'
|
77
77
|
- !ruby/object:Gem::Dependency
|
78
78
|
name: rspec-rails
|
79
79
|
requirement: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
|
-
- -
|
81
|
+
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
83
|
version: '0'
|
84
84
|
type: :development
|
85
85
|
prerelease: false
|
86
86
|
version_requirements: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- -
|
88
|
+
- - ">="
|
89
89
|
- !ruby/object:Gem::Version
|
90
90
|
version: '0'
|
91
91
|
description: Modeled after active_model_serializers, this gem allows you to create
|
@@ -114,17 +114,17 @@ require_paths:
|
|
114
114
|
- lib
|
115
115
|
required_ruby_version: !ruby/object:Gem::Requirement
|
116
116
|
requirements:
|
117
|
-
- -
|
117
|
+
- - ">="
|
118
118
|
- !ruby/object:Gem::Version
|
119
119
|
version: '0'
|
120
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
125
|
requirements: []
|
126
126
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.4.
|
127
|
+
rubygems_version: 2.4.6
|
128
128
|
signing_key:
|
129
129
|
specification_version: 4
|
130
130
|
summary: Deserializers for Rails params
|