class Puppet::Pops::Evaluator::CompareOperator

Compares the puppet DSL way

Equality

All string vs. numeric equalities check for numeric equality first, then string equality Arrays are equal to arrays if they have the same length, and each element equals Hashes are equal to hashes if they have the same size and keys and values equals. All other objects are equal if they are ruby #== equal

Public Class Methods

new() click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
20 def initialize
21   @@equals_visitor  ||= Visitor.new(self, "equals", 1, 1)
22   @@compare_visitor ||= Visitor.new(self, "cmp", 1, 1)
23   @@match_visitor ||= Visitor.new(self, "match", 2, 2)
24   @@include_visitor ||= Visitor.new(self, "include", 2, 2)
25 end

Public Instance Methods

compare(a, b) click to toggle source

Performs a comparison of a and b, and return > 0 if a is bigger, 0 if equal, and < 0 if b is bigger. Comparison of String vs. Numeric always compares using numeric.

   # File lib/puppet/pops/evaluator/compare_operator.rb
33 def compare(a, b)
34   @@compare_visitor.visit_this_1(self, a, b)
35 end
equals(a, b) click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
27 def equals(a, b)
28   @@equals_visitor.visit_this_1(self, a, b)
29 end
include?(a, b, scope) click to toggle source

Answers is b included in a

   # File lib/puppet/pops/evaluator/compare_operator.rb
43 def include?(a, b, scope)
44   @@include_visitor.visit_this_2(self, a, b, scope)
45 end
match(a, b, scope = nil) click to toggle source

Performs a match of a and b, and returns true if b matches a

   # File lib/puppet/pops/evaluator/compare_operator.rb
38 def match(a, b, scope = nil)
39   @@match_visitor.visit_this_2(self, b, a, scope)
40 end

Protected Instance Methods

cmp_Numeric(a, b) click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
60 def cmp_Numeric(a, b)
61   if b.is_a?(Numeric)
62     a <=> b
63   elsif b.is_a?(Time::Timespan) || b.is_a?(Time::Timestamp)
64     -(b <=> a) # compare other way and invert result
65   else
66     raise ArgumentError.new(_("A Numeric is not comparable to non Numeric"))
67   end
68 end
cmp_Object(a, b) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
113 def cmp_Object(a, b)
114   raise ArgumentError.new(_('Only Strings, Numbers, Timespans, Timestamps, and Versions are comparable'))
115 end
cmp_String(a, b) click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
49 def cmp_String(a, b)
50   return a.casecmp(b) if b.is_a?(String)
51   raise ArgumentError.new(_("A String is not comparable to a non String"))
52 end
cmp_Symbol(a, b) click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
90 def cmp_Symbol(a, b)
91   if b.is_a?(Symbol)
92     a <=> b
93   else
94     raise ArgumentError.new(_("Symbol not comparable to non Symbol"))
95   end
96 end
cmp_Timespan(a, b) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
 98 def cmp_Timespan(a, b)
 99   raise ArgumentError.new(_('Timespans are only comparable to Timespans, Integers, and Floats')) unless b.is_a?(Time::Timespan) ||  b.is_a?(Integer) || b.is_a?(Float)
100   a <=> b
101 end
cmp_Timestamp(a, b) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
103 def cmp_Timestamp(a, b)
104   raise ArgumentError.new(_('Timestamps are only comparable to Timestamps, Integers, and Floats')) unless b.is_a?(Time::Timestamp) ||  b.is_a?(Integer) || b.is_a?(Float)
105   a <=> b
106 end
cmp_Version(a, b) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
108 def cmp_Version(a, b)
109   raise ArgumentError.new(_('Versions not comparable to non Versions')) unless b.is_a?(SemanticPuppet::Version)
110   a <=> b
111 end
equals_Array(a, b) click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
78 def equals_Array(a, b)
79   return false unless b.is_a?(Array) && a.size == b.size
80   a.each_index {|i| return false unless equals(a.slice(i), b.slice(i)) }
81   true
82 end
equals_Hash(a, b) click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
84 def equals_Hash(a, b)
85   return false unless b.is_a?(Hash) && a.size == b.size
86   a.each {|ak, av| return false unless equals(b[ak], av)}
87   true
88 end
equals_NilClass(a, b) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
122 def equals_NilClass(a, b)
123   # :undef supported in case it is passed from a 3x data structure
124   b.nil? || b == :undef
125 end
equals_Numeric(a, b) click to toggle source
   # File lib/puppet/pops/evaluator/compare_operator.rb
70 def equals_Numeric(a, b)
71   if b.is_a?(Numeric)
72     a == b
73   else
74     false
75   end
76 end
equals_Object(a, b) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
118 def equals_Object(a, b)
119   a == b
120 end
equals_String(a, b) click to toggle source

Equality is case independent.

   # File lib/puppet/pops/evaluator/compare_operator.rb
55 def equals_String(a, b)
56   return false unless b.is_a?(String)
57   a.casecmp(b) == 0
58 end
equals_Symbol(a, b) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
127 def equals_Symbol(a, b)
128   # :undef supported in case it is passed from a 3x data structure
129   a == b || a == :undef && b.nil?
130 end
include_Array(a, b, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
166 def include_Array(a, b, scope)
167   case b
168   when Regexp
169     matched = nil
170     a.each do |element|
171       next unless element.is_a? String
172       matched = element.match(b) # nil, or MatchData
173       break if matched
174     end
175     # Always set match data, a "not found" should not keep old match data visible
176     set_match_data(matched, scope) # creates ephemeral
177     return !!matched
178   when String, SemanticPuppet::Version
179     a.any? { |element| match(b, element, scope) }
180   when Types::PAnyType
181     a.each {|element| return true if b.instance?(element) }
182     return false
183   else
184     a.each {|element| return true if equals(element, b) }
185     return false
186   end
187 end
include_Binary(a, b, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
153 def include_Binary(a, b, scope)
154   case b
155   when Puppet::Pops::Types::PBinaryType::Binary
156     a.binary_buffer.include?(b.binary_buffer)
157   when String
158     a.binary_buffer.include?(b)
159   when Numeric
160     a.binary_buffer.bytes.include?(b)
161   else
162     false
163   end
164 end
include_Hash(a, b, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
189 def include_Hash(a, b, scope)
190   include?(a.keys, b, scope)
191 end
include_Object(a, b, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
132 def include_Object(a, b, scope)
133   false
134 end
include_String(a, b, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
136 def include_String(a, b, scope)
137   case b
138   when String
139     # substring search downcased
140     a.downcase.include?(b.downcase)
141   when Regexp
142     matched = a.match(b)           # nil, or MatchData
143     set_match_data(matched, scope) # creates ephemeral
144     !!matched                      # match (convert to boolean)
145   when Numeric
146     # convert string to number, true if ==
147     equals(a, b)
148   else
149     false
150   end
151 end
include_VersionRange(a, b, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
193 def include_VersionRange(a, b, scope)
194   Types::PSemVerRangeType.include?(a, b)
195 end
match_Array(array, left, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
238 def match_Array(array, left, scope)
239   return false unless left.is_a?(Array)
240   return false unless left.length == array.length
241   array.each_with_index.all? { | pattern, index| match(left[index], pattern, scope) }
242 end
match_Hash(hash, left, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
244 def match_Hash(hash, left, scope)
245   return false unless left.is_a?(Hash)
246   hash.all? {|x,y| match(left[x], y, scope) }
247 end
match_Object(pattern, a, scope) click to toggle source

Matches in general by using == operator

    # File lib/puppet/pops/evaluator/compare_operator.rb
198 def match_Object(pattern, a, scope)
199   equals(a, pattern)
200 end
match_PAnyType(any_type, left, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
230 def match_PAnyType(any_type, left, scope)
231   # right is a type and left is not - check if left is an instance of the given type
232   # (The reverse is not terribly meaningful - computing which of the case options that first produces
233   # an instance of a given type).
234   #
235   any_type.instance?(left)
236 end
match_Regexp(regexp, left, scope) click to toggle source

Matches only against strings

    # File lib/puppet/pops/evaluator/compare_operator.rb
203 def match_Regexp(regexp, left, scope)
204   return false unless left.is_a? String
205   matched = regexp.match(left)
206   set_match_data(matched, scope) unless scope.nil? # creates or clears ephemeral
207   !!matched # convert to boolean
208 end
match_Symbol(symbol, left, scope) click to toggle source
    # File lib/puppet/pops/evaluator/compare_operator.rb
249 def match_Symbol(symbol, left, scope)
250   return true if symbol == :default
251   equals(left, default)
252 end
match_Version(version, left, scope) click to toggle source

Matches against semvers and strings

    # File lib/puppet/pops/evaluator/compare_operator.rb
211 def match_Version(version, left, scope)
212   if left.is_a?(SemanticPuppet::Version)
213     version == left
214   elsif left.is_a? String
215     begin
216       version == SemanticPuppet::Version.parse(left)
217     rescue ArgumentError
218       false
219     end
220   else
221     false
222   end
223 end
match_VersionRange(range, left, scope) click to toggle source

Matches against semvers and strings

    # File lib/puppet/pops/evaluator/compare_operator.rb
226 def match_VersionRange(range, left, scope)
227   Types::PSemVerRangeType.include?(range, left)
228 end