On Mar 4, 2009, at 3:35 PM, duritong wrote:
>
> this ensures we can compare all kind of objects and
> not only instances of 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.
> ---
> lib/facter/util.rb | 11 +++++++++++
> lib/facter/util/confine.rb | 26 ++++++++++++++------------
> spec/unit/util/confine.rb | 38 +++++++++++++++++++++++++++++++++++
> ++-
> 3 files changed, 62 insertions(+), 13 deletions(-)
> create mode 100644 lib/facter/util.rb
>
> diff --git a/lib/facter/util.rb b/lib/facter/util.rb
> new file mode 100644
> index 0000000..ebd2646
> --- /dev/null
> +++ b/lib/facter/util.rb
> @@ -0,0 +1,11 @@
> +# A util module for facter containing helper methods
> +module Facter
> + module Util
I'd be more comfortable with this being a more focused Util module;
something like Facter::Util::Values. I've let the Util module in
Puppet collect into this huge steaming mess, and I've been trying to
stick to small modules to stop that from happening in Facter or
getting worse in Puppet.
>
> + module_function
> +
> + def convert(value)
> + value = value.to_s if value.is_a?(Symbol)
> + value
> + end
> + end
> +end
> diff --git a/lib/facter/util/confine.rb b/lib/facter/util/confine.rb
> index a430bbe..7412123 100644
> --- a/lib/facter/util/confine.rb
> +++ b/lib/facter/util/confine.rb
> @@ -1,22 +1,22 @@
> # A restricting tag for fact resolution mechanisms. The tag must be
> true
> # for the resolution mechanism to be suitable.
> +
> +require 'facter/util'
> +
> class Facter::Util::Confine
> attr_accessor :fact, :values
>
> + include Facter::Util
> +
> # 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
> + @fact = convert(fact)
> + @values = values.collect { |value|
> + convert(value)
> + }
> end
>
> def to_s
> @@ -33,9 +33,11 @@ class Facter::Util::Confine
>
> return false if value.nil?
>
> - @values.each { |v|
> - return true if value.downcase == v.downcase
> - }
> + @values.each do |v|
> + next unless v.class == value.class
> + return true if v.is_a?(String) and v.downcase ==
> value.downcase
> + return true if v == value
> + end
Wouldn't you use 'convert' here? Something like 'return
convert(value) == v'?
>
> return false
> end
> end
> diff --git a/spec/unit/util/confine.rb b/spec/unit/util/confine.rb
> index 5c1ce3b..5bf6536 100755
> --- a/spec/unit/util/confine.rb
> +++ b/spec/unit/util/confine.rb
> @@ -35,7 +35,7 @@ describe Facter::Util::Confine do
>
> describe "when evaluating" do
> before do
> - @confine = Facter::Util::Confine.new("yay", "one", "two")
> + @confine = Facter::Util::Confine.new("yay", "one",
> "two", true, 1, [3,4])
> @fact = mock 'fact'
> Facter.stubs(:[]).returns @fact
> end
> @@ -66,10 +66,46 @@ describe Facter::Util::Confine do
> @confine.true?.should be_true
> end
>
> + it "should return true if any of the provided integer
> values matches the fact's value" do
> + @fact.stubs(:value).returns 1
> +
> + @confine.true?.should be_true
> + end
> +
> + it "should return true if any of the provided boolan values
> matches the fact's value" do
> + @fact.stubs(:value).returns true
> +
> + @confine.true?.should be_true
> + end
> +
> + it "should return true if any of the provided array values
> matches the fact's value" do
> + @fact.stubs(:value).returns [3,4]
> +
> + @confine.true?.should be_true
> + end
> +
> it "should return false if none of the provided values
> matches the fact's value" do
> @fact.stubs(:value).returns "three"
>
> @confine.true?.should be_false
> end
> +
> + it "should return false if none of the provided integer
> values matches the fact's value" do
> + @fact.stubs(:value).returns 2
> +
> + @confine.true?.should be_false
> + end
> +
> + it "should return false if none of the provided boolan
> values matches the fact's value" do
> + @fact.stubs(:value).returns false
> +
> + @confine.true?.should be_false
> + end
> +
> + it "should return false if none of the provided array
> values matches the fact's value" do
> + @fact.stubs(:value).returns [1,2]
> +
> + @confine.true?.should be_false
> + end
> end
> end
> --
> 1.5.6.3
>
>
> >
--
The Internet, of course, is more than just a place to find pictures of
people having sex with dogs. -- Time Magazine, 3 July 1995
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---