Class: ActiveSupport::Callbacks::Callback

Inherits:
Object
  • Object
show all
Defined in:
activesupport/lib/active_support/callbacks.rb

Overview

:nodoc:#

Constant Summary collapse

@@_callback_sequence =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chain, filter, kind, options, klass) ⇒ Callback



89
90
91
92
93
94
95
96
97
98
99
100
# File 'activesupport/lib/active_support/callbacks.rb', line 89

def initialize(chain, filter, kind, options, klass)
  @chain, @kind, @klass = chain, kind, klass
  normalize_options!(options)

  @per_key              = options.delete(:per_key)
  @raw_filter, @options = filter, options
  @filter               = _compile_filter(filter)
  @compiled_options     = _compile_options(options)
  @callback_id          = next_id

  _compile_per_key_options
end

Instance Attribute Details

#chainObject

Returns the value of attribute chain



87
88
89
# File 'activesupport/lib/active_support/callbacks.rb', line 87

def chain
  @chain
end

#filterObject

Returns the value of attribute filter



87
88
89
# File 'activesupport/lib/active_support/callbacks.rb', line 87

def filter
  @filter
end

#kindObject

Returns the value of attribute kind



87
88
89
# File 'activesupport/lib/active_support/callbacks.rb', line 87

def kind
  @kind
end

#klassObject

Returns the value of attribute klass



87
88
89
# File 'activesupport/lib/active_support/callbacks.rb', line 87

def klass
  @klass
end

#optionsObject

Returns the value of attribute options



87
88
89
# File 'activesupport/lib/active_support/callbacks.rb', line 87

def options
  @options
end

#per_keyObject

Returns the value of attribute per_key



87
88
89
# File 'activesupport/lib/active_support/callbacks.rb', line 87

def per_key
  @per_key
end

#raw_filterObject

Returns the value of attribute raw_filter



87
88
89
# File 'activesupport/lib/active_support/callbacks.rb', line 87

def raw_filter
  @raw_filter
end

Instance Method Details

#_compile_per_key_optionsObject



151
152
153
154
155
156
157
158
159
# File 'activesupport/lib/active_support/callbacks.rb', line 151

def _compile_per_key_options
  key_options  = _compile_options(@per_key)

  @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    def _one_time_conditions_valid_#{@callback_id}?
      true #{key_options[0]}
    end
  RUBY_EVAL
end

#_update_filter(filter_options, new_options) ⇒ Object



136
137
138
139
# File 'activesupport/lib/active_support/callbacks.rb', line 136

def _update_filter(filter_options, new_options)
  filter_options[:if].push(new_options[:unless]) if new_options.key?(:unless)
  filter_options[:unless].push(new_options[:if]) if new_options.key?(:if)
end

#clone(chain, klass) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'activesupport/lib/active_support/callbacks.rb', line 102

def clone(chain, klass)
  obj                  = super()
  obj.chain            = chain
  obj.klass            = klass
  obj.per_key          = @per_key.dup
  obj.options          = @options.dup
  obj.per_key[:if]     = @per_key[:if].dup
  obj.per_key[:unless] = @per_key[:unless].dup
  obj.options[:if]     = @options[:if].dup
  obj.options[:unless] = @options[:unless].dup
  obj
end

#end(key = nil, object = nil) ⇒ Object

This will supply contents for around and after filters, but not before filters (for the backward pass).



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'activesupport/lib/active_support/callbacks.rb', line 220

def end(key=nil, object=nil)
  return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?")

  case @kind
  when :after
    # if condition    # after_save :filter_name, :if => :condition
    #   filter_name
    # end
    [@compiled_options[0], @filter, @compiled_options[1]].compact.join("\n")
  when :around
    <<-RUBY_EVAL
      value
    end
    RUBY_EVAL
  end
end

#matches?(_kind, _filter) ⇒ Boolean



132
133
134
# File 'activesupport/lib/active_support/callbacks.rb', line 132

def matches?(_kind, _filter)
  @kind == _kind && @filter == _filter
end

#nameObject



124
125
126
# File 'activesupport/lib/active_support/callbacks.rb', line 124

def name
  chain.name
end

#next_idObject



128
129
130
# File 'activesupport/lib/active_support/callbacks.rb', line 128

def next_id
  @@_callback_sequence += 1
end

#normalize_options!(options) ⇒ Object



115
116
117
118
119
120
121
122
# File 'activesupport/lib/active_support/callbacks.rb', line 115

def normalize_options!(options)
  options[:if] = Array.wrap(options[:if])
  options[:unless] = Array.wrap(options[:unless])

  options[:per_key] ||= {}
  options[:per_key][:if] = Array.wrap(options[:per_key][:if])
  options[:per_key][:unless] = Array.wrap(options[:per_key][:unless])
end

#recompile!(_options, _per_key) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'activesupport/lib/active_support/callbacks.rb', line 141

def recompile!(_options, _per_key)
  _update_filter(self.options, _options)
  _update_filter(self.per_key, _per_key)

  @callback_id      = next_id
  @filter           = _compile_filter(@raw_filter)
  @compiled_options = _compile_options(@options)
                      _compile_per_key_options
end

#start(key = nil, object = nil) ⇒ Object

This will supply contents for before and around filters, and no contents for after filters (for the forward pass).



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'activesupport/lib/active_support/callbacks.rb', line 163

def start(key=nil, object=nil)
  return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?")

  # options[0] is the compiled form of supplied conditions
  # options[1] is the "end" for the conditional
  #
  case @kind
  when :before
    # if condition    # before_save :filter_name, :if => :condition
    #   filter_name
    # end
    filter = <<-RUBY_EVAL
      unless halted
        # This double assignment is to prevent warnings in 1.9.3.  I would
        # remove the `result` variable, but apparently some other
        # generated code is depending on this variable being set sometimes
        # and sometimes not.
        result = result = #{@filter}
        halted = (#{chain.config[:terminator]})
      end
    RUBY_EVAL

    [@compiled_options[0], filter, @compiled_options[1]].compact.join("\n")
  when :around
    # Compile around filters with conditions into proxy methods
    # that contain the conditions.
    #
    # For `around_save :filter_name, :if => :condition':
    #
    # def _conditional_callback_save_17
    #   if condition
    #     filter_name do
    #       yield self
    #     end
    #   else
    #     yield self
    #   end
    # end
    #
    name = "_conditional_callback_#{@kind}_#{next_id}"
    @klass.class_eval <<-RUBY_EVAL,  __FILE__, __LINE__ + 1
       def #{name}(halted)
        #{@compiled_options[0] || "if true"} && !halted
          #{@filter} do
            yield self
          end
        else
          yield self
        end
      end
    RUBY_EVAL
    "#{name}(halted) do"
  end
end