I've got a PR to add a feature to a Ruby application (Puppet) to allow Regex detection.
Right now, the method only does comparison on exact string matching:
defaultfor :operatingsystem => :fedora, :operatingsystemmajrelease => ['22', '23', '24']
This is annoying, as we have to add in new values everytime a new Fedora release comes out, for example.
I've extended it to match on a Regex:
defaultfor :operatingsystem => :fedora, :operatingsystemmajrelease => /^2[2-9]$/
The old method looks like this:
def self.fact_match(fact, values)
values = [values] unless values.is_a? Array
values.map! { |v| v.to_s.downcase.intern }
if fval = Facter.value(fact).to_s and fval != ""
fval = fval.to_s.downcase.intern
values.include?(fval)
else
false
end
end
This is my current proof-of-concept code, which will match both regex and strings, but it feels off.
def self.fact_match(fact, values)
values = [values] unless values.is_a? Array
values.map! do |v|
if v.is_a? Regexp
v
else
v.to_s.downcase.intern
end
end
if fval = Facter.value(fact).to_s and fval != ""
fval = fval.to_s.downcase.intern
if values.any? {|v| v.is_a? Regexp }
regex_match = Regexp.union(values)
fval =~ regex_match
else
values.include?(fval)
end
else
false
end
end
I feel like there's a much easier way of detecting if it's a Regex or not.