File Coverage

blib/lib/CCfnX/Attachments.pm
Criterion Covered Total %
statement 16 20 80.0
branch 4 6 66.6
condition n/a
subroutine 2 3 66.6
pod 0 1 0.0
total 22 30 73.3


line stmt bran cond sub pod time code
1             package CCfnX::Attachments;
2 1     1   55711 use Moose::Exporter;
  1         3  
  1         6  
3              
4             Moose::Exporter->setup_import_methods(
5             with_meta => [ 'attachment' ],
6             );
7              
8             sub attachment {
9 2 50   2 0 30307 Moose->throw_error(
10             'Usage: attachment \'name\' => (type => \'Type\', documentation => \'\',' .
11             ' provides => { param => key_from_attachment, ... })'
12             ) if (@_ < 2);
13 2         10 my ( $meta, $name, %options ) = @_;
14              
15             # Add the attachment
16             $meta->add_attribute(
17             $name,
18             is => 'rw',
19             isa => 'Str',
20             type => $options{ type },
21             traits => [ 'Attachable' ],
22 2         9 generates_params => [ keys %{ $options{provides} } ],
  2         11  
23             );
24              
25             # Every attachment will declare that it provides some extra parameters in the provides
26             # these will be converted in attributes. If they start with "-", then they will not be
27             # StackParameters, that is they will not be accessible in CF via a Ref.
28 2         11233 foreach my $attribute (keys %{ $options{ provides } }){
  2         9  
29 2         6 my $lookup_in_attachment = $options{ provides }->{$attribute};
30              
31 2 100       10 if ($meta->find_attribute_by_name($attribute)) {
32 1         53 Moose->throw_error("An attribute with name $attribute already exists");
33             }
34              
35 1         72 my @extra_traits = ();
36 1 50       6 if (substr($attribute,0,1) eq '-'){
37             # Strip off the '-' in the attribute name
38 0         0 substr($attribute,0,1) = '';
39             } else {
40 1         3 push @extra_traits, 'StackParameter';
41             }
42              
43             $meta->add_attribute(
44             $attribute,
45             is => 'rw',
46             isa => 'Any',
47             lazy => 1,
48             traits => [ 'NoGetopt', 'Attached', @extra_traits ],
49             default => sub {
50 0     0     my $params = $_[0];
51 0           my $param = $params->meta->find_attribute_by_name($name);
52 0           return $param->get_info($params->$name, $lookup_in_attachment)
53             },
54 1         10 );
55             }
56             }
57              
58             1;