Class: MongoModel::Associations::Base::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomodel/concerns/associations/base/definition.rb

Direct Known Subclasses

MongoModel::Associations::BelongsTo, HasManyByForeignKey, HasManyByIds

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Definition) initialize(owner, name, options = {})

Returns a new instance of Definition



7
8
9
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 7

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

Instance Attribute Details

- (Object) name (readonly)

Returns the value of attribute name



5
6
7
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 5

def name
  @name
end

- (Object) options (readonly)

Returns the value of attribute options



5
6
7
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 5

def options
  @options
end

- (Object) owner (readonly)

Returns the value of attribute owner



5
6
7
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 5

def owner
  @owner
end

Class Method Details

+ (Object) methods(&block)



58
59
60
61
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 58

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

+ (Object) properties



53
54
55
56
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 53

def self.properties
  @properties = Proc.new if block_given?
  @properties
end

Instance Method Details

- (Boolean) collection?

Returns:



41
42
43
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 41

def collection?
  true
end

- (Object) define!



15
16
17
18
19
20
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 15

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

- (Object) for(instance)



11
12
13
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 11

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

- (Object) klass



22
23
24
25
26
27
28
29
30
31
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 22

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

- (Boolean) polymorphic?

Returns:



37
38
39
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 37

def polymorphic?
  options[:polymorphic]
end

- (Object) scope



45
46
47
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 45

def scope
  klass.scoped.apply_finder_options(scope_options)
end

- (Object) scope_options



49
50
51
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 49

def scope_options
  options.slice(:conditions, :select, :offset, :limit, :order)
end

- (Object) singular_name



33
34
35
# File 'lib/mongomodel/concerns/associations/base/definition.rb', line 33

def singular_name
  name.to_s.singularize
end