Sha256: 939efa041afbac931c59c508049625fb875594467e6667579877fa836d0f33fd

Contents?: true

Size: 1.56 KB

Versions: 33

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8
module Mongoid
  module Relations

    # Superclass for all builder objects. Builders are responsible for either
    # looking up a relation's target from the database, or creating them from a
    # supplied attributes hash.
    class Builder
      include Threaded::Lifecycle

      attr_reader :base, :metadata, :object

      # Instantiate the new builder for a relation.
      #
      # @example Create the builder.
      #   Builder.new(metadata, { :field => "value })
      #
      # @param [ Document ] base The base document.
      # @param [ Metdata ] metadata The metadata for the relation.
      # @param [ Hash, Moped::BSON::ObjectId ] object The attributes to build from or
      #   id to query with.
      #
      # @since 2.0.0.rc.1
      def initialize(base, metadata, object)
        @base, @metadata, @object = base, metadata, object
      end

      protected

      # Get the class from the metadata.
      #
      # @example Get the class.
      #   builder.klass
      #
      # @return [ Class ] The class from the metadata.
      #
      # @since 2.3.2
      def klass
        @klass ||= metadata.klass
      end

      # Do we need to perform a database query? It will be so if the object we
      # have is not a document.
      #
      # @example Should we query the database?
      #   builder.query?
      #
      # @return [ true, false ] Whether a database query should happen.
      #
      # @since 2.0.0.rc.1
      def query?
        obj = object.__array__.first
        !obj.is_a?(Mongoid::Document) && !obj.nil?
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 4 rubygems

Version Path
mongoid-3.1.7 lib/mongoid/relations/builder.rb
mongoid-3.1.6 lib/mongoid/relations/builder.rb
mongoid-3.1.5 lib/mongoid/relations/builder.rb
mongoid_heroku_stable-4.0.0 lib/mongoid/relations/builder.rb
mongoid_rails4-4.0.0 lib/mongoid/relations/builder.rb
mongoid-3.1.4 lib/mongoid/relations/builder.rb
mongoid-3.1.3 lib/mongoid/relations/builder.rb
mongoid-3.1.2 lib/mongoid/relations/builder.rb
mongoid-3.0.23 lib/mongoid/relations/builder.rb
mongoid-3.1.1 lib/mongoid/relations/builder.rb
mongoid-3.1.0 lib/mongoid/relations/builder.rb
mongoid-3.0.22 lib/mongoid/relations/builder.rb
mongoid-3.0.21 lib/mongoid/relations/builder.rb
mongoid-3.0.20 lib/mongoid/relations/builder.rb
mongoid-3.0.19 lib/mongoid/relations/builder.rb
mongoid-3.0.18 lib/mongoid/relations/builder.rb
mongoid-3.0.17 lib/mongoid/relations/builder.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.16/lib/mongoid/relations/builder.rb
mongoid-3.0.16 lib/mongoid/relations/builder.rb
mongoid-3.0.15 lib/mongoid/relations/builder.rb