Class: Mongoid::Alize::ToCallback

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

Instance Attribute Summary

Attributes inherited from Callback

#debug, #denorm_attrs, #inverse_klass, #inverse_metadata, #inverse_relation, #klass, #metadata, #relation

Instance Method Summary collapse

Methods inherited from Callback

#alias_callback, #aliased_callback_name, #callback_attached?, #callback_defined?, #callback_name, #define_denorm_attrs, #denorm_attrs_name, #field_values, #fields_to_s, #force_check, #force_param, #initialize, #to_s

Constructor Details

This class inherits a constructor from Mongoid::Alize::Callback

Instance Method Details

#alias_destroy_callbackObject



139
140
141
142
143
144
# File 'lib/mongoid/alize/to_callback.rb', line 139

def alias_destroy_callback
  unless callback_defined?(aliased_destroy_callback_name)
    klass.send(:alias_method, aliased_destroy_callback_name, destroy_callback_name)
    klass.send(:public, aliased_destroy_callback_name)
  end
end

#aliased_destroy_callback_nameObject



146
147
148
# File 'lib/mongoid/alize/to_callback.rb', line 146

def aliased_destroy_callback_name
  "denormalize_destroy_#{direction}_#{relation}"
end

#attachObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mongoid/alize/to_callback.rb', line 5

def attach
  define_denorm_attrs

  define_callback
  alias_callback
  set_callback

  define_destroy_callback
  alias_destroy_callback
  set_destroy_callback
end

#define_callbackObject



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
43
44
45
46
47
# File 'lib/mongoid/alize/to_callback.rb', line 17

def define_callback
  klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

    def #{callback_name}#{force_param}

      #{iterable_relation}.each do |relation|
        next if relation.attributes.frozen?

        is_one = #{is_one?}
        if is_one
          field_values = #{field_values("self")}
        else
          field_values = #{field_values("self", :id => true)}
        end

        prefixed_name = #{prefixed_name}
        if is_one
          #{relation_set('prefixed_name', 'field_values')}
        else
          #{pull_from_inverse}
          #{relation_push('prefixed_name', 'field_values')}
        end

      end

      #{debug ? "puts \"#{callback_name}\"": ""}
      true
    end
    protected :#{callback_name}
  CALLBACK
end

#define_destroy_callbackObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mongoid/alize/to_callback.rb', line 49

def define_destroy_callback
  klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

    def #{destroy_callback_name}
      #{iterable_relation}.each do |relation|
        next if relation.attributes.frozen?

        is_one = #{is_one?}
        prefixed_name = #{prefixed_name}
        if is_one
          #{relation_set('prefixed_name', 'nil')}
        else
          #{pull_from_inverse}
        end
      end

      #{debug ? "puts \"#{destroy_callback_name}\"": ""}
      true
    end
    protected :#{destroy_callback_name}
  CALLBACK
end

#destroy_callback_nameObject



150
151
152
# File 'lib/mongoid/alize/to_callback.rb', line 150

def destroy_callback_name
  "_#{aliased_destroy_callback_name}"
end

#directionObject



154
155
156
# File 'lib/mongoid/alize/to_callback.rb', line 154

def direction
  "to"
end

#find_relationObject



119
120
121
# File 'lib/mongoid/alize/to_callback.rb', line 119

def find_relation
  "relation.class.relations.values.find { |metadata| metadata.inverse(self) == :#{relation} && metadata.class_name == self.class.name }"
end

#is_one?Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mongoid/alize/to_callback.rb', line 105

def is_one?
  if inverse_relation
    if self..relation.superclass == Mongoid::Relations::One
      "true"
    else
      "false"
    end
  else
    <<-RUBIES
      (#{find_relation}.relation.superclass == Mongoid::Relations::One)
    RUBIES
  end
end

#iterable_relationObject



123
124
125
# File 'lib/mongoid/alize/to_callback.rb', line 123

def iterable_relation
  "[self.#{relation}].flatten.compact"
end

#mongoid_four_or_newer?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/mongoid/alize/to_callback.rb', line 158

def mongoid_four_or_newer?
  Mongoid::VERSION.split('.').first.to_i >= 4
end

#prefixed_nameObject



83
84
85
86
87
88
89
90
91
# File 'lib/mongoid/alize/to_callback.rb', line 83

def prefixed_name
  if inverse_relation
    ":#{inverse_relation}_fields"
  else
    <<-RUBIES
      (#{find_relation}.name.to_s + '_fields')
    RUBIES
  end
end

#pull_from_inverseObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/mongoid/alize/to_callback.rb', line 72

def pull_from_inverse
  <<-RUBIES
    #{relation_pull('prefixed_name', '{ "_id" => self.id }')}
    if _f = relation.send(prefixed_name)
      _f.reject! do |hash|
        hash["_id"] == self.id
      end
    end
  RUBIES
end

#relation_pull(field, value) ⇒ Object



97
98
99
# File 'lib/mongoid/alize/to_callback.rb', line 97

def relation_pull(field, value)
  mongoid_four_or_newer? ? "relation.pull(#{field} => #{value})" : "relation.pull(#{field}, #{value})"
end

#relation_push(field, value) ⇒ Object



101
102
103
# File 'lib/mongoid/alize/to_callback.rb', line 101

def relation_push(field, value)
  mongoid_four_or_newer? ? "relation.push(#{field} => #{value})" : "relation.push(#{field}, #{value})"
end

#relation_set(field, value) ⇒ Object



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

def relation_set(field, value)
  mongoid_four_or_newer? ? "relation.set(#{field} => #{value})" : "relation.set(#{field}, #{value})"
end

#set_callbackObject



127
128
129
130
131
# File 'lib/mongoid/alize/to_callback.rb', line 127

def set_callback
  unless callback_attached?("save", aliased_callback_name)
    klass.set_callback(:save, :after, aliased_callback_name)
  end
end

#set_destroy_callbackObject



133
134
135
136
137
# File 'lib/mongoid/alize/to_callback.rb', line 133

def set_destroy_callback
  unless callback_attached?("destroy", aliased_destroy_callback_name)
    klass.set_callback(:destroy, :after, aliased_destroy_callback_name)
  end
end