Sha256: 10b0f98341977104123522c4747dd47db08c064d8091f43d5943201858871303

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module MongoMapper
  module EmbeddedDocument
    extend Support::DescendantAppends
    extend ActiveSupport::Concern
    included do
      extend Plugins

      plugin Plugins::Associations
      plugin Plugins::Clone
      plugin Plugins::Descendants
      plugin Plugins::Equality
      plugin Plugins::Inspect
      plugin Plugins::Keys
      plugin Plugins::Logger
      plugin Plugins::Protected
      plugin Plugins::Rails
      plugin Plugins::Serialization
      plugin Plugins::Validations

      attr_accessor :_root_document, :_parent_document
    end

    module ClassMethods
      def embeddable?
        true
      end

      def embedded_in(owner_name)
        define_method(owner_name) { _parent_document }
      end
    end

    module InstanceMethods
      def initialize(attrs={}, from_database=false)
        unless attrs.nil?
          provided_keys = attrs.keys.map { |k| k.to_s }
          unless provided_keys.include?('_id') || provided_keys.include?('id')
            write_key :_id, Mongo::ObjectID.new
          end
        end

        assign_type_if_present

        if from_database
          @new = false
          self.attributes = attrs
        else
          @new = true
          assign(attrs)
        end
      end
      
      def save(options={})
        if result = _root_document.try(:save, options)
          @new = false
        end
        result
      end

      def save!(options={})
        if result = _root_document.try(:save!, options)
          @new = false
        end
        result
      end
    end # InstanceMethods
  end # EmbeddedDocument
end # MongoMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo_mapper-rails3-0.7.0.1 lib/mongo_mapper/embedded_document.rb