Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Slowly approaching a functional state

The value resolution code is still broken, as directly
translated from the Ruby yield paradigm.
That needs to change.
  • Loading branch information...
commit b0106a3b685f24b445e4ac216993ff06611c5059 1 parent 5e20701
@cosimo authored
View
12 lib/Facter.pm
@@ -39,13 +39,13 @@ our $LAST_OBJECT;
# Static variables (@@debug)
our $debug = 1;
our $timing = 0;
+our $collection;
# Private members
-has $!collection is rw;
has @!search_path is rw = ();
method collection {
- $!collection //= Facter::Util::Collection.new
+ $collection //= Facter::Util::Collection.new
}
method version {
@@ -107,7 +107,9 @@ method get_fact($name) {
}
method fact(*@args) {
- self.collection.fact(@args);
+ my $fact = self.collection.fact(@args);
+ Facter.debug("Facter.fact returns $fact");
+ return $fact;
}
method flush(*@args) {
@@ -144,8 +146,6 @@ method to_hash (*@args) {
# Add a resolution mechanism for a named fact. This does not distinguish
# between adding a new fact and adding a new way to resolve a fact.
method add ($name, Sub $block) {
- # TODO add %options support
- #multi method add ($name, %options = (), $block) {
Facter.debug("Facter: adding fact $name as " ~ $block.perl);
my $instance = self // Facter.get_instance;
$instance.collection.add($name, $block);
@@ -180,7 +180,7 @@ method warn ($msg) {
}
method reset {
- $!collection = ();
+ $collection = ();
}
# Load all of the default facts, and then everything from disk.
View
17 lib/Facter/Util/Collection.pm
@@ -7,7 +7,7 @@ class Facter::Util::Collection;
use Facter::Util::Loader;
# Private members
-has %!facts is rw;
+has %!facts is rw = ();
has $!loader is rw;
# Return a fact object by name. If you use this, you still have to call
@@ -18,21 +18,22 @@ method get($name) {
# Add a resolution mechanism for a named fact. This does not distinguish
# between adding a new fact and adding a new way to resolve a fact.
-method add($fact_name, Sub $block) {
+method add($fact_name, $block) {
# TODO add %options support
my %options = ();
- Facter.debug("collection.add $fact_name: " ~ $block.perl);
+ Facter.debug("Facter::Util::Collection.add $fact_name: $block");
my $name = self.canonize($fact_name);
my $fact = %!facts{$name};
unless $fact {
- Facter.debug("new Fact '$name'");
+ Facter.debug("- new Fact '$name'");
$fact = Facter::Util::Fact.new(name => $name);
- Facter.debug("new Fact '$name' created: " ~ $fact.perl);
+ Facter.debug("- new Fact '$name' created: " ~ $fact.perl);
%!facts{$name} = $fact;
+ Facter.debug('%!facts{$name}' ~ " = '" ~ %!facts{$name});
}
# Set any fact-appropriate options.
@@ -63,6 +64,7 @@ method add($fact_name, Sub $block) {
die "Invalid facter option(s) " ~ %options.keys ==> map { $_.Str } ==> join(",");
}
+ Facter.debug("Facter::Util::Collection.add returns \$fact=$fact");
return $fact;
}
@@ -80,13 +82,14 @@ method each () {
# Return a fact by name.
method fact($name) {
my $fact_name = self.canonize($name);
- Facter.debug("self.canonize($name) = $fact_name");
unless %!facts{$fact_name} {
- Facter.debug("Loading fact $fact_name through loader");
+ Facter.debug("Facter::Util::Collection.fact loading fact $fact_name through loader");
self.loader.load($fact_name);
}
+ Facter.debug("Facter::Util::Collection.fact fact $fact_name: " ~ %!facts{$fact_name});
+
return %!facts{$fact_name};
}
View
47 lib/Facter/Util/Fact.pm
@@ -5,12 +5,12 @@ use Facter::Util::Resolution;
our $TIMEOUT = 5;
has $!value is rw;
-has $!suitable is rw;
+has $!suitable is rw = False;
-has $.name is rw;
-has $.ldapname is rw;
-has @.resolves is rw;
-has $!searching is rw;
+has $.name is rw = "";
+has $.ldapname is rw = "";
+has @.resolves is rw = ();
+has $!searching is rw = False;
# Create a new fact, with no resolution mechanisms.
method initialize($name, %options = ()) {
@@ -23,13 +23,13 @@ method initialize($name, %options = ()) {
$.ldapname //= $.name.Str;
@.resolves = ();
- $!searching = false;
+ $!searching = False;
$!value = Mu;
}
# Add a new resolution mechanism. This requires a block, which will then
# be evaluated in the context of the new mechanism.
-method add($block) {
+method add ($block) {
#raise ArgumentError, "You must pass a block to Fact<instance>.add" unless block_given?
if ! $block {
@@ -38,12 +38,15 @@ method add($block) {
Facter.debug("Fact.add($.name, $block)");
my $resolve = Facter::Util::Resolution.new(name => $.name);
+ Facter.debug("\$resolve = " ~ $resolve.perl);
# ruby: resolve.instance_eval(block);
$block($resolve);
@.resolves.push($resolve);
+ Facter.debug("\$resolve = " ~ $resolve.perl);
+
# Immediately sort the resolutions, so that we always have
# a sorted list for looking up values.
# We always want to look them up in the order of number of
@@ -62,7 +65,11 @@ method flush {
# Return the value for a given fact. Searches through all of the mechanisms
# and returns either the first value or nil.
method value {
- return $!value if $!value;
+
+ if $!value.defined {
+ Facter.debug("Fact value already defined: $!value");
+ return $!value;
+ }
if @.resolves.elems == 0 {
Facter.debug("No resolves for $!name");
@@ -70,23 +77,33 @@ method value {
}
self.searching(sub {
- $!value = Mu;
+ Facter.debug("Facter::Util::Fact.value for $!name. Searching.");
+
+ $!value = Mu;
my $foundsuits = False;
while @.resolves {
my $resolve = @.resolves.shift;
- next unless $resolve.suitable;
+ Facter.debug("- Evaluating resolve $resolve");
+
+ unless $resolve.suitable {
+ Facter.debug("- Resolve $resolve unsuitable");
+ next;
+ }
+
$foundsuits = True;
my $candidate_value = $resolve.value;
- if $candidate_value.defined and $candidate_value != "" {
+ Facter.debug("- Candidate value from resolve $resolve is $candidate_value");
+
+ if $candidate_value.defined and $candidate_value ne "" {
return $candidate_value;
}
}
unless $foundsuits {
Facter.debug("Found no suitable resolves of "
- ~ @!resolves.elems ~ " for " ~ $!name
+ ~ @.resolves.elems ~ " for " ~ $!name
);
}
@@ -122,8 +139,12 @@ method searching (Sub $block) {
# If we've gotten this far, we're not already searching, so go ahead and do so.
$!searching = True;
+ Facter.debug("Facter::Util::Fact.searching for $!name: start");
+
gather {
- take $block.();
+ my $next-value = $block();
+ Facter.debug("- Got next value: $next-value");
+ take $next-value;
$!searching = False;
};
View
5 lib/Facter/Util/Resolution.pm
@@ -13,10 +13,11 @@ class Facter::Util::Resolution;
#equire 'timeout'
#equire 'rbconfig'
+has $!value is rw;
+
has $.code is rw;
has $.interpreter is rw;
has $.name is rw;
-has $!value is rw;
has $.timeout is rw;
has @.confines is rw;
@@ -133,7 +134,7 @@ method suitable {
return $.suitable;
}
-method Str {
+method to_s {
return self.value;
}
View
3  lib/Facter/perl6os.pm
@@ -1,5 +1,4 @@
-Facter.add("perl6os", sub ($f) {
- Facter.debug("perl6os fact block running");
+Facter.add(<perl6os>, sub ($f) {
$f.setcode(block => sub {
$*OS.Str
});
View
5 t/basic.t
@@ -17,13 +17,10 @@ my $fact = $facter.fact($test-fact);
ok($fact, "test fact is loaded");
ok($fact.^can("value"), "fact object has a value() method");
-my $lsbdistname = $facter.fact($test-fact).Str;
+my $lsbdistname = $facter.fact($test-fact).value;
ok($lsbdistname, "fact '$test-fact' is loaded");
is($lsbdistname, $*OS, "test fact has correct value");
-my $same-value = $facter.fact($test-fact).value;
-is($lsbdistname, $same-value, "fact.value and fact.Str should yield the same result");
-
# Unused for now...
#@search_dirs = $facter.search_path;
#ok(@search_dirs.elems > 0, "Search path should be filled now");
Please sign in to comment.
Something went wrong with that request. Please try again.