Class: CoreExt::Callbacks::Callback

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



299
300
301
302
303
304
305
306
307
# File 'lib/core_ext/callbacks.rb', line 299

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.



297
298
299
# File 'lib/core_ext/callbacks.rb', line 297

def chain_config
  @chain_config
end

#kindObject

Returns the value of attribute kind.



296
297
298
# File 'lib/core_ext/callbacks.rb', line 296

def kind
  @kind
end

#nameObject

Returns the value of attribute name.



296
297
298
# File 'lib/core_ext/callbacks.rb', line 296

def name
  @name
end

Class Method Details

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



292
293
294
# File 'lib/core_ext/callbacks.rb', line 292

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

Instance Method Details

#apply(callback_sequence) ⇒ Object

Wraps code with filter



338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/core_ext/callbacks.rb', line 338

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)


328
329
330
331
332
333
334
335
# File 'lib/core_ext/callbacks.rb', line 328

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

#filterObject



309
# File 'lib/core_ext/callbacks.rb', line 309

def filter; @key; end

#matches?(_kind, _filter) ⇒ Boolean

Returns:

  • (Boolean)


324
325
326
# File 'lib/core_ext/callbacks.rb', line 324

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

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



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/core_ext/callbacks.rb', line 312

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



310
# File 'lib/core_ext/callbacks.rb', line 310

def raw_filter; @filter; end