Class: ParamsDeserializer

Inherits:
Object
  • Object
show all
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:

  1. Redistributions of source code must retain the above copyright notice,

this list of conditions and the following disclaimer.

  1. 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.

  1. 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

Class Method Summary collapse

Instance Method Summary collapse

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_formatObject

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_keyObject (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_modeObject

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, options = {})
  define_getter_method(attr, options) 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

.attrsObject



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, options = {})
  define_getter_method(attr, options) do
    return params_root[attr] unless options[:deserializer]

    params_root[attr].map do |relation|
      options[: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

Returns:

  • (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, options = {})
  @root_key = key
  @discard_root_key = options[:discard]
end

Instance Method Details

#deserializeObject



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