Sha256: 4623e5fe4c3aa77f3f4ab7f3b5c0ae2ef006bae52d3824970f776d6cb657d2dd

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require "multi_json"

module Mongoid
  module NestedSerialization
    class Finder
      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
      # parse the raw JSON data into a hash
      def self.parse_input(json)
        MultiJson.load(json)
      end
      
      # load the top level object directly with the collection
      def self.top_level_object(data)
        data["class_name"].constantize.find(data["id"])
      end
      
      # load an object nested within another, using the data
      def self.nested_object(object, data)
        object.send(data["association"]).find(data["id"])
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-nested-serialization-0.0.5 lib/mongoid/nested_serialization/finder.rb