Sha256: 88a8d5497ecc4296ec62747c7aaa0a680308226034db622ab518f0ee31764891

Contents?: true

Size: 1.75 KB

Versions: 11

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8
module Mongoid # :nodoc:
  module Relations #:nodoc:
    module Bindings #:nodoc:
      module Referenced #:nodoc:

        # Binding class for references_one relations.
        class One < Binding

          # Binds the base object to the inverse of the relation. This is so we
          # are referenced to the actual objects themselves on both sides.
          #
          # This case sets the metadata on the inverse object as well as the
          # document itself.
          #
          # @example Bind the document.
          #   person.game.bind(:continue => true)
          #   person.game = Game.new
          #
          # @since 2.0.0.rc.1
          def bind
            unless binding?
              binding do
                target.send(metadata.foreign_key_setter, base.id)
                target.send(metadata.inverse_setter, base)
                if metadata.type
                  target.send(metadata.type_setter, base.class.model_name)
                end
              end
            end
          end
          alias :bind_one :bind

          # Unbinds the base object and the inverse, caused by setting the
          # reference to nil.
          #
          # @example Unbind the document.
          #   person.game.unbind(:continue => true)
          #   person.game = nil
          #
          # @since 2.0.0.rc.1
          def unbind
            unless binding?
              binding do
                target.send(metadata.foreign_key_setter, nil)
                target.send(metadata.inverse_setter, nil)
                if metadata.type
                  target.send(metadata.type_setter, nil)
                end
              end
            end
          end
          alias :unbind_one :unbind
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mongoid-2.2.0 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.9 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.8 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.7 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.6 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.5 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.4 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.3 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.2 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.1 lib/mongoid/relations/bindings/referenced/one.rb
mongoid-2.1.0 lib/mongoid/relations/bindings/referenced/one.rb