Class: Machinery::ElementFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/element_filter.rb

Overview

Copyright © 2013-2016 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, operator = nil, matchers = nil) ⇒ ElementFilter

Returns a new instance of ElementFilter.



21
22
23
24
25
26
27
28
29
30
# File 'lib/element_filter.rb', line 21

def initialize(path, operator = nil, matchers = nil)
  @path = path
  @matchers = {}

  unless [NilClass, String, Array].include?(matchers.class)
    raise Machinery::Errors::InvalidFilter.new("Wrong filter type")
  end

  add_matchers(operator, matchers) if operator && matchers
end

Instance Attribute Details

#matchersObject

Returns the value of attribute matchers.



19
20
21
# File 'lib/element_filter.rb', line 19

def matchers
  @matchers
end

#pathObject

Returns the value of attribute path.



19
20
21
# File 'lib/element_filter.rb', line 19

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



87
88
89
90
# File 'lib/element_filter.rb', line 87

def ==(other)
  path == other.path &&
    matchers == other.matchers
end

#add_matchers(operator, matchers) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/element_filter.rb', line 36

def add_matchers(operator, matchers)
  unless [
    Machinery::Filter::OPERATOR_EQUALS,
    Machinery::Filter::OPERATOR_EQUALS_NOT
  ].include?(operator)
    raise Machinery::Errors::InvalidFilter.new("Wrong filter operator '#{operator}'")
  end

  @matchers[operator] ||= []
  @matchers[operator] += Array(matchers)
end

#filters_scope?(scope) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/element_filter.rb', line 83

def filters_scope?(scope)
  (path =~ /\/#{scope}(\/|$)/) ? true : false
end

#initialize_copy(other) ⇒ Object



32
33
34
# File 'lib/element_filter.rb', line 32

def initialize_copy(other)
  @matchers = other.matchers.dup
end

#matches?(value) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/element_filter.rb', line 48

def matches?(value)
  @matchers.each do |operator, matchers|
    matchers.each do |matcher|
      values_equal = case value
      when Machinery::Array
        value_array = value.elements

        (value_array - Array(matcher)).empty? && (Array(matcher) - value_array).empty?
      when ::Array
        (value - Array(matcher)).empty? && (Array(matcher) - value).empty?
      when String
        if matcher.is_a?(Array)
          exception = Machinery::Errors::ElementFilterTypeMismatch.new
          exception.failed_matcher =
            "#{path}#{operator}#{matcher.join(",")}"
          raise exception
        end

        if matcher.end_with?("*")
          value.start_with?(matcher[0..-2])
        else
          value == matcher
        end
      end
      if operator == Machinery::Filter::OPERATOR_EQUALS
        return true if values_equal
      elsif operator == Machinery::Filter::OPERATOR_EQUALS_NOT
        return true unless values_equal
      end
    end
  end

  false
end