Class: ActiveSupport::Callbacks::Callback

Inherits:
Object
  • Object
show all
Defined in:
activesupport/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.



296
297
298
299
300
301
302
303
# File 'activesupport/lib/active_support/callbacks.rb', line 296

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])
end

Instance Attribute Details

#chain_configObject (readonly)

Returns the value of attribute chain_config



294
295
296
# File 'activesupport/lib/active_support/callbacks.rb', line 294

def chain_config
  @chain_config
end

#filterObject (readonly)

Returns the value of attribute filter



294
295
296
# File 'activesupport/lib/active_support/callbacks.rb', line 294

def filter
  @filter
end

#kindObject

Returns the value of attribute kind



293
294
295
# File 'activesupport/lib/active_support/callbacks.rb', line 293

def kind
  @kind
end

#nameObject

Returns the value of attribute name



293
294
295
# File 'activesupport/lib/active_support/callbacks.rb', line 293

def name
  @name
end

Class Method Details

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



282
283
284
285
286
287
288
289
290
291
# File 'activesupport/lib/active_support/callbacks.rb', line 282

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



331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'activesupport/lib/active_support/callbacks.rb', line 331

def apply(callback_sequence)
  user_conditions = conditions_lambdas
  user_callback = CallTemplate.build(@filter, self)

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

#current_scopesObject



345
346
347
# File 'activesupport/lib/active_support/callbacks.rb', line 345

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

#duplicates?(other) ⇒ Boolean

Returns:

  • (Boolean)


321
322
323
324
325
326
327
328
# File 'activesupport/lib/active_support/callbacks.rb', line 321

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

#matches?(_kind, _filter) ⇒ Boolean

Returns:

  • (Boolean)


317
318
319
# File 'activesupport/lib/active_support/callbacks.rb', line 317

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

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



305
306
307
308
309
310
311
312
313
314
315
# File 'activesupport/lib/active_support/callbacks.rb', line 305

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