Class: Kitchen::PlatformFilter

Inherits:
Object
  • Object
show all
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.

Author:

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ PlatformFilter

Constructs a new filter.

Parameters:

  • value (Regexp, String)

    of the filter

Raises:

  • (::ArgumentError)


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

#valueRegexp (readonly)

Returns value of this filter.

Returns:

  • (Regexp)

    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

Returns:

  • (Array)

    filters with regexp-like string converted to PlatformRegexpFilter



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))
      options = match["options"].include?("i") ? ::Regexp::IGNORECASE : 0
      options |= ::Regexp::EXTENDED if match["options"].include?("x")
      filter = ::Regexp.new(match["pattern"], options)
    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.

Parameters:

  • other (Object)

    object to compare to

Returns:

  • (Boolean)

    whether the objects are equal or the wrapped Regexp matches the given string or symbol



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