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



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

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



152
153
154
# File 'lib/mongoid/alize/to_callback.rb', line 152

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

#attachObject



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

def attach
  define_denorm_attrs

  define_callback
  alias_callback
  set_callback

  define_destroy_callback
  alias_destroy_callback
  set_destroy_callback
end

#define_callbackObject



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
48
49
# File 'lib/mongoid/alize/to_callback.rb', line 19

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



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

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_unset('prefixed_name')}
        else
          #{pull_from_inverse}
        end
      end

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

#destroy_callback_nameObject



156
157
158
# File 'lib/mongoid/alize/to_callback.rb', line 156

def destroy_callback_name
  :"_#{aliased_destroy_callback_name}"
end

#directionObject



160
161
162
# File 'lib/mongoid/alize/to_callback.rb', line 160

def direction
  "to"
end

#find_relationObject



125
126
127
# File 'lib/mongoid/alize/to_callback.rb', line 125

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)


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mongoid/alize/to_callback.rb', line 111

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



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

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

#prefixed_nameObject



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

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

#pull_from_inverseObject



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

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



103
104
105
# File 'lib/mongoid/alize/to_callback.rb', line 103

def relation_pull(field, value)
  Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.pull(#{field}.to_sym => #{value})" : "relation.pull(#{field}, #{value})"
end

#relation_push(field, value) ⇒ Object



107
108
109
# File 'lib/mongoid/alize/to_callback.rb', line 107

def relation_push(field, value)
  Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.push(#{field}.to_sym => #{value})" : "relation.push(#{field}, #{value})"
end

#relation_set(field, value) ⇒ Object



95
96
97
# File 'lib/mongoid/alize/to_callback.rb', line 95

def relation_set(field, value)
  Mongoid::Compatibility::Version.mongoid4_or_newer? ? "relation.set(#{field}.to_sym => #{value})" : "relation.set(#{field}, #{value})"
end

#relation_unset(field) ⇒ Object



99
100
101
# File 'lib/mongoid/alize/to_callback.rb', line 99

def relation_unset(field)
  "relation.unset(#{field}.to_sym)"
end

#set_callbackObject



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

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

#set_destroy_callbackObject



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

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