[SCM] libmessage-passing-perl Debian packaging branch, master, updated. debian/0.111-3-14-g44f6e88

Tomas Doran bobtfish at bobtfish.net
Mon May 6 11:56:52 UTC 2013


The following commit has been merged in the master branch:
commit fafa2235f0ccdb4319b47c78f2abcdde98a0d0c4
Author: Tomas Doran <bobtfish at bobtfish.net>
Date:   Sun Apr 22 16:32:37 2012 +0100

    Rip common stuff out into a param role

diff --git a/lib/Log/Stash.pm b/lib/Log/Stash.pm
index caf99fc..222b4c6 100644
--- a/lib/Log/Stash.pm
+++ b/lib/Log/Stash.pm
@@ -14,29 +14,15 @@ use 5.8.4;
 
 use Log::Stash::DSL;
 
-with 'MooseX::Getopt';
+with
+    'MooseX::Getopt',
+    'Log::Stash::Role::CLIComponent' => { name => 'input' },
+    'Log::Stash::Role::CLIComponent' => { name => 'output' },
+    'Log::Stash::Role::CLIComponent' => { name => 'filter', default => 'Null' };
 
 our $VERSION = '0.001';
 $VERSION = eval $VERSION;
 
-my %things = (
-    input  => 1,
-    filter => 0,
-    output => 1,
-);
-
-foreach my $name (keys %things ) {
-    has $name => (
-        isa => 'Str',
-        is => 'ro',
-        required => $things{$name},
-    );
-}
-
-has '+filter' => (
-    default => 'Null',
-);
-
 sub build_chain {
     my $self = shift;
         log_chain {
@@ -117,29 +103,6 @@ sub start {
     run_log_server $instance->build_chain;
 }
 
-my $json_type = subtype
-  as "HashRef";
-
-coerce $json_type,
-  from NonEmptySimpleStr,
-  via { try { JSON::XS->new->relaxed->decode($_) } };
-
-MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
-    $json_type => '=s'
-);
-
-foreach my $name (map { "${_}_options"  } keys %things) {
-    has $name => (
-        isa => $json_type,
-        traits    => ['Hash'],
-        default => sub { {} },
-        handles => {
-            $name => 'elements',
-        },
-        coerce => 1,
-    );
-}
-
 __PACKAGE__->meta->make_immutable;
 1;
 
diff --git a/lib/Log/Stash/Role/CLIComponent.pm b/lib/Log/Stash/Role/CLIComponent.pm
new file mode 100644
index 0000000..e953321
--- /dev/null
+++ b/lib/Log/Stash/Role/CLIComponent.pm
@@ -0,0 +1,41 @@
+package Log::Stash::Role::CLIComponent;
+use MooseX::Role::Parameterized;
+use Moose::Util::TypeConstraints;
+use Log::Stash::Types;
+use namespace::autoclean;
+
+parameter name => (
+    isa      => 'Str',
+    required => 1,
+);
+
+parameter default => (
+    isa => 'Str',
+    predicate => 'has_default',
+);
+
+role {
+    my $p = shift;
+
+    my $name = $p->name;
+    my $has_default = $p->has_default;
+    my $default = $has_default ? $p->default : undef;
+
+    has $name => (
+        isa => 'Str',
+        is => 'ro',
+        required => $has_default ? 0 : 1,
+        $has_default ? ( default => $default ) : (),
+    );
+
+    has "${name}_options" => (
+        isa => "Log::Stash::Types::FromJSON",
+        traits    => ['Hash'],
+        default => sub { {} },
+        handles => {
+            "${name}_options" => 'elements',
+        },
+        coerce => 1,
+    );
+};
+
diff --git a/lib/Log/Stash/Types.pm b/lib/Log/Stash/Types.pm
index 1fbfd08..846c343 100644
--- a/lib/Log/Stash/Types.pm
+++ b/lib/Log/Stash/Types.pm
@@ -1,6 +1,11 @@
 package Log::Stash::Types;
 use MooseX::Types ();
 use Moose::Util::TypeConstraints;
+use JSON::XS ();
+use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
+use MooseX::Getopt;
+use Try::Tiny;
+use namespace::autoclean;
 
 role_type $_ for map { "Log::Stash::Role::$_" }
     qw/
@@ -18,5 +23,16 @@ coerce 'Log::Stash::Role::Output',
         $class->new(%stuff);
     };
 
+subtype 'Log::Stash::Types::FromJSON',
+    as "HashRef";
+
+coerce 'Log::Stash::Types::FromJSON',
+  from NonEmptySimpleStr,
+  via { try { JSON::XS->new->relaxed->decode($_) } };
+
+MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
+    'Log::Stash::Types::FromJSON' => '=s'
+);
+
 1;
 

-- 
libmessage-passing-perl Debian packaging



More information about the Pkg-perl-cvs-commits mailing list