Class: ParamsDeserializer
- Inherits:
-
Object
- Object
- ParamsDeserializer
- Defined in:
- lib/params_deserializers/attribute.rb,
lib/params_deserializers/params_deserializer.rb,
lib/params_deserializers/attribute_collection.rb
Overview
Copyright © 2015, Groupon, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
-
Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Defined Under Namespace
Classes: Attribute, AttributeCollection, InvalidKeyError, MissingRootKeyError
Class Attribute Summary collapse
-
.key_format ⇒ Object
Returns the value of attribute key_format.
-
.root_key ⇒ Object
readonly
Returns the value of attribute root_key.
-
.strict_mode ⇒ Object
Returns the value of attribute strict_mode.
Class Method Summary collapse
- .attribute(attr, options = {}) ⇒ Object
- .attributes(*args) ⇒ Object
- .attrs ⇒ Object
- .deserialize(params) ⇒ Object
- .has_many(attr, options = {}) ⇒ Object
- .ignore(*param_names) ⇒ Object
- .include_root_key? ⇒ Boolean
- .inherited(subclass) ⇒ Object
- .root(key, options = {}) ⇒ Object
Instance Method Summary collapse
- #deserialize ⇒ Object
-
#initialize(params) ⇒ ParamsDeserializer
constructor
A new instance of ParamsDeserializer.
Constructor Details
#initialize(params) ⇒ ParamsDeserializer
Returns a new instance of ParamsDeserializer.
37 38 39 40 41 |
# File 'lib/params_deserializers/params_deserializer.rb', line 37 def initialize(params) @params = params verify_root_key_exists verify_valid_keys end |
Class Attribute Details
.key_format ⇒ Object
Returns the value of attribute key_format.
100 101 102 |
# File 'lib/params_deserializers/params_deserializer.rb', line 100 def key_format @key_format end |
.root_key ⇒ Object (readonly)
Returns the value of attribute root_key.
99 100 101 |
# File 'lib/params_deserializers/params_deserializer.rb', line 99 def root_key @root_key end |
.strict_mode ⇒ Object
Returns the value of attribute strict_mode.
101 102 103 |
# File 'lib/params_deserializers/params_deserializer.rb', line 101 def strict_mode @strict_mode end |
Class Method Details
.attribute(attr, options = {}) ⇒ Object
121 122 123 124 125 |
# File 'lib/params_deserializers/params_deserializer.rb', line 121 def attribute(attr, = {}) define_getter_method(attr, ) do params_root[attr] end end |
.attributes(*args) ⇒ Object
133 134 135 136 137 |
# File 'lib/params_deserializers/params_deserializer.rb', line 133 def attributes(*args) args.each do |attr| attribute(attr) end end |
.attrs ⇒ Object
117 118 119 |
# File 'lib/params_deserializers/params_deserializer.rb', line 117 def attrs @attrs ||= AttributeCollection.new end |
.deserialize(params) ⇒ Object
113 114 115 |
# File 'lib/params_deserializers/params_deserializer.rb', line 113 def deserialize(params) new(params).deserialize end |
.has_many(attr, options = {}) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/params_deserializers/params_deserializer.rb', line 139 def has_many(attr, = {}) define_getter_method(attr, ) do return params_root[attr] unless [:deserializer] params_root[attr].map do |relation| [:deserializer].new(relation).deserialize end if params_root[attr].is_a?(Array) end end |
.ignore(*param_names) ⇒ Object
127 128 129 130 131 |
# File 'lib/params_deserializers/params_deserializer.rb', line 127 def ignore(*param_names) param_names.each do |param_name| attrs << Attribute.new(param_name, ignored: true) end end |
.include_root_key? ⇒ Boolean
154 155 156 |
# File 'lib/params_deserializers/params_deserializer.rb', line 154 def include_root_key? @root_key && !@discard_root_key end |
.inherited(subclass) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/params_deserializers/params_deserializer.rb', line 105 def inherited(subclass) subclass.instance_variable_set(:@attrs, @attrs) subclass.instance_variable_set(:@discard_root_key, @discard_root_key) subclass.instance_variable_set(:@key_format, @key_format) subclass.instance_variable_set(:@root_key, @root_key) subclass.instance_variable_set(:@strict_mode, @strict_mode) end |
.root(key, options = {}) ⇒ Object
149 150 151 152 |
# File 'lib/params_deserializers/params_deserializer.rb', line 149 def root(key, = {}) @root_key = key @discard_root_key = [:discard] end |
Instance Method Details
#deserialize ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/params_deserializers/params_deserializer.rb', line 43 def deserialize deserialized_params = {} self.class.attrs.unignored.each do |attr| next unless instance_exec(&attr.present_if) deserialized_params[attr.name] = self.send(attr.name) end format_keys(deserialized_params).with_indifferent_access end |