diff options
| author | Peter Meier <peter.meier@immerda.ch> | 2009-04-07 18:45:21 +0200 |
|---|---|---|
| committer | Peter Meier <peter.meier@immerda.ch> | 2009-04-07 18:45:21 +0200 |
| commit | 7a819454febdf34f4384e1bc64c5b2599df4bd38 (patch) | |
| tree | 2d3eec5d0ae55a19189c0cc38e24ce167dead6cd /lib | |
| parent | 1288b26e35a2a9e126d4ae260f377d854b3c3848 (diff) | |
| download | facter-7a819454febdf34f4384e1bc64c5b2599df4bd38.tar.gz facter-7a819454febdf34f4384e1bc64c5b2599df4bd38.tar.xz facter-7a819454febdf34f4384e1bc64c5b2599df4bd38.zip | |
correctly compare values - fixes #2021
this ensures we can compare all kind of objects and
not only instances of strings.
It compares strings in a case-insensitive manner and converts
symbols to strings.
introducing this behavior required that we introduce
a convert util method, to ensure that we convert the value
correctly. Introduced this method in other places as well.
This behavior change requires that we drop one test, which have
become anyway deprecated.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/facter/util/confine.rb | 24 | ||||
| -rw-r--r-- | lib/facter/util/values.rb | 14 |
2 files changed, 26 insertions, 12 deletions
diff --git a/lib/facter/util/confine.rb b/lib/facter/util/confine.rb index a430bbe..4cbb32c 100644 --- a/lib/facter/util/confine.rb +++ b/lib/facter/util/confine.rb @@ -1,22 +1,20 @@ # A restricting tag for fact resolution mechanisms. The tag must be true # for the resolution mechanism to be suitable. + +require 'facter/util/values' + class Facter::Util::Confine attr_accessor :fact, :values + include Facter::Util::Values + # Add the restriction. Requires the fact name, an operator, and the value # we're comparing to. def initialize(fact, *values) raise ArgumentError, "The fact name must be provided" unless fact raise ArgumentError, "One or more values must be provided" if values.empty? - fact = fact.to_s if fact.is_a? Symbol @fact = fact - @values = values.collect do |value| - if value.is_a? String - value - else - value.to_s - end - end + @values = values end def to_s @@ -29,13 +27,15 @@ class Facter::Util::Confine Facter.debug "No fact for %s" % @fact return false end - value = fact.value + value = convert(fact.value) return false if value.nil? - @values.each { |v| - return true if value.downcase == v.downcase - } + @values.each do |v| + v = convert(v) + next unless v.class == value.class + return true if value == v + end return false end end diff --git a/lib/facter/util/values.rb b/lib/facter/util/values.rb new file mode 100644 index 0000000..ebc7614 --- /dev/null +++ b/lib/facter/util/values.rb @@ -0,0 +1,14 @@ +# A util module for facter containing helper methods +module Facter + module Util + module Values + module_function + + def convert(value) + value = value.to_s if value.is_a?(Symbol) + value = value.downcase if value.is_a?(String) + value + end + end + end +end |
