File Coverage

blib/lib/Message/Passing/Role/CLIComponent.pm
Criterion Covered Total %
statement 36 36 100.0
branch 6 6 100.0
condition n/a
subroutine 12 12 100.0
pod 0 1 0.0
total 54 55 98.1


line stmt bran cond sub pod time code
1             package Message::Passing::Role::CLIComponent;
2 3     3   24 use strict;
  3         9  
  3         142  
3 3     3   21 use warnings;
  3         7  
  3         119  
4             use Package::Variant
5 3         22 importing => ['Moo::Role'],
6 3     3   1581 subs => [ qw(has around before after with) ];
  3         17583  
7 3     3   2081 use MooX::Options;
  3         5188  
  3         20  
8 3     3   1461 use MooX::Types::MooseLike::Base qw/ Str /;
  3         13653  
  3         199  
9 3     3   22 use JSON::MaybeXS ();
  3         6  
  3         86  
10 3     3   1091 use Try::Tiny qw/ try /;
  3         2731  
  3         938  
11              
12             sub make_variant {
13 21     21 0 31823 my ($class, $target_package, %arguments) = @_;
14 21         50 my $p = shift;
15              
16 21         44 my $name = $arguments{name};
17 21         45 my $has_default = exists $arguments{default};
18 21 100       54 my $default = $has_default ? $arguments{default} : undef;
19              
20             option "$name" => (
21             isa => Str,
22             is => 'ro',
23             # required => "$has_default" ? 0 : 1,
24 21 100   10   84 "$has_default" ? ( default => sub { "$default" } ) : (),
  10         363  
25             format => 's',
26             );
27              
28             option "${name}_options" => (
29             is => 'ro',
30 10     10   378 default => sub { {} },
31 14     14   385 isa => sub { ref($_[0]) eq 'HASH' },
32             coerce => sub {
33 14     14   211 my $str = shift;
34 14 100       37 if (! ref $str) {
35             try {
36 3         104 $str = JSON::MaybeXS->new(relaxed => 1)->decode($str)
37 3         21 };
38             }
39 14         332 $str;
40             },
41 21         20964 format => 's',
42             );
43             }
44              
45             1;
46              
47             =head1 NAME
48              
49             Message::Passing::Role::CLIComponent - Package::Variant providing 'foo' and 'foo_options' attributes
50              
51             =head1 SYNOPSIS
52              
53             package My::Message::Passing::Script;
54             use Moo;
55             use MooX::Options;
56             use Message::Passing::Role::CLIComponent;
57             use Message::Passing::DSL;
58             use namespace::clean -except => 'meta';
59              
60             with
61             CLIComponent( name => 'input', default => 'STDIN' ),
62             'Message::Passing::Role::Script';
63              
64             sub build_chain {
65             my $self = shift;
66             message_chain {
67             input example => ( %{ $self->input_options }, output_to => 'test_out', class => $self->input, );
68             output test_out => ( ... );
69             };
70             }
71              
72             __PACKAGE__->start unless caller;
73             1;
74              
75             =head1 DESCRIPTION
76              
77             A L<Package::Variant> role producer, which is used to provide a pair of attributes for name/options
78             as per the L<message-pass> script.
79              
80             =head1 ROLE PARAMETERS
81              
82             =head2 name
83              
84             The name of the main attribute. An additional attribute called C<< "${name}_options" >> will also be added,
85             which coerces a hashref from JSON.
86              
87             =head2 default
88              
89             A default value for the main attribute. If this is not supplied, than the attribute will be required.
90              
91             =head1 SPONSORSHIP
92              
93             This module exists due to the wonderful people at Suretec Systems Ltd.
94             <http://www.suretecsystems.com/> who sponsored its development for its
95             VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
96             the SureVoIP API -
97             <http://www.surevoip.co.uk/support/wiki/api_documentation>
98              
99             =head1 AUTHOR, COPYRIGHT AND LICENSE
100              
101             See L<Message::Passing>.
102              
103             =cut