File Coverage

blib/lib/Stepford/Role/Step.pm
Criterion Covered Total %
statement 42 46 91.3
branch n/a
condition n/a
subroutine 14 16 87.5
pod 5 5 100.0
total 61 67 91.0


line stmt bran cond sub pod time code
1             package Stepford::Role::Step;
2              
3 43     43   26596 use strict;
  43         106  
  43         1296  
4 43     43   228 use warnings;
  43         85  
  43         1083  
5 43     43   225 use namespace::autoclean;
  43         78  
  43         259  
6              
7             our $VERSION = '0.006000';
8              
9 43     43   3918 use List::AllUtils qw( any );
  43         9398  
  43         2379  
10 43     43   16941 use Stepford::LoggerWithMoniker;
  43         145  
  43         1657  
11 43     43   20518 use Stepford::Trait::StepDependency;
  43         209  
  43         2092  
12 43     43   17783 use Stepford::Trait::StepProduction;
  43         104  
  43         1527  
13 43     43   308 use Stepford::Types qw( Logger );
  43         74  
  43         345  
14              
15 43     43   84057 use Moose::Role;
  43         92  
  43         166  
16              
17             requires qw( run last_run_time );
18              
19             has logger => (
20             is => 'ro',
21             isa => Logger,
22             required => 1,
23             );
24              
25             around BUILDARGS => sub {
26             my $orig = shift;
27             my $class = shift;
28              
29             my $args = $class->$orig(@_);
30              
31             if ( $args->{logger} ) {
32             $args->{logger} = Stepford::LoggerWithMoniker->new(
33             logger => $args->{logger},
34             moniker => $class->_log_moniker,
35             );
36             }
37              
38             return $args;
39             };
40              
41             sub _log_moniker {
42 632     632   1223 my $class = shift;
43 632         24122 return $class;
44             }
45              
46             # Some of these should be moved into a metaclass extension
47             sub productions {
48 1997     1997 1 4991 my $class = shift;
49              
50             return
51 1997         8366 grep { $_->does('Stepford::Trait::StepProduction') }
  8037         1190735  
52             $class->meta->get_all_attributes;
53             }
54              
55             sub has_production {
56 0     0 1 0 my $class = shift;
57 0         0 my $name = shift;
58              
59 0     0   0 return any { $_->name eq $name } $class->productions;
  0         0  
60             }
61              
62             sub productions_as_hashref {
63 382     382 1 1212 my $self = shift;
64              
65 382         1792 return { map { $_->name => $self->production_value( $_->name ) }
  460         58828  
66             $self->productions };
67             }
68              
69             sub production_value {
70 460     460 1 882 my $self = shift;
71 460         766 my $name = shift;
72              
73 460         1431 my $reader = $self->meta->find_attribute_by_name($name)->get_read_method;
74 460         51029 return $self->$reader;
75             }
76              
77             sub dependencies {
78 1186     1186 1 13133 my $class = shift;
79              
80             return
81 1186         5179 grep { $_->does('Stepford::Trait::StepDependency') }
  4409         730345  
82             $class->meta->get_all_attributes;
83             }
84              
85             1;
86              
87             # ABSTRACT: The basic role all step classes must implement
88              
89             __END__
90              
91             =pod
92              
93             =encoding UTF-8
94              
95             =head1 NAME
96              
97             Stepford::Role::Step - The basic role all step classes must implement
98              
99             =head1 VERSION
100              
101             version 0.006000
102              
103             =head1 DESCRIPTION
104              
105             All of your step classes must consume this role. It provides the basic
106             interface that the L<Stepford::Runner> class expects.
107              
108             =head1 ATTRIBUTES
109              
110             This role provides one attribute:
111              
112             =head2 logger
113              
114             This attribute is required for all roles. It will be provided to your step
115             classes by the L<Stepford::Runner> object.
116              
117             The Step object will wrap the logger with an object that prepends prepends
118             C<[$log_moniker] > to each log message. The moniker is determined by calling
119             C<< $class->_log_moniker >> on the class during object construction.
120              
121             =head1 METHODS
122              
123             This role provides the following methods:
124              
125             =head2 $step->productions
126              
127             This method returns a list of L<Moose::Meta::Attribute> objects that were
128             given the C<StepProduction> trait. This can be an empty list.
129              
130             =head2 $step->has_production($name)
131              
132             Returns true if the step has a production of the given name.
133              
134             =head2 $step->productions_as_hashref
135              
136             Returns all production values as a hash reference.
137              
138             =head2 $step->production_value($name)
139              
140             This method returns the value of the given production for the object it is
141             called on.
142              
143             =head2 $step->dependencies
144              
145             This method returns a list of L<Moose::Meta::Attribute> objects that were
146             given the C<StepDependency> trait. This can be an empty list.
147              
148             =head1 REQUIRED METHODS
149              
150             All classes which consume the L<Stepford::Role::Step> role must implement the
151             following methods:
152              
153             =head2 $step->run
154              
155             This method receives no arguments. It is expected to do whatever it is that
156             the step does.
157              
158             It may also do other things such as record the last run time.
159              
160             =head2 $step->last_run_time
161              
162             This method must return a timestamp marking the last time the step was
163             run. You are encouraged to use L<Time::HiRes> as appropriate to provide hi-res
164             timestamps.
165              
166             You can return C<undef> from this method to request an unconditional rebuild
167             of this step, regardless of the C<last_run_time> of previous steps. If a step
168             has an C<undef> C<last_run_time> after being run, then all steps that depend
169             on that step will also be re-run.
170              
171             =head1 OPTIONAL METHODS
172              
173             All classes which consume the L<Stepford::Role::Step> role may implement the
174             following methods:
175              
176             =head2 $class->_log_moniker
177              
178             This is expected to return a string identifying the class for the purposes of
179             logging. The default moniker is the full class name, but you may prefer to
180             override this in your step classes with something shorter or more descriptive.
181              
182             =head1 SUPPORT
183              
184             Bugs may be submitted through L<https://github.com/maxmind/Stepford/issues>.
185              
186             =head1 AUTHOR
187              
188             Dave Rolsky <drolsky@maxmind.com>
189              
190             =head1 COPYRIGHT AND LICENSE
191              
192             This software is copyright (c) 2014 - 2019 by MaxMind, Inc.
193              
194             This is free software; you can redistribute it and/or modify it under
195             the same terms as the Perl 5 programming language system itself.
196              
197             =cut