Class: ActiveSupport::Callbacks::Callback

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

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, filter, kind, options, chain_config) ⇒ Callback

Returns a new instance of Callback.



245
246
247
248
249
250
251
252
253
254
# File 'lib/active_support/callbacks.rb', line 245

def initialize(name, filter, kind, options, chain_config)
  @chain_config = chain_config
  @name    = name
  @kind    = kind
  @filter  = filter
  @if      = check_conditionals(options[:if])
  @unless  = check_conditionals(options[:unless])

  compiled
end

Instance Attribute Details

#chain_configObject (readonly)

Returns the value of attribute chain_config.



243
244
245
# File 'lib/active_support/callbacks.rb', line 243

def chain_config
  @chain_config
end

#filterObject (readonly)

Returns the value of attribute filter.



243
244
245
# File 'lib/active_support/callbacks.rb', line 243

def filter
  @filter
end

#kindObject

Returns the value of attribute kind.



242
243
244
# File 'lib/active_support/callbacks.rb', line 242

def kind
  @kind
end

#nameObject

Returns the value of attribute name.



242
243
244
# File 'lib/active_support/callbacks.rb', line 242

def name
  @name
end

Class Method Details

.build(chain, filter, kind, options) ⇒ Object



231
232
233
234
235
236
237
238
239
240
# File 'lib/active_support/callbacks.rb', line 231

def self.build(chain, filter, kind, options)
  if filter.is_a?(String)
    raise ArgumentError, <<-MSG.squish
      Passing string to define a callback is not supported. See the `.set_callback`
      documentation to see supported values.
    MSG
  end

  new chain.name, filter, kind, options, chain.config
end

Instance Method Details

#apply(callback_sequence) ⇒ Object

Wraps code with filter



299
300
301
# File 'lib/active_support/callbacks.rb', line 299

def apply(callback_sequence)
  compiled.apply(callback_sequence)
end

#compiledObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/active_support/callbacks.rb', line 281

def compiled
  @compiled ||=
    begin
      user_conditions = conditions_lambdas
      user_callback = CallTemplate.build(@filter, self)

      case kind
      when :before
        Filters::Before.new(user_callback.make_lambda, user_conditions, chain_config, @filter, name)
      when :after
        Filters::After.new(user_callback.make_lambda, user_conditions, chain_config)
      when :around
        Filters::Around.new(user_callback, user_conditions)
      end
    end
end

#current_scopesObject



303
304
305
# File 'lib/active_support/callbacks.rb', line 303

def current_scopes
  Array(chain_config[:scope]).map { |s| public_send(s) }
end

#duplicates?(other) ⇒ Boolean

Returns:

  • (Boolean)


272
273
274
275
276
277
278
279
# File 'lib/active_support/callbacks.rb', line 272

def duplicates?(other)
  case @filter
  when Symbol
    matches?(other.kind, other.filter)
  else
    false
  end
end

#matches?(_kind, _filter) ⇒ Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/active_support/callbacks.rb', line 268

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

#merge_conditional_options(chain, if_option:, unless_option:) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/active_support/callbacks.rb', line 256

def merge_conditional_options(chain, if_option:, unless_option:)
  options = {
    if: @if.dup,
    unless: @unless.dup
  }

  options[:if].concat     Array(unless_option)
  options[:unless].concat Array(if_option)

  self.class.build chain, @filter, @kind, options
end