Sha256: f3518c53ad2451a06b26b1281b894908df35e0b44a86caa017dee179c073650a

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

module MongoMapper
  module EmbeddedDocument
    extend Support::DescendantAppends

    def self.included(model)
      model.class_eval do
        include InstanceMethods
        extend  ClassMethods
        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::Persistence
        plugin Plugins::Protected
        plugin Plugins::Rails
        plugin Plugins::Serialization
        plugin Plugins::Validations
        plugin Plugins::Callbacks

        attr_reader :_root_document, :_parent_document
      end

      super
    end

    module ClassMethods
      def embeddable?
        true
      end

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

    module InstanceMethods
      def destroyed?
        !!_root_document.try(:destroyed?)
      end

      def new?
        _root_document.try(:new?) || @new
      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

      def _parent_document=(value)
        @_root_document   = value._root_document
        @_parent_document = value
      end
    end # InstanceMethods
  end # EmbeddedDocument
end # MongoMapper

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
mongo_mapper-0.7.6 lib/mongo_mapper/embedded_document.rb
mongo_mapper-0.7.5 lib/mongo_mapper/embedded_document.rb
mongo_mapper_ign-0.7.4 lib/mongo_mapper/embedded_document.rb
mongo_mapper-0.7.4 lib/mongo_mapper/embedded_document.rb
mongo_mapper-0.7.3 lib/mongo_mapper/embedded_document.rb