Permalink
Browse files

Refactoring: guard clause for the special cases

- #504.
  • Loading branch information...
1 parent d0cdea9 commit de1dffa8a11648c511cacd364d77a5fca0c59d43 @dchelimsky dchelimsky committed Dec 18, 2011
Showing with 26 additions and 26 deletions.
  1. +26 −26 lib/rspec/core/metadata.rb
View
@@ -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

0 comments on commit de1dffa

Please sign in to comment.