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.



346
347
348
349
350
351
352
353
354
# File 'activesupport/lib/active_support/callbacks.rb', line 346

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



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

def chain_config
  @chain_config
end

#kindObject

Returns the value of attribute kind



343
344
345
# File 'activesupport/lib/active_support/callbacks.rb', line 343

def kind
  @kind
end

#nameObject

Returns the value of attribute name



343
344
345
# File 'activesupport/lib/active_support/callbacks.rb', line 343

def name
  @name
end

Class Method Details

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



339
340
341
# File 'activesupport/lib/active_support/callbacks.rb', line 339

def self.build(chain, filter, kind, options)
  new chain.name, filter, kind, options, chain.config
end

Instance Method Details

#apply(next_callback) ⇒ Object

Wraps code with filter



385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'activesupport/lib/active_support/callbacks.rb', line 385

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

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

#duplicates?(other) ⇒ Boolean

Returns:

  • (Boolean)


375
376
377
378
379
380
381
382
# File 'activesupport/lib/active_support/callbacks.rb', line 375

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

#filterObject



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

def filter; @key; end

#matches?(_kind, _filter) ⇒ Boolean

Returns:

  • (Boolean)


371
372
373
# File 'activesupport/lib/active_support/callbacks.rb', line 371

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

#merge(chain, new_options) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
# File 'activesupport/lib/active_support/callbacks.rb', line 359

def merge(chain, new_options)
  options = {
    :if     => @if.dup,
    :unless => @unless.dup
  }

  options[:if].concat     Array(new_options.fetch(:unless, []))
  options[:unless].concat Array(new_options.fetch(:if, []))

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

#raw_filterObject



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

def raw_filter; @filter; end