Sha256: 86ac40085e32d20ecd0c212ac5f833e396af9f4b3ffdb6239c0ef7a6d6cdd422

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

module Mongoid #:nodoc:
  module Associations #:nodoc:
    class HasOne #:nodoc:
      include Decorator

      delegate :valid?, :to => :document

      attr_accessor :parent, :options

      # Build a new object for the association.
      def build(attributes)
        @document = attributes.assimilate(@parent, @options)
        decorate!
        self
      end

      # Create a new object for the association and save it.
      def create(attributes)
        build(attributes)
        @document.save
        self
      end

      # Creates the new association by finding the attributes in 
      # the parent document with its name, and instantiating a 
      # new document for it.
      #
      # All method calls on this object will then be delegated
      # to the internal document itself.
      def initialize(document, options)
        @parent, @options = document, options
        attributes = @parent.attributes[options.name]
        @document = (attributes || {}).assimilate(@parent, @options)
        decorate!
      end

      class << self
        # Returns the macro used to create the association.
        def macro
          :has_one
        end

        # Perform an update of the relationship of the parent and child. This
        # will assimilate the child +Document+ into the parent's object graph.
        #
        # Options:
        #
        # child: The child +Document+ or +Hash+.
        # parent: The parent +Document+ to update.
        # options: The association +Options+
        #
        # Example:
        #
        # <tt>HasOne.update({:first_name => "Hank"}, person, options)</tt>
        def update(child, parent, options)
          child.assimilate(parent, options)
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongoid-0.9.4 lib/mongoid/associations/has_one.rb
mongoid-0.9.3 lib/mongoid/associations/has_one.rb
mongoid-0.9.2 lib/mongoid/associations/has_one.rb
mongoid-0.9.1 lib/mongoid/associations/has_one.rb