diff options
author | Pavel Gorbokon <pahanix@gmail.com> | 2010-12-07 19:00:49 +0200 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2010-12-07 20:56:10 -0200 |
commit | 1ce9b73e5c7cbf6c29c57bb60c97eb2d2c10801d (patch) | |
tree | c2506f6998c202bedbadc2a68592de956e5172d1 /activesupport | |
parent | 4c4c7a272fdcec12691ccf48b5eefce49b7bac8d (diff) | |
download | rails-1ce9b73e5c7cbf6c29c57bb60c97eb2d2c10801d.tar.gz rails-1ce9b73e5c7cbf6c29c57bb60c97eb2d2c10801d.tar.bz2 rails-1ce9b73e5c7cbf6c29c57bb60c97eb2d2c10801d.zip |
Replace nested ifs with case/when
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 90 |
1 files changed, 44 insertions, 46 deletions
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 4dcbbb819a..844237fe6a 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -178,49 +178,48 @@ module ActiveSupport # options[0] is the compiled form of supplied conditions # options[1] is the "end" for the conditional # - if @kind == :before || @kind == :around - if @kind == :before - # if condition # before_save :filter_name, :if => :condition - # filter_name - # end - filter = <<-RUBY_EVAL - unless halted - result = #{@filter} - halted = (#{chain.config[:terminator]}) - end - RUBY_EVAL - - [@compiled_options[0], filter, @compiled_options[1]].compact.join("\n") - else - # Compile around filters with conditions into proxy methods - # that contain the conditions. - # - # For `around_save :filter_name, :if => :condition': - # - # def _conditional_callback_save_17 - # if condition - # filter_name do - # yield self - # end - # else - # yield self - # end - # end - # - name = "_conditional_callback_#{@kind}_#{next_id}" - @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 - def #{name}(halted) - #{@compiled_options[0] || "if true"} && !halted - #{@filter} do - yield self - end - else + case @kind + when :before + # if condition # before_save :filter_name, :if => :condition + # filter_name + # end + filter = <<-RUBY_EVAL + unless halted + result = #{@filter} + halted = (#{chain.config[:terminator]}) + end + RUBY_EVAL + + [@compiled_options[0], filter, @compiled_options[1]].compact.join("\n") + when :around + # Compile around filters with conditions into proxy methods + # that contain the conditions. + # + # For `around_save :filter_name, :if => :condition': + # + # def _conditional_callback_save_17 + # if condition + # filter_name do + # yield self + # end + # else + # yield self + # end + # end + # + name = "_conditional_callback_#{@kind}_#{next_id}" + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{name}(halted) + #{@compiled_options[0] || "if true"} && !halted + #{@filter} do yield self end + else + yield self end - RUBY_EVAL - "#{name}(halted) do" - end + end + RUBY_EVAL + "#{name}(halted) do" end end @@ -229,15 +228,14 @@ module ActiveSupport def end(key=nil, object=nil) return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?") - if @kind == :around || @kind == :after + case @kind + when :after # if condition # after_save :filter_name, :if => :condition # filter_name # end - if @kind == :after - [@compiled_options[0], @filter, @compiled_options[1]].compact.join("\n") - else - "end" - end + [@compiled_options[0], @filter, @compiled_options[1]].compact.join("\n") + when :around + "end" end end |