Class: Kitchen::PlatformFilter
- Inherits:
-
Object
- Object
- Kitchen::PlatformFilter
- Defined in:
- lib/kitchen/platform_filter.rb
Overview
A wrapper on Regexp and strings to mix them in platform filters.
This should handle backward compatibility in most cases were platform are matched against a filters array using Array.include?
This wrapper does not work if filters arrays are converted to Set.
Constant Summary collapse
- REGEXP_LIKE_PATTERN =
Pattern used to determine whether a filter should be handled as a Regexp
%r{^/(?<pattern>.*)/(?<options>[ix]*)$}
Instance Attribute Summary collapse
-
#value ⇒ Regexp
readonly
Value of this filter.
Class Method Summary collapse
-
.convert(filters) ⇒ Array
Converts platform filters into an array of PlatformFilter handling both strings and Regexp.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eq?)
Override of the equality operator to check whether the wrapped Regexp match the given object.
-
#initialize(value) ⇒ PlatformFilter
constructor
Constructs a new filter.
Constructor Details
#initialize(value) ⇒ PlatformFilter
Constructs a new filter.
52 53 54 55 56 |
# File 'lib/kitchen/platform_filter.rb', line 52 def initialize(value) raise ::ArgumentError, "PlatformFilter#new requires value to be a String or a Regexp" unless value.is_a?(::Regexp) || value.is_a?(::String) @value = value end |
Instance Attribute Details
#value ⇒ Regexp (readonly)
Returns value of this filter.
47 48 49 |
# File 'lib/kitchen/platform_filter.rb', line 47 def value @value end |
Class Method Details
.convert(filters) ⇒ Array
Converts platform filters into an array of PlatformFilter handling both strings and Regexp. A string “looks-like” a regexp if it starts by / and end by / + Regexp options i or x
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kitchen/platform_filter.rb', line 35 def self.convert(filters) ::Kernel.Array(filters).map do |filter| if (match = filter.match(REGEXP_LIKE_PATTERN)) = match["options"].include?("i") ? ::Regexp::IGNORECASE : 0 |= ::Regexp::EXTENDED if match["options"].include?("x") filter = ::Regexp.new(match["pattern"], ) end new(filter) end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eq?
Override of the equality operator to check whether the wrapped Regexp match the given object.
62 63 64 65 66 67 68 |
# File 'lib/kitchen/platform_filter.rb', line 62 def ==(other) if @value.is_a?(::Regexp) && (other.is_a?(::String) || other.is_a?(::Symbol)) @value =~ other else other == @value end end |