File Coverage

blib/lib/Message/Passing/DSL/Factory.pm
Criterion Covered Total %
statement 60 61 98.3
branch 7 10 70.0
condition 6 15 40.0
subroutine 15 15 100.0
pod 0 6 0.0
total 88 107 82.2


line stmt bran cond sub pod time code
1             package Message::Passing::DSL::Factory;
2 4     4   594 use Moo;
  4         11478  
  4         43  
3 4     4   3063 use MooX::Types::MooseLike::Base qw/ HashRef /;
  4         6780  
  4         296  
4 4     4   1939 use String::RewritePrefix;
  4         49514  
  4         27  
5 4     4   2668 use Message::Passing::Output::STDERR;
  4         15  
  4         143  
6 4     4   29 use Carp qw/ confess /;
  4         9  
  4         192  
7 4     4   42 use Scalar::Util qw/ blessed /;
  4         8  
  4         161  
8 4     4   46 use Module::Runtime qw/ require_module /;
  4         10  
  4         28  
9 4     4   230 use namespace::clean -except => [qw/ meta _build_default_error_chain /];
  4         9  
  4         32  
10              
11             sub expand_class_name {
12 13     13 0 36 my ($self, $type, $name) = @_;
13 13         104 String::RewritePrefix->rewrite({
14             '' => 'Message::Passing::' . $type . '::',
15             '+' => ''
16             }, $name);
17             }
18              
19             has registry => (
20             is => 'ro',
21             isa => HashRef,
22             default => sub { {} },
23             lazy => 1,
24             clearer => 'clear_registry',
25             );
26              
27 9     9 0 143 sub registry_get { shift->registry->{shift()} }
28 13     13 0 235 sub registry_has { exists shift->registry->{shift()} }
29             sub registry_set {
30 13     13 0 37 my ($self, $name, $val) = @_;
31 13         253 $self->registry->{$name} = $val;
32             }
33              
34             sub set_error {
35 1     1 0 4 my ($self, %opts) = @_;
36             my $class = delete $opts{class}
37 1   33     4 || confess("Class name needed");
38 1         5 require_module($class);
39 1         44 $self->_set_error($class->new(%opts));
40             }
41              
42 4     4   3866 use Message::Passing::Role::HasErrorChain;
  4         11  
  4         1616  
43             *_build_default_error_chain = \&Message::Passing::Role::HasErrorChain::_build_default_error_chain;
44             has error => (
45             is => 'ro',
46             writer => '_set_error',
47             lazy => 1,
48             builder => '_build_default_error_chain',
49             );
50              
51             sub make {
52 13     13 0 59 my ($self, %opts) = @_;
53             my $class = delete $opts{class}
54 13   33     47 || confess("Class name needed");
55 13         30 my $name = delete $opts{name};
56 13         27 my $type = delete $opts{_type};
57 13 50       39 confess("We already have a thing named $name")
58             if $self->registry_has($name);
59 13         247 my $output_to = $opts{output_to};
60 13 100 66     70 if ($output_to && !blessed($output_to)) {
61             # We have to deal with the ARRAY case here for Filter::T
62 9 100       31 if (ref($output_to) eq 'ARRAY') {
63 1         2 my @out;
64 1         3 foreach my $name_or_thing (@$output_to) {
65 1 50       3 if (blessed($name_or_thing)) {
66 0         0 push(@out, $name_or_thing);
67             }
68             else {
69 1   33     3 my $thing = $self->registry_get($name_or_thing)
70             || confess("Do not have a component named '$name_or_thing'");
71 1         10 push(@out, $thing);
72             }
73             }
74 1         4 $opts{output_to} = \@out;
75             }
76             else {
77 8   33     21 my $proper_output_to = $self->registry_get($output_to)
78             || confess("Do not have a component named '$output_to'");
79 8         80 $opts{output_to} = $proper_output_to;
80             }
81             }
82 13 50       36 if (!exists($opts{error})) {
83 13         210 $opts{error} = $self->error;
84             }
85 13         225 $class = $self->expand_class_name($type, $class);
86 13         873 require_module($class);
87 13         197 my $out = $class->new(%opts);
88 13         9719 $self->registry_set($name, $out);
89 13         1065 return $out;
90             }
91              
92              
93             1;
94              
95             =head1 NAME
96              
97             Message::Passing::DSL::Factory - Build a set of chains using symbolic names
98              
99             =head1 DESCRIPTION
100              
101             No user serviceable parts inside. See L<Message::Passing::DSL>.
102              
103             =head1 SPONSORSHIP
104              
105             This module exists due to the wonderful people at Suretec Systems Ltd.
106             <http://www.suretecsystems.com/> who sponsored its development for its
107             VoIP division called SureVoIP <http://www.surevoip.co.uk/> for use with
108             the SureVoIP API -
109             <http://www.surevoip.co.uk/support/wiki/api_documentation>
110              
111             =head1 AUTHOR, COPYRIGHT AND LICENSE
112              
113             See L<Message::Passing>.
114              
115             =cut
116