Sha256: f10e2d5d3c222f54f91cd843cc2421d7802139b233f50235548765d76147d94d

Contents?: true

Size: 1.48 KB

Versions: 5

Compression:

Stored size: 1.48 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
          @properties = Proc.new if block_given?
          @properties
        end

        def self.methods(&block)
          @methods = Proc.new if block_given?
          @methods
        end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mongomodel-0.4.1 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.4.0 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.3.6 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.3.5 lib/mongomodel/concerns/associations/base/definition.rb
mongomodel-0.3.4 lib/mongomodel/concerns/associations/base/definition.rb