Sha256: 5f852287643d21174245ce6dc2150cc885768f9a6e1be200624e441a0d1928af

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

module MongoMapper
  module Associations
    class Base
      attr_reader :type, :name, :options

      def initialize(type, name, options = {})
        @options = options
        @type = type
        @name = name
      end
      
      def class_name
        @class_name ||= begin
          if cn = options[:class_name]
            cn
          elsif many?
            name.to_s.singularize.camelize
          else
            name.to_s.camelize
          end
        end
      end
      
      def klass
        @klass ||= class_name.constantize
      end
      
      def many?
        @many_type ||= @type == :many
      end
      
      def belongs_to?
        @belongs_to_type ||= @type == :belongs_to
      end
      
      def polymorphic?
        !!@options[:polymorphic]
      end
      
      def type_key_name
        @type_key_name ||= many? ? '_type' : "#{name}_type"
      end
      
      def belongs_to_key_name
        "#{name}_id"
      end
      
      def ivar
        @ivar ||= "@_#{name}"
      end
      
      def embeddable?
        many? && klass.embeddable?
      end

      def proxy_class
        @proxy_class ||= begin
          if many?
            if self.klass.embeddable?
              polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
            else
              polymorphic? ? ManyPolymorphicProxy : ManyProxy
            end
          else
            polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
          end
        end # end begin
      end # end proxy_class
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
djsun-mongomapper-0.3.1.1 lib/mongomapper/associations/base.rb
djsun-mongomapper-0.3.1 lib/mongomapper/associations/base.rb
jnunemaker-mongomapper-0.3.2 lib/mongomapper/associations/base.rb
jnunemaker-mongomapper-0.3.3 lib/mongomapper/associations/base.rb