[Moose-commits] r7300 - in MooseX-Role-Cmd: . lib/MooseX/Role lib/MooseX/Role/Cmd/Meta/Attribute t t/lib/Test/Cmd

isillitoe at code2.0beta.co.uk isillitoe at code2.0beta.co.uk
Tue Jan 13 21:12:45 GMT 2009


Author: isillitoe
Date: 2009-01-13 13:12:44 -0800 (Tue, 13 Jan 2009)
New Revision: 7300

Modified:
   MooseX-Role-Cmd/Makefile.PL
   MooseX-Role-Cmd/lib/MooseX/Role/Cmd.pm
   MooseX-Role-Cmd/lib/MooseX/Role/Cmd/Meta/Attribute/Trait.pm
   MooseX-Role-Cmd/t/lib/Test/Cmd/DirWithTraits.pm
   MooseX-Role-Cmd/t/traits.t
Log:
Use attribute to set environment variable rather than command line option, e.g.

    has 'home_dir' => (
        traits => [ 'CmdOpt' ],
        is => 'rw',
        isa => 'Str',
        cmdopt_env => 'APP_HOME',
        default => '/my/app/home'
    );
    
    # $ENV{APP_HOME} = /my/app/home

Bumped the $VERSION up to 0.05

Increased the Moose dependency to 0.60



Modified: MooseX-Role-Cmd/Makefile.PL
===================================================================
--- MooseX-Role-Cmd/Makefile.PL	2009-01-13 17:40:23 UTC (rev 7299)
+++ MooseX-Role-Cmd/Makefile.PL	2009-01-13 21:12:44 UTC (rev 7300)
@@ -3,7 +3,7 @@
 name     'MooseX-Role-Cmd';
 all_from 'lib/MooseX/Role/Cmd.pm';
 
-requires 'Moose' => 0.24;
+requires 'Moose' => 0.60;
 requires 'IPC::Cmd' => 0.42;
 
 auto_install;

Modified: MooseX-Role-Cmd/lib/MooseX/Role/Cmd/Meta/Attribute/Trait.pm
===================================================================
--- MooseX-Role-Cmd/lib/MooseX/Role/Cmd/Meta/Attribute/Trait.pm	2009-01-13 17:40:23 UTC (rev 7299)
+++ MooseX-Role-Cmd/lib/MooseX/Role/Cmd/Meta/Attribute/Trait.pm	2009-01-13 21:12:44 UTC (rev 7300)
@@ -82,6 +82,35 @@
     predicate => 'has_cmdopt_name',
 );
 
+=head2 cmdopt_env
+
+This attribute trait can be used to specify an environment variable rather
+than a command line option.
+
+    has 'home_dir' => (
+        traits => [ 'CmdOpt' ],
+        is => 'rw',
+        isa => 'Str',
+        cmdopt_env => 'APP_HOME',
+        default => '/my/app/home'
+    );
+    
+    # $ENV{APP_HOME} = /my/app/home
+
+=cut
+
+=head2 has_cmdopt_env
+
+Test for attribute above
+
+=cut
+
+has 'cmdopt_env' => (
+    is        => 'rw',
+    isa       => 'Str',
+    predicate => 'has_cmdopt_env',
+);
+
 no Moose::Role;
 
 # register this as a metaclass alias ...

Modified: MooseX-Role-Cmd/lib/MooseX/Role/Cmd.pm
===================================================================
--- MooseX-Role-Cmd/lib/MooseX/Role/Cmd.pm	2009-01-13 17:40:23 UTC (rev 7299)
+++ MooseX-Role-Cmd/lib/MooseX/Role/Cmd.pm	2009-01-13 21:12:44 UTC (rev 7300)
@@ -7,7 +7,7 @@
 use Moose::Role;
 use MooseX::Role::Cmd::Meta::Attribute::Trait;
 
-our $VERSION = '0.04';
+our $VERSION = '0.05';
 
 =head1 NAME
 
@@ -173,7 +173,6 @@
     return wantarray ? @args : \@args;
 }
 
-
 =head1 PRIVATE METHODS
 
 =head2 _attr_name_to_cmd_options
@@ -206,6 +205,16 @@
                        cmdopt_name  => '+foo'
                      );                                 # +foo
 
+Use attribute to set an environment variable rather than command line param
+
+    has 'home_dir' => ( traits      => [ 'CmdOpt' ],
+                       isa          => 'Str',
+                       cmdopt_env   => 'APP_HOME'
+                       default      => '/my/app/home'
+                     );
+    
+    # $ENV{APP_HOME} = /my/app/home
+
 =cut
 
 sub _attr_name_to_cmd_options {
@@ -219,6 +228,15 @@
     
     # override defaults with Traits
     if ( $attr->does('MooseX::Role::Cmd::Meta::Attribute::Trait') ) {
+        
+        # environment vars not used as params
+        if ($attr->has_cmdopt_env) {
+            my $env_key   = $attr->cmdopt_env;
+            my $env_value = $self->$attr_name;
+            $ENV{$env_key} = $env_value;
+            return;
+        }
+        
         if ($attr->has_cmdopt_prefix) {
             $opt_prefix = $attr->cmdopt_prefix;
         }

Modified: MooseX-Role-Cmd/t/lib/Test/Cmd/DirWithTraits.pm
===================================================================
--- MooseX-Role-Cmd/t/lib/Test/Cmd/DirWithTraits.pm	2009-01-13 17:40:23 UTC (rev 7299)
+++ MooseX-Role-Cmd/t/lib/Test/Cmd/DirWithTraits.pm	2009-01-13 21:12:44 UTC (rev 7300)
@@ -16,6 +16,8 @@
 has 'undef'     => ( traits => [ 'CmdOpt' ], isa => 'Bool', is => 'rw' );
 has 'undef_str' => ( traits => [ 'CmdOpt' ], isa => 'Str',  is => 'rw' );
 
+has 'env_test'  => ( traits => [ 'CmdOpt' ], isa => 'Str',  is => 'rw', cmdopt_env => 'ENV_TEST_KEY' );
+
 sub build_bin_name { 'dir' };
 
 1;

Modified: MooseX-Role-Cmd/t/traits.t
===================================================================
--- MooseX-Role-Cmd/t/traits.t	2009-01-13 17:40:23 UTC (rev 7299)
+++ MooseX-Role-Cmd/t/traits.t	2009-01-13 21:12:44 UTC (rev 7300)
@@ -1,7 +1,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 9;
+use Test::More tests => 10;
 
 use FindBin;
 use lib "$FindBin::Bin/lib";
@@ -45,7 +45,7 @@
 is_deeply( \@cmd_args, \@expected_args, 'args look OK' );
 
 # with traits
-isa_ok( $cmd = Test::Cmd::DirWithTraits->new( %args ), 'Test::Cmd::DirWithTraits' );
+isa_ok( $cmd = Test::Cmd::DirWithTraits->new( %args, env_test => 'test_value' ), 'Test::Cmd::DirWithTraits' );
 
 is( $cmd->bin_name, 'dir' );
 
@@ -56,5 +56,7 @@
 # warn "GOT: "     .Dumper( \@cmd_args );
 # warn "EXPECTED: ".Dumper( \@expected_args );
 
+is ($ENV{'ENV_TEST_KEY'}, 'test_value', 'check cmdopt_env' );
+
 is_deeply( \@cmd_args, \@expected_args, 'trait args look OK' );
 




More information about the Moose-commits mailing list