1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Replace nested ifs with case/when

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
This commit is contained in:
Pavel Gorbokon 2010-12-07 19:00:49 +02:00 committed by Santiago Pastorino
parent 4c4c7a272f
commit 1ce9b73e5c

View file

@ -178,49 +178,48 @@ module ActiveSupport
# options[0] is the compiled form of supplied conditions # options[0] is the compiled form of supplied conditions
# options[1] is the "end" for the conditional # options[1] is the "end" for the conditional
# #
if @kind == :before || @kind == :around case @kind
if @kind == :before when :before
# if condition # before_save :filter_name, :if => :condition # if condition # before_save :filter_name, :if => :condition
# filter_name # filter_name
# end # end
filter = <<-RUBY_EVAL filter = <<-RUBY_EVAL
unless halted unless halted
result = #{@filter} result = #{@filter}
halted = (#{chain.config[:terminator]}) halted = (#{chain.config[:terminator]})
end end
RUBY_EVAL RUBY_EVAL
[@compiled_options[0], filter, @compiled_options[1]].compact.join("\n") [@compiled_options[0], filter, @compiled_options[1]].compact.join("\n")
else when :around
# Compile around filters with conditions into proxy methods # Compile around filters with conditions into proxy methods
# that contain the conditions. # that contain the conditions.
# #
# For `around_save :filter_name, :if => :condition': # For `around_save :filter_name, :if => :condition':
# #
# def _conditional_callback_save_17 # def _conditional_callback_save_17
# if condition # if condition
# filter_name do # filter_name do
# yield self # yield self
# end # end
# else # else
# yield self # yield self
# end # end
# end # end
# #
name = "_conditional_callback_#{@kind}_#{next_id}" name = "_conditional_callback_#{@kind}_#{next_id}"
@klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{name}(halted) def #{name}(halted)
#{@compiled_options[0] || "if true"} && !halted #{@compiled_options[0] || "if true"} && !halted
#{@filter} do #{@filter} do
yield self
end
else
yield self yield self
end end
else
yield self
end end
RUBY_EVAL end
"#{name}(halted) do" RUBY_EVAL
end "#{name}(halted) do"
end end
end end
@ -229,15 +228,14 @@ module ActiveSupport
def end(key=nil, object=nil) def end(key=nil, object=nil)
return if key && !object.send("_one_time_conditions_valid_#{@callback_id}?") 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 # if condition # after_save :filter_name, :if => :condition
# filter_name # filter_name
# end # end
if @kind == :after [@compiled_options[0], @filter, @compiled_options[1]].compact.join("\n")
[@compiled_options[0], @filter, @compiled_options[1]].compact.join("\n") when :around
else "end"
"end"
end
end end
end end