Skip to content

Commit

Permalink
Refactoring: guard clause for the special cases
Browse files Browse the repository at this point in the history
- #504.
  • Loading branch information
dchelimsky committed Dec 18, 2011
1 parent d0cdea9 commit de1dffa
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions lib/rspec/core/metadata.rb
Expand Up @@ -169,37 +169,37 @@ def all_apply?(filters)

# @private
def filter_applies?(key, value, metadata=self)
return metadata[key].any? {|v| filter_applies?(key, v, {key => value})} if Array === metadata[key]

case key
when :line_numbers
metadata.line_number_filter_applies?(value)
when :locations
metadata.location_filter_applies?(value)
else
case value
when Hash
value.all? {|k, v| filter_applies?(k, v, metadata[key])}
when Regexp
metadata[key] =~ value
when Proc
if value.arity == 2
# Pass the metadata hash to allow the proc to check if it even has the key.
# This is necessary for the implicit :if exclusion filter:
# { } # => run the example
# { :if => nil } # => exclude the example
# The value of metadata[:if] is the same in these two cases but
# they need to be treated differently.
value.call(metadata[key], metadata) rescue false
else
value.call(metadata[key]) rescue false
end
return metadata.filter_applies_to_any_value?(key, value) if Array === metadata[key]
return metadata.line_number_filter_applies?(value) if key == :line_numbers
return metadata.location_filter_applies?(value) if key == :locations

case value
when Hash
value.all? {|k, v| filter_applies?(k, v, metadata[key])}
when Regexp
metadata[key] =~ value
when Proc
if value.arity == 2
# Pass the metadata hash to allow the proc to check if it even has the key.
# This is necessary for the implicit :if exclusion filter:
# { } # => run the example
# { :if => nil } # => exclude the example
# The value of metadata[:if] is the same in these two cases but
# they need to be treated differently.
value.call(metadata[key], metadata) rescue false
else
metadata[key].to_s == value.to_s
value.call(metadata[key]) rescue false
end
else
metadata[key].to_s == value.to_s
end
end

# @private
def filter_applies_to_any_value?(key, value)
self[key].any? {|v| filter_applies?(key, v, {key => value})}
end

# @private
def location_filter_applies?(locations)
# it ignores location filters for other files
Expand Down

0 comments on commit de1dffa

Please sign in to comment.