Sha256: 917c9ff06c1dec875b86793a64e8c895bc96f1bd90129e35fd5689a5b99fa07f

Contents?: true

Size: 856 Bytes

Versions: 15

Compression:

Stored size: 856 Bytes

Contents

require 'active_support/core_ext/module/introspection'

module MongoModel
  module Associations
    module Base
      class Association
        attr_reader :definition, :instance
        delegate :name, :klass, :polymorphic?, :to => :definition
        
        def initialize(definition, instance)
          @definition, @instance = definition, instance
        end
        
        def proxy
          @proxy ||= proxy_class.new(self)
        end
        
        def replace(obj)
          proxy.target = obj
          proxy
        end
        
        def ensure_class(value)
          raise AssociationTypeMismatch, "expected instance of #{klass} but got #{value.class}" unless value.is_a?(klass)
        end
      
      protected
        def proxy_class
          self.class.parent::Proxy rescue Base::Proxy
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
mongomodel-0.5.2 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.5.1 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.5.0 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.9 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.8 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.7 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.6 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.5 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.4 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.3 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.2 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.1 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.4.0 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.3.6 lib/mongomodel/concerns/associations/base/association.rb
mongomodel-0.3.5 lib/mongomodel/concerns/associations/base/association.rb