class Mongoid::NestedSerialization::Finder

Public Class Methods

find(json) click to toggle source
# File lib/mongoid/nested_serialization/finder.rb, line 7
def self.find(json)
  data = parse_input(json)
  # load the top level object
  object = top_level_object(data)
  # if we have embedded stuff
  while data["embedded"]
    # work on the next level down
    data = data["embedded"]
    # find the nested object
    object = nested_object(object, data)
  end
  # once at the bottom, return the object
  object
end

Private Class Methods

nested_object(object, data) click to toggle source

load an object nested within another, using the data

# File lib/mongoid/nested_serialization/finder.rb, line 34
def self.nested_object(object, data)
  object.send(data["association"]).find(data["id"])
end
parse_input(json) click to toggle source

parse the raw JSON data into a hash

# File lib/mongoid/nested_serialization/finder.rb, line 24
def self.parse_input(json)
  MultiJson.load(json)
end
top_level_object(data) click to toggle source

load the top level object directly with the collection

# File lib/mongoid/nested_serialization/finder.rb, line 29
def self.top_level_object(data)
  data["class_name"].constantize.find(data["id"])
end