File Coverage

blib/lib/Directory/Deploy/Declare.pm
Criterion Covered Total %
statement 25 40 62.5
branch 0 2 0.0
condition n/a
subroutine 9 14 64.2
pod n/a
total 34 56 60.7


line stmt bran cond sub pod time code
1             package Directory::Deploy::Declare;
2              
3 2     2   1317 use strict;
  2         4  
  2         73  
4 2     2   11 use warnings;
  2         4  
  2         62  
5              
6 2     2   11 use Directory::Deploy::Carp;
  2         4  
  2         19  
7              
8 2     2   358 use Moose();
  2         4  
  2         32  
9 2     2   10 use Moose::Exporter;
  2         5  
  2         18  
10              
11             Moose::Exporter->setup_import_methods(
12             with_caller => [qw/ add include /],
13             also => [qw/ Moose /],
14             );
15              
16             sub init_meta {
17 0     0     shift;
18 0           return Moose->init_meta( base_class => 'Directory::Deploy::Declare::Base', @_ );
19             }
20              
21             sub initialize {
22 0     0     shift;
23 0           my $class = shift;
24              
25 0 0         return $class->deploy_meta if $class->can( 'deploy_meta' );
26              
27 0           my $deploy_meta = Directory::Deploy::Declare::Meta->new;
28             $class->meta->add_method( deploy_meta => sub {
29 0     0     return $deploy_meta;
30 0           } );
31              
32 0           return $deploy_meta;
33             }
34              
35             sub add {
36 0     0     my $class = shift;
37 0           __PACKAGE__->initialize( $class )->add_replay( add => @_ );
38             }
39              
40             sub include {
41 0     0     my $class = shift;
42 0           __PACKAGE__->initialize( $class )->add_replay( include => @_ );
43             }
44              
45             1;
46              
47             package Directory::Deploy::Declare::Meta;
48              
49 2     2   496 use strict;
  2         4  
  2         47  
50 2     2   9 use warnings;
  2         4  
  2         39  
51              
52 2     2   10 use Moose;
  2         4  
  2         12  
53 2     2   19748 use MooseX::AttributeHelpers;
  0            
  0            
54              
55             has _replay_list => qw/metaclass Collection::Array is ro isa ArrayRef/, default => sub { [] }, provides => {qw/
56             push _add_replay
57             elements replay_list
58             /};
59              
60             sub build {
61             my $self = shift;
62             my $deploy = shift;
63             my $given = shift; # Called from BUILD
64              
65             for my $replay ($self->replay_list) {
66             my @replay = @$replay;
67             my $method = shift @replay;
68             $deploy->$method( @replay );
69             }
70             }
71              
72             sub add_replay {
73             my $self = shift;
74             $self->_add_replay( [ @_ ] );
75             }
76              
77             1;
78              
79             package Directory::Deploy::Declare::Base;
80              
81             use strict;
82             use warnings;
83              
84             use Moose;
85              
86             extends qw/Directory::Deploy/;
87              
88             sub BUILD {
89             my $self = shift;
90             $self->BUILD_deploy( @_ );
91             }
92              
93             sub BUILD_deploy {
94             my $self = shift;
95             if (my $method = $self->can( 'deploy_meta' ) ) {
96             $method->()->build( $self, @_ );
97             }
98             }
99              
100             1;
101             __END__
102              
103             our $CALLER; # Sub::Exporter doesn't make this available
104              
105             my $exporter = Sub::Exporter::build_exporter({
106             into_level => 1,
107             groups => {
108             default => \&build_sugar,
109             },
110             });
111              
112             sub import {
113             my $self = shift;
114             my $pkg = caller;
115              
116             my @args = grep { !/^-base$/i } @_;
117              
118             # just loading the class..
119             return if @args == @_;
120              
121             do {
122             no strict 'refs';
123             push @{ $pkg . '::ISA' }, $self;
124             };
125              
126             local $CALLER = $pkg;
127              
128             $exporter->($self, @args);
129             }
130              
131             sub build_sugar {
132             my ($class, $group, $arg) = @_;
133              
134             my $into = $CALLER;
135              
136             $class->populate_defaults($arg);
137              
138             my $dispatcher = $class->dispatcher_class->new(name => $into);
139              
140             my $builder = $class->builder_class->new(
141             dispatcher => $dispatcher,
142             %$arg,
143             );
144              
145             return {
146             dispatcher => sub { $builder->dispatcher },
147             rewrite => sub { $builder->rewrite(@_) },
148             on => sub { $builder->on(@_) },
149             under => sub { $builder->under(@_) },
150             redispatch_to => sub { $builder->redispatch_to(@_) },
151             next_rule => sub { $builder->next_rule(@_) },
152             last_rule => sub { $builder->last_rule(@_) },
153              
154             then => sub (&) { $builder->then(@_) },
155             chain => sub (&) { $builder->chain(@_) },
156              
157             # NOTE on shift if $into: if caller is $into, then this function is
158             # being used as sugar otherwise, it's probably a method call, so
159             # discard the invocant
160             dispatch => sub { shift if caller ne $into; $builder->dispatch(@_) },
161             run => sub { shift if caller ne $into; $builder->run(@_) },
162             };
163             }