Module: Mongoid::Alize::Macros

Defined in:
lib/mongoid/alize/macros.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alize_from_callbacksObject

Returns the value of attribute alize_from_callbacks



5
6
7
# File 'lib/mongoid/alize/macros.rb', line 5

def alize_from_callbacks
  @alize_from_callbacks
end

#alize_to_callbacksObject

Returns the value of attribute alize_to_callbacks



5
6
7
# File 'lib/mongoid/alize/macros.rb', line 5

def alize_to_callbacks
  @alize_to_callbacks
end

Class Method Details

.==(klass) ⇒ Object



82
83
84
85
# File 'lib/mongoid/alize/macros.rb', line 82

def (many = many.dup).==(klass)
  [Mongoid::Relations::Many,
   Mongoid::Relations::Referenced::Many].map(&:name).include?(klass.name)
end

Instance Method Details

#alize(relation, *fields) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mongoid/alize/macros.rb', line 7

def alize(relation, *fields)
  alize_from(relation, *fields)
   = self.relations[relation.to_s]
  unless _alize_unknown_inverse?()
    .klass.alize_to(.inverse, *fields)
  end
end

#alize_from(relation, *fields) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mongoid/alize/macros.rb', line 15

def alize_from(relation, *fields)
  one, many = _alize_relation_types

  from_one  = Mongoid::Alize::Callbacks::From::One
  from_many = Mongoid::Alize::Callbacks::From::Many

  klass = self
   = klass.relations[relation.to_s]
  relation_superclass = .relation.superclass

  callback_klass =
    case [relation_superclass]
    when [one]  then from_one
    when [many] then from_many
    end

  options = fields.extract_options!
  if options[:fields]
    fields = options[:fields]
  elsif fields.empty? && !_alize_unknown_inverse?()
    fields = .klass.default_alize_fields
  end

  (klass.alize_from_callbacks ||= []) << callback =
    callback_klass.new(klass, relation, fields)
  callback.attach

end

#alize_to(relation, *fields) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongoid/alize/macros.rb', line 44

def alize_to(relation, *fields)
  one, many = _alize_relation_types

  klass = self
   = klass.relations[relation.to_s]
  relation_superclass = .relation.superclass

  options = fields.extract_options!
  if options[:fields]
    fields = options[:fields]
  elsif fields.empty?
    fields = klass.default_alize_fields
  end

  (klass.alize_to_callbacks ||= []) << callback =
    Mongoid::Alize::ToCallback.new(klass, relation, fields)
  callback.attach

end

#default_alize_fieldsObject



64
65
66
67
68
# File 'lib/mongoid/alize/macros.rb', line 64

def default_alize_fields
  self.fields.reject { |name, field|
    name =~ /^_/
  }.keys
end