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.


311
312
313
314
315
316
317
318
319
# File 'activesupport/lib/active_support/callbacks.rb', line 311

def initialize(name, filter, kind, options, chain_config)
  @chain_config  = chain_config
  @name    = name
  @kind    = kind
  @filter  = filter
  @key     = compute_identifier filter
  @if      = Array(options[:if])
  @unless  = Array(options[:unless])
end

Instance Attribute Details

#chain_configObject (readonly)

Returns the value of attribute chain_config


309
310
311
# File 'activesupport/lib/active_support/callbacks.rb', line 309

def chain_config
  @chain_config
end

#kindObject

Returns the value of attribute kind


308
309
310
# File 'activesupport/lib/active_support/callbacks.rb', line 308

def kind
  @kind
end

#nameObject

Returns the value of attribute name


308
309
310
# File 'activesupport/lib/active_support/callbacks.rb', line 308

def name
  @name
end

Class Method Details

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


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

def self.build(chain, filter, kind, options)
  if filter.is_a?(String)
    ActiveSupport::Deprecation.warn(<<-MSG.squish)
      Passing string to define callback is deprecated and will be removed
      in Rails 5.1 without replacement.
    MSG
  end

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

Instance Method Details

#apply(callback_sequence) ⇒ Object

Wraps code with filter


350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'activesupport/lib/active_support/callbacks.rb', line 350

def apply(callback_sequence)
  user_conditions = conditions_lambdas
  user_callback = make_lambda @filter

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

#duplicates?(other) ⇒ Boolean

Returns:

  • (Boolean)

340
341
342
343
344
345
346
347
# File 'activesupport/lib/active_support/callbacks.rb', line 340

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

#filterObject


321
# File 'activesupport/lib/active_support/callbacks.rb', line 321

def filter; @key; end

#matches?(_kind, _filter) ⇒ Boolean

Returns:

  • (Boolean)

336
337
338
# File 'activesupport/lib/active_support/callbacks.rb', line 336

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

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


324
325
326
327
328
329
330
331
332
333
334
# File 'activesupport/lib/active_support/callbacks.rb', line 324

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

#raw_filterObject


322
# File 'activesupport/lib/active_support/callbacks.rb', line 322

def raw_filter; @filter; end