Class: Mongoid::Alize::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/alize/callback.rb

Direct Known Subclasses

FromCallback, ToCallback

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_klass, _relation, _denorm_attrs) ⇒ Callback

Returns a new instance of Callback.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/alize/callback.rb', line 17

def initialize(_klass, _relation, _denorm_attrs)
  self.debug = ENV["ALIZE_DEBUG"]

  self.klass = _klass
  self.relation = _relation
  self.denorm_attrs = _denorm_attrs

  self. = _klass.relations[_relation.to_s]
  if !(self..polymorphic? &&
        self..stores_foreign_key?)
    self.inverse_klass = self..klass
    self.inverse_relation = self..inverse
    self. = self.inverse_klass.relations[inverse_relation.to_s]
  end
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



15
16
17
# File 'lib/mongoid/alize/callback.rb', line 15

def debug
  @debug
end

#denorm_attrsObject

Returns the value of attribute denorm_attrs.



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

def denorm_attrs
  @denorm_attrs
end

#inverse_klassObject

Returns the value of attribute inverse_klass.



11
12
13
# File 'lib/mongoid/alize/callback.rb', line 11

def inverse_klass
  @inverse_klass
end

#inverse_metadataObject

Returns the value of attribute inverse_metadata.



13
14
15
# File 'lib/mongoid/alize/callback.rb', line 13

def 
  @inverse_metadata
end

#inverse_relationObject

Returns the value of attribute inverse_relation.



12
13
14
# File 'lib/mongoid/alize/callback.rb', line 12

def inverse_relation
  @inverse_relation
end

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/mongoid/alize/callback.rb', line 7

def klass
  @klass
end

#metadataObject

Returns the value of attribute metadata.



9
10
11
# File 'lib/mongoid/alize/callback.rb', line 9

def 
  @metadata
end

#relationObject

Returns the value of attribute relation.



8
9
10
# File 'lib/mongoid/alize/callback.rb', line 8

def relation
  @relation
end

Instance Method Details

#alias_callbackObject



46
47
48
49
50
51
# File 'lib/mongoid/alize/callback.rb', line 46

def alias_callback
  unless callback_defined?(aliased_callback_name)
    klass.send(:alias_method, aliased_callback_name, callback_name)
    klass.send(:public, aliased_callback_name)
  end
end

#aliased_callback_nameObject



57
58
59
# File 'lib/mongoid/alize/callback.rb', line 57

def aliased_callback_name
  "denormalize_#{direction}_#{relation}"
end

#attachObject



33
34
35
# File 'lib/mongoid/alize/callback.rb', line 33

def attach
  # implement in subclasses
end

#callback_attached?(callback_type, callback_name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/mongoid/alize/callback.rb', line 37

def callback_attached?(callback_type, callback_name)
  !!klass.send(:"_#{callback_type}_callbacks").
    map(&:raw_filter).include?(callback_name)
end

#callback_defined?(callback_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/mongoid/alize/callback.rb', line 42

def callback_defined?(callback_name)
  klass.method_defined?(callback_name)
end

#callback_nameObject



53
54
55
# File 'lib/mongoid/alize/callback.rb', line 53

def callback_name
  "_#{aliased_callback_name}"
end

#define_denorm_attrsObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoid/alize/callback.rb', line 61

def define_denorm_attrs
  _denorm_attrs = denorm_attrs
  if denorm_attrs.is_a?(Proc)
    klass.send(:define_method, denorm_attrs_name) do |inverse|
      self.instance_exec(inverse, &_denorm_attrs).map(&:to_s)
    end
  else
    klass.send(:define_method, denorm_attrs_name) do |inverse|
      _denorm_attrs.map(&:to_s)
    end
  end
end

#denorm_attrs_nameObject



74
75
76
# File 'lib/mongoid/alize/callback.rb', line 74

def denorm_attrs_name
  "#{callback_name}_attrs"
end

#field_values(source, options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/mongoid/alize/callback.rb', line 78

def field_values(source, options={})
  extras = options[:id] ? "['_id']" : "[]"
  <<-RUBY
    value = (#{denorm_attrs_name}(#{source}) + #{extras}).inject({}) do |hash, name|
      hash[name] = #{source}.send(name)
      hash
    end
    value.respond_to?(:mongoize) ? value.mongoize : value
  RUBY
end

#fields_to_sObject



97
98
99
100
101
102
103
# File 'lib/mongoid/alize/callback.rb', line 97

def fields_to_s
  if fields.is_a?(Proc)
    "Proc Given"
  else
    fields.join(", ")
  end
end

#force_checkObject



93
94
95
# File 'lib/mongoid/alize/callback.rb', line 93

def force_check
  "force || self.force_denormalization"
end

#force_paramObject



89
90
91
# File 'lib/mongoid/alize/callback.rb', line 89

def force_param
  "(force=false)"
end

#to_sObject



105
106
107
108
109
110
111
# File 'lib/mongoid/alize/callback.rb', line 105

def to_s
  "#{self.class.name}" +
  "\nModel: #{self.klass}, Relation: #{self.relation}" + (self..polymorphic? ?
  "\nPolymorphic" :
  "\nInverse: #{self.inverse_klass}, Relation: #{self.inverse_relation}") +
  "\nFields: #{fields_to_s}"
end