Declare/ZDCF.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 130 CC 9 SCAR 26.4
CC
9
Cov
0%
BUILD
42.0
application
20.0
application_names
2.0
CRAP 14.0
1 package ZMQ::Declare::ZDCF;
2 {
3 $ZMQ::Declare::ZDCF::VERSION = '0.03';
4 }
5 use 5.008001;
6 use Moose;
7  
8 use ZMQ::Declare;
9 use ZMQ::Declare::ZDCF::Validator;
10 use ZMQ::Declare::ZDCF::Encoder;
11 use ZMQ::Declare::ZDCF::Encoder::JSON;
12  
13 use ZeroMQ qw(:all);
14  
15 use ZMQ::Declare::Constants qw(:all);
16 use ZMQ::Declare::Types;
17 use Carp ();
18 use Clone ();
19  
20 has 'validator' => (
21 is => 'rw',
22 isa => 'ZMQ::Declare::ZDCF::Validator',
23 default => sub {ZMQ::Declare::ZDCF::Validator->new},
24 );
25  
26 has 'encoder' => (
27 is => 'rw',
28 isa => 'ZMQ::Declare::ZDCF::Encoder',
29 default => sub {ZMQ::Declare::ZDCF::Encoder::JSON->new},
30 );
31  
32 has 'tree' => (
33 is => 'rw',
34 required => 1,
35 );
36  
37 sub BUILD {
38 my $self = shift;
39 my $tree = $self->tree;
40  
41 # needs decoding
42 if (not ref($tree) eq 'HASH') {
43 my $sref;
44 if (ref($tree) eq 'SCALAR') { # content as scalar ref
45 $sref = $tree;
46 }
47 elsif (not ref $tree) { # slurp from file
48 use autodie;
49 open my $fh, "<", $tree;
50 local $/;
51 my $zdcf_content = <$fh>;
52 $sref = \$zdcf_content;
53 }
54  
55 $tree = $self->encoder->decode($sref);
56 Carp::croak("Failed to decode input ZDCF")
57 if not defined $tree;
58  
59 $self->tree($tree);
60 }
61  
62 $tree = $self->validator->validate_and_upgrade($tree);
63 Carp::croak("Failed to validate decoded ZDCF")
64 if not defined $tree;
65 $self->tree($tree);
66 }
67  
68 sub application_names {
69 my $self = shift;
70 return keys %{ $self->tree->{apps} || {} };
71 }
72  
73 sub application {
74 my $self = shift;
75 my $name = shift;
76 $name = "" if not defined $name; # more-or-less compat with pre v1 ZDCF spec
77  
78 my $apps = $self->tree->{apps} || {};
79 Carp::croak("Invalid application '$name'")
80 if not exists $apps->{$name};
81  
82 my $app_spec = $apps->{$name};
83 my $typename = $app_spec->{type};
84 $typename = '' if not defined $typename;
85  
86 return ZMQ::Declare::Application->new(
87 name => $name,
88 spec => $self,
89 );
90 }
91  
92 sub encode {
93 my ($self) = @_;
94 return $self->encoder->encode($self->tree);
95 }
96  
97 sub write_to_file {
98 my ($self, $filename) = @_;
99 open my $fh, ">", $filename or die $!;
100 print $fh ${ $self->encode };
101 close $fh;
102 }
103  
104 no Moose;
105 __PACKAGE__->meta->make_immutable;
106  
107 __END__