diff options
author | Genadi Samokovarov <gsamokovarov@gmail.com> | 2013-07-01 12:14:09 +0300 |
---|---|---|
committer | Genadi Samokovarov <gsamokovarov@gmail.com> | 2013-07-01 12:18:25 +0300 |
commit | 269b4638d56081742a2166d23ead95d5c744aa48 (patch) | |
tree | dfe982f5dd5eecdd1facf9794a6458283c5436be | |
parent | 18099b0672ff6df6c649f00d87a92ac28ce8d8e7 (diff) | |
download | rails-269b4638d56081742a2166d23ead95d5c744aa48.tar.gz rails-269b4638d56081742a2166d23ead95d5c744aa48.tar.bz2 rails-269b4638d56081742a2166d23ead95d5c744aa48.zip |
Further clean-up of ActiveSupport::Callbacks
In #11195 I noticed a trailing comma in the docs, but I decided to
further clean it up.
What I have done:
* Clean up the trailing comma in the docs and some extra whitespace
lines.
* Used `Array#extract` options to factor the repetitive pattern of
`args.last.is_a(Hash) ? ...`
* Renamed the local var `config` to `options` in `define_callbacks`, as
`options` seems to be the de facto name for the options objects.
* Renamed the local var `l` to `line` in `define_callback` (maybe it
meant `lambda` in the context) as single `l` may look like `1` in some
fonts.
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 6168188d3b..5c738572a8 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -1,5 +1,6 @@ require 'active_support/concern' require 'active_support/descendants_tracker' +require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/kernel/singleton_class' @@ -542,14 +543,12 @@ module ActiveSupport @callbacks = nil @chain.delete_if { |c| callback.duplicates?(c) } end - end module ClassMethods - def normalize_callback_params(filters, block) # :nodoc: type = CALLBACK_FILTER_TYPES.include?(filters.first) ? filters.shift : :before - options = filters.last.is_a?(Hash) ? filters.pop : {} + options = filters.extract_options! filters.unshift(block) if block [type, filters, options.dup] end @@ -662,7 +661,7 @@ module ActiveSupport # The current object and the return result of the callback will be called # with the lambda. # - # define_callbacks :validate, terminator: ->(target,result) { result == false }, + # define_callbacks :validate, terminator: ->(target, result) { result == false } # # In this example, if any before validate callbacks returns +false+, # other callbacks are not executed. Defaults to +false+, meaning no value @@ -718,17 +717,17 @@ module ActiveSupport # # would call <tt>Audit#save</tt>. def define_callbacks(*names) - config = names.last.is_a?(Hash) ? names.pop : {} - if config.key?(:terminator) && String === config[:terminator] + options = names.extract_options! + if options.key?(:terminator) && String === options[:terminator] ActiveSupport::Deprecation.warn "String based terminators are deprecated, please use a lambda" - value = config[:terminator] - l = class_eval "lambda { |result| #{value} }", __FILE__, __LINE__ - config[:terminator] = lambda { |target, result| target.instance_exec(result, &l) } + value = options[:terminator] + line = class_eval "lambda { |result| #{value} }", __FILE__, __LINE__ + options[:terminator] = lambda { |target, result| target.instance_exec(result, &line) } end names.each do |name| class_attribute "_#{name}_callbacks" - set_callbacks name, CallbackChain.new(name, config) + set_callbacks name, CallbackChain.new(name, options) end end |