Permalink
Browse files

Ported Facter::Util::Confine to Perl6

  • Loading branch information...
1 parent 3645cc2 commit c635196de945e63d3fc613de9c6c818e5f260587 @cosimo committed Nov 21, 2010
Showing with 56 additions and 41 deletions.
  1. +56 −0 lib/Facter/Util/Confine.pm
  2. +0 −41 lib/Facter/Util/confine.rb
View
@@ -0,0 +1,56 @@
+=begin pod
+
+=head1 NAME
+
+Facter::Util::Confine
+
+=head1 DESCRIPTION
+
+A restricting tag for fact resolution mechanisms. The tag must be true
+for the resolution mechanism to be suitable.
+
+=end pod
+
+class Facter::Util::Confine;
+
+use Facter::Util::Values;
+
+has $.fact is rw;
+has @.values is rw;
+
+# Add the restriction. Requires the fact name, an operator, and the value
+# we're comparing to.
+method initialize($fact, *@values) {
+ die "The fact name must be provided" unless $fact; # ArgumentError
+ die "One or more values must be provided" if @values.elems == 0;
+ $.fact = $fact;
+ @.values = @values;
+}
+
+method to_s { # XXX Str ?
+ my $fact = $.fact;
+ my $values = @.values.join(',');
+ return "'$fact' '$values'";
+}
+
+# Evaluate the fact, returning true or false.
+method true {
+
+ unless my $fact = Facter.get_fact($.fact) {
+ Facter.debug("No fact for $.fact");
+ return False
+ }
+
+ my $value = Facter::Util::Values.convert($fact.value);
+
+ return False if ! $value.defined;
+
+ for @.values -> $v {
+ $v = Facter::Util::Values.convert($v);
+ next unless $v.WHAT == $value.WHAT; # ruby's .class
+ return True if $value == $v;
+ }
+
+ return False
+}
+
View
@@ -1,41 +0,0 @@
-# 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
- @values = values
- end
-
- def to_s
- return "'%s' '%s'" % [@fact, @values.join(",")]
- end
-
- # Evaluate the fact, returning true or false.
- def true?
- unless fact = Facter[@fact]
- Facter.debug "No fact for %s" % @fact
- return false
- end
- value = convert(fact.value)
-
- return false if value.nil?
-
- @values.each do |v|
- v = convert(v)
- next unless v.class == value.class
- return true if value == v
- end
- return false
- end
-end

0 comments on commit c635196

Please sign in to comment.