Sha256: afdf6d8b244a7045e4ec209b91048982f54bdb3a94245492c63f27068ca6c799

Contents?: true

Size: 1.69 KB

Versions: 23

Compression:

Stored size: 1.69 KB

Contents

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

      def initialize(type, name, options = {})
        @type, @name, @options = type, name, options
      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 as?
        !!@options[:as]
      end

      def type_key_name
        @type_key_name ||= many? ? '_type' : "#{as}_type"
      end

      def as
        @options[:as] || self.name
      end

      def foreign_key
        @options[:foreign_key] || "#{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
              if polymorphic?
                ManyPolymorphicProxy
              elsif as?
                ManyDocumentsAsProxy
              else
                ManyProxy
              end
            end
          else
            polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
          end
        end # end begin
      end # end proxy_class
    end
  end
end

Version data entries

23 entries across 23 versions & 6 rubygems

Version Path
crnixon-mongomapper-0.3.4 lib/mongomapper/associations/base.rb
djsun-mongomapper-0.3.5.1 lib/mongomapper/associations/base.rb
djsun-mongomapper-0.3.5.2 lib/mongomapper/associations/base.rb
djsun-mongomapper-0.3.5.4 lib/mongomapper/associations/base.rb
djsun-mongomapper-0.3.5.5 lib/mongomapper/associations/base.rb
djsun-mongomapper-0.3.5 lib/mongomapper/associations/base.rb
jnunemaker-mongomapper-0.3.4 lib/mongomapper/associations/base.rb
jnunemaker-mongomapper-0.3.5 lib/mongomapper/associations/base.rb
mongo_mapper-0.5.6 lib/mongo_mapper/associations/base.rb
mongo_mapper-unstable-2009.10.16 lib/mongo_mapper/associations/base.rb
mongo_mapper-0.5.5 lib/mongo_mapper/associations/base.rb
mongo_mapper-unstable-2009.10.12 lib/mongo_mapper/associations/base.rb
djsun-mongo_mapper-0.5.4.1 lib/mongo_mapper/associations/base.rb
djsun-mongo_mapper-0.5.3.1 lib/mongo_mapper/associations/base.rb
mongo_mapper-0.5.4 lib/mongo_mapper/associations/base.rb
mongo_mapper-0.5.3 lib/mongo_mapper/associations/base.rb
mongo_mapper-unstable-2009.10.11 lib/mongo_mapper/associations/base.rb
djsun-mongo_mapper-0.5.2.1 lib/mongo_mapper/associations/base.rb
mongo_mapper-0.5.2 lib/mongo_mapper/associations/base.rb
djsun-mongo_mapper-0.5.0.1 lib/mongo_mapper/associations/base.rb