Sha256: 04df8719fdbe07d02f9ce1f4a2a0de4f6ebcf32dc92c1bf02f9d650723a5ea9f

Contents?: true

Size: 1.36 KB

Versions: 38

Compression:

Stored size: 1.36 KB

Contents

# MultiMatch allows multiple values to be tested at once in a case expression.
# This class is needed since Array does not implement the === operator to mean
# "each v === other.each v". 
#
# This class is useful in situations when the Puppet Type System cannot be used
# (e.g. in Logging, since it needs to be able to log very early i the intialization
# cycle of puppet)
#
# Typically used with the constants
# NOT_NIL
# TUPLE
# TRIPLE
# 
# which test against single NOT_NIL value, Array with two NOT_NIL, and Array with three NOT_NIL
#
module Puppet::Util
class MultiMatch
  attr_reader :values

  def initialize(*values)
    @values = values
  end

  def ===(other)
    lv = @values  # local var is faster than instance var
    case other
    when MultiMatch
      return false unless other.values.size == values.size
      other.values.each_with_index {|v, i| return false unless lv[i] === v || v === lv[i]}
    when Array
      return false unless other.size == values.size
      other.each_with_index {|v, i| return false unless lv[i] === v || v === lv[i]}
    else
      false
    end
    true
  end

  # Matches any value that is not nil using the === operator.
  #
  class MatchNotNil
    def ===(v)
      !v.nil?
    end
  end

  NOT_NIL = MatchNotNil.new().freeze
  TUPLE = MultiMatch.new(NOT_NIL, NOT_NIL).freeze
  TRIPLE = MultiMatch.new(NOT_NIL, NOT_NIL, NOT_NIL).freeze
end
end

Version data entries

38 entries across 38 versions & 2 rubygems

Version Path
puppet-retrospec-1.5.0 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.4.1 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.4.0 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.3.2 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.3.1 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.3.0 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.2.1 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.2.0 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-retrospec-1.1.0 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-4.5.3 lib/puppet/util/multi_match.rb
puppet-4.5.3-x86-mingw32 lib/puppet/util/multi_match.rb
puppet-4.5.3-x64-mingw32 lib/puppet/util/multi_match.rb
puppet-4.5.3-universal-darwin lib/puppet/util/multi_match.rb
puppet-retrospec-1.0.0 vendor/gems/puppet-4.5.2/lib/puppet/util/multi_match.rb
puppet-4.5.2 lib/puppet/util/multi_match.rb
puppet-4.5.2-x86-mingw32 lib/puppet/util/multi_match.rb
puppet-4.5.2-x64-mingw32 lib/puppet/util/multi_match.rb
puppet-4.5.2-universal-darwin lib/puppet/util/multi_match.rb
puppet-4.5.1 lib/puppet/util/multi_match.rb
puppet-4.5.1-x86-mingw32 lib/puppet/util/multi_match.rb