Class: ActiveSupport::Callbacks::Callback
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activesupport/lib/active_support/callbacks.rb |
Constant Summary
-
EMPTY_ARRAY =
private
# File 'activesupport/lib/active_support/callbacks.rb', line 308[].freeze
Class Method Summary
Instance Attribute Summary
- #chain_config readonly
- #filter readonly
- #kind rw
- #name rw
Instance Method Summary
Constructor Details
.new(name, filter, kind, options, chain_config) ⇒ Callback
# File 'activesupport/lib/active_support/callbacks.rb', line 245
def initialize(name, filter, kind, , chain_config) @chain_config = chain_config @name = name @kind = kind @filter = filter @if = check_conditionals( [:if]) @unless = check_conditionals( [:unless]) compiled end
Class Method Details
.build(chain, filter, kind, options)
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 231
def self.build(chain, filter, kind, ) 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, , chain.config end
Instance Attribute Details
#chain_config (readonly)
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 243
attr_reader :chain_config, :filter
#filter (readonly)
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 243
attr_reader :chain_config, :filter
#kind (rw)
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 242
attr_accessor :kind, :name
#name (rw)
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 242
attr_accessor :kind, :name
Instance Method Details
#apply(callback_sequence)
Wraps code with filter
# File 'activesupport/lib/active_support/callbacks.rb', line 299
def apply(callback_sequence) compiled.apply(callback_sequence) end
#check_conditionals(conditionals) (private)
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 311
def check_conditionals(conditionals) return EMPTY_ARRAY if conditionals.blank? conditionals = Array(conditionals) if conditionals.any?(String) raise ArgumentError, <<-MSG.squish Passing string to be evaluated in :if and :unless conditional options is not supported. Pass a symbol for an instance method, or a lambda, proc or block, instead. MSG end conditionals.freeze end
#compiled
[ GitHub ]# File 'activesupport/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
#conditions_lambdas (private)
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 326
def conditions_lambdas conditions = @if.map { |c| CallTemplate.build(c, self).make_lambda } + @unless.map { |c| CallTemplate.build(c, self).inverted_lambda } conditions.empty? ? EMPTY_ARRAY : conditions end
#current_scopes
[ GitHub ]# File 'activesupport/lib/active_support/callbacks.rb', line 303
def current_scopes Array(chain_config[:scope]).map { |s| public_send(s) } end
#duplicates?(other) ⇒ Boolean
#matches?(_kind, _filter) ⇒ Boolean
# File 'activesupport/lib/active_support/callbacks.rb', line 268
def matches?(_kind, _filter) @kind == _kind && filter == _filter end