[Jifty-commit] r5097 - in Net-Jifty: .

Jifty commits jifty-commit at lists.jifty.org
Mon Feb 11 19:01:46 EST 2008


Author: sartak
Date: Mon Feb 11 19:01:46 2008
New Revision: 5097

Modified:
   Net-Jifty/   (props changed)
   Net-Jifty/Changes
   Net-Jifty/Makefile.PL
   Net-Jifty/lib/Net/Jifty.pm

Log:
 r51767 at onn:  sartak | 2008-02-11 19:01:42 -0500
 Add directory filters for use by applications and subclasses. Basically, each
 directory in your path can have a .jifty file with config in it. Each file is
 Hash::Merge'd. This lets your app have whatever context you want, based on
 where you are. Net::Hiveminder will be using this to great effect.


Modified: Net-Jifty/Changes
==============================================================================
--- Net-Jifty/Changes	(original)
+++ Net-Jifty/Changes	Mon Feb 11 19:01:46 2008
@@ -1,5 +1,12 @@
 Revision history for Net-Jifty
 
+0.06
+        Add directory filters for use by applications and subclasses
+            Basically, each directory in your path can have a .jifty file
+            with config in it. Each file is Hash::Merge'd. This lets your
+            app have whatever context you want, based on where you are.
+        appname is no longer required
+
 0.05    Mon Dec 21 01:56:40
         Removed canonicalize_action and canonicalize_model
         Allow search arguments to be arrayrefs, to facilitate using hashes for

Modified: Net-Jifty/Makefile.PL
==============================================================================
--- Net-Jifty/Makefile.PL	(original)
+++ Net-Jifty/Makefile.PL	Mon Feb 11 19:01:46 2008
@@ -11,6 +11,9 @@
 requires        'DateTime';
 requires        'Email::Address';
 requires        'Term::ReadKey';
+requires        'Path::Class';
+requires        'Cwd';
+requires        'Hash::Merge';
 
 build_requires  'Test::More';
 build_requires  'Test::MockObject';

Modified: Net-Jifty/lib/Net/Jifty.pm
==============================================================================
--- Net-Jifty/lib/Net/Jifty.pm	(original)
+++ Net-Jifty/lib/Net/Jifty.pm	Mon Feb 11 19:01:46 2008
@@ -1,14 +1,22 @@
 #!/usr/bin/env perl
 package Net::Jifty;
 use Moose;
+
+use LWP::UserAgent;
+use HTTP::Request;
+use URI;
+
 use YAML;
+use Hash::Merge;
+
 use Encode;
-use URI;
-use LWP::UserAgent;
+use Fcntl qw(:mode);
+
+use Cwd;
+use Path::Class;
+
 use DateTime;
 use Email::Address;
-use Fcntl qw(:mode);
-use HTTP::Request;
 
 =head1 NAME
 
@@ -16,11 +24,11 @@
 
 =head1 VERSION
 
-Version 0.05 released 21 Dec 08
+Version 0.06 released ???
 
 =cut
 
-our $VERSION = '0.05';
+our $VERSION = '0.06';
 
 =head1 SYNOPSIS
 
@@ -156,6 +164,20 @@
     documentation => "Storage for the user's config",
 );
 
+has use_filters => (
+    is            => 'rw',
+    isa           => 'Bool',
+    default       => 1,
+    documentation => "Whether or not to use config files in the user's directory tree",
+);
+
+has filter_file => (
+    is            => 'rw',
+    isa           => 'Str',
+    default       => ".jifty",
+    documentation => "The filename to look for in each parent directory",
+);
+
 =head2 BUILD
 
 Each L<Net::Jifty> object will do the following upon creation:
@@ -740,6 +762,49 @@
     }
 }
 
+=head2 filter_config [DIRECTORY] -> HASH
+
+Looks at the (given or) current directory, and all parent directories, for
+files named C<< $self->filter_file >>. Each file is YAML. The contents of the
+files will be merged (such that child settings override parent settings), and
+the merged hash will be returned.
+
+What this is used for is up to the application or subclasses. L<Net::Jifty>
+doesn't look at this at all, but it may in the future (such as for email and
+password).
+
+=cut
+
+sub filter_config {
+    my $self = shift;
+
+    return {} unless $self->use_filters;
+
+    my $all_config = {};
+
+    my $dir = dir(shift || getcwd);
+
+    my $old_behavior = Hash::Merge::behavior;
+    Hash::Merge::set_behavior('RIGHT_PRECEDENT');
+
+    while (1) {
+        my $file = $dir->file( $self->filter_file );
+
+        if (-e $file && -r _) {
+            my $this_config = YAML::LoadFile($file);
+            $all_config = Hash::Merge::merge($this_config, $all_config);
+        }
+
+        my $parent = $dir->parent;
+        last if $parent eq $dir;
+        $dir = $parent;
+    }
+
+    Hash::Merge::set_behavior($old_behavior);
+
+    return $all_config;
+}
+
 =head2 email_of ID
 
 Retrieve user C<ID>'s email address.


More information about the Jifty-commit mailing list