Sha256: 671115c0a3773cb228a7af775d8da820cf016e3a99252dd56014b4c7350c531c

Contents?: true

Size: 1008 Bytes

Versions: 4

Compression:

Stored size: 1008 Bytes

Contents

module Mongoid #:nodoc:
  module Associations #:nodoc:
    class HasOneAssociation #:nodoc:

      attr_reader :document
      delegate :valid?, :to => :document

      # 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(association_name, document)
        klass = association_name.to_s.titleize.constantize
        attributes = document.attributes[association_name]
        @document = klass.new(attributes)
        @document.parent = document
        decorate!
      end

      private
      def decorate!
        @document.public_methods(false).each do |method|
          (class << self; self; end).class_eval do
            define_method method do |*args|
              @document.send method, *args
            end
          end
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
durran-mongoid-0.2.4 lib/mongoid/associations/has_one_association.rb
mongoid-0.2.7 lib/mongoid/associations/has_one_association.rb
mongoid-0.2.6 lib/mongoid/associations/has_one_association.rb
mongoid-0.2.5 lib/mongoid/associations/has_one_association.rb