Sha256: 7c3244013dd2e35fa975aa568891679a5ebc13ac9da1d97e994ad97f7e7b354e

Contents?: true

Size: 1.58 KB

Versions: 25

Compression:

Stored size: 1.58 KB

Contents

module MongoModel
  module Associations
    module Base
      class Definition
        attr_reader :owner, :name, :options

        def initialize(owner, name, options={})
          @owner, @name, @options = owner, name, options
        end

        def for(instance)
          association_class.new(self, instance)
        end

        def define!
          owner.instance_exec(self, &self.class.properties) if self.class.properties
          owner.instance_exec(self, &self.class.methods) if self.class.methods

          self
        end

        def klass
          case options[:class]
          when Class
            options[:class]
          when String
            options[:class].constantize
          else
            name.to_s.classify.constantize
          end
        end
        
        def singular_name
          name.to_s.singularize
        end

        def polymorphic?
          options[:polymorphic]
        end
        
        def scope
          klass.scoped.apply_finder_options(scope_options)
        end
        
        def scope_options
          options.slice(:conditions, :select, :offset, :limit, :order)
        end

        def self.properties(&block)
          block_given? ? write_inheritable_attribute(:properties, block) : read_inheritable_attribute(:properties)
        end

        def self.methods(&block)
          block_given? ? write_inheritable_attribute(:methods, block) : read_inheritable_attribute(:methods)
        end

      private
        def association_class
          self.class::Association rescue Base::Association
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
mongomodel-0.3.3 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.3.2 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.3.1 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.3.0 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.20 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.19 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.18 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.17 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.16 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.15 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.14 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.13 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.12 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.11 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.10 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.9 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.8 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.7 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.6 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.2.5 lib/mongomodel/concerns/associations/base/definition.rb