Module: Proxima::Serialization::ClassMethods

Defined in:
lib/proxima/serialization.rb

Instance Method Summary collapse

Instance Method Details

#convert_query_or_delta_to_json(query) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/proxima/serialization.rb', line 119

def convert_query_or_delta_to_json(query)
  json_query = {}
  query.each do |attribute, val|
    attr_str  = attribute.to_s
    json_path = attributes[attribute] ? attributes[attribute][:json_path] : attr_str

    json_query[json_path] = unless attr_str[0] == '$' && val.is_a?(Hash)
      val
    else
      self.convert_query_or_delta_to_json val
    end
  end
  json_query
end

#from_json(json, opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/proxima/serialization.rb', line 104

def from_json(json, opts = {})
  json = ActiveSupport::JSON.decode(json) if json.is_a? String
  json = json.values.first if opts[:include_root] || self.include_root_in_json
  json = json.first        if opts[:single_model_from_array] && json.is_a?(Array)

  if json.is_a? Array
    return json.map { |json| self.from_json json }
  end

  model            = self.new.from_json json
  model.new_record = opts[:new_record] || false

  model
end