Sha256: 838d583577f3410027a34e1b4fa09d17bcf1fa704462944540fe9da83ff30bc2

Contents?: true

Size: 1014 Bytes

Versions: 3

Compression:

Stored size: 1014 Bytes

Contents

require "multi_json"

module Mongoid
  module NestedSerialization
    class Finder
      # 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

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mongoid-nested-serialization-0.0.4 lib/mongoid/nested_serialization/finder.rb
mongoid-nested-serialization-0.0.3 lib/mongoid/nested_serialization/finder.rb
mongoid-nested-serialization-0.0.2 lib/mongoid/nested_serialization/finder.rb