File Coverage

blib/lib/MooseX/Role/DryRunnable.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1 1     1   40839 use strict;
  1         2  
  1         36  
2 1     1   4 use warnings;
  1         2  
  1         56  
3             package MooseX::Role::DryRunnable;
4             {
5             $MooseX::Role::DryRunnable::VERSION = '0.006';
6             }
7              
8 1     1   22 use 5.008;
  1         2  
  1         37  
9 1     1   410 use MooseX::Role::Parameterized;
  0            
  0            
10             with 'MooseX::Role::DryRunnable::Base';
11              
12             use namespace::clean -except => 'meta';
13              
14              
15             parameter methods => (
16             traits => ['Array'],
17             is => 'ro',
18             isa => 'ArrayRef[Str]',
19             default => sub { [] },
20             handles => { all_methods => 'elements' },
21             );
22              
23             role {
24             my $p = shift;
25            
26             foreach my $method_name ( $p->all_methods() ){
27             around $method_name => sub {
28             my $code = shift;
29             my $self = shift;
30              
31             $self->is_dry_run($method_name,@_)
32             ? $self->on_dry_run($method_name,@_)
33             : $self->$code(@_)
34             }
35             }
36             };
37              
38             1;
39              
40             __END__
41              
42             =head1 NAME
43              
44             MooseX::Role::DryRunnable - role for add a dry_run (or dryrun) option into your Moose Class
45              
46             =head1 SYNOPSIS
47              
48             package Foo;
49             use Moose;
50              
51             with 'MooseX::Role::DryRunnable' => {
52             methods => [ qw(bar) ]
53             };
54              
55             has dry_run => (is => 'ro', isa => 'Bool', default => 0);
56              
57             sub bar {
58             shift;
59             print "Foo::bar @_\n";
60             }
61              
62             sub is_dry_run { # required !
63             my $self = shift;
64             my $method = shift;
65             my @args = @_;
66            
67             $ENV{'DRY_RUN'} || shift->dry_run
68             }
69              
70             sub on_dry_run { # required !
71             my $self = shift;
72             my $method = shift;
73             my @args = @_;
74            
75             $self->logger("Dry Run method=$method, args: \n", @args);
76             }
77              
78             =head1 DESCRIPTION
79              
80             This module is a L<Moose> Role who require two methods, `is_dry_run` and `on_dry_run`, the first method return true if we are in this mode (reading from a configuration file, command line option or some environment variable) and the second receive the name of the method and the list of arguments.
81              
82             =head1 REQUIRES
83              
84             =head2 is_dry_run (self, method_name, argument_list)
85              
86             This method will receive the name of the method and the argument list from the original method. Must return a boolean value. If true, we will execute the alternate code described in `on_dry_run`. You must implement!
87              
88             This method can be useful to build a more complex idea of `dry run`. For example you can create groups of methods and create a `is_dry_run` more selective, like methods who save in database and save in filesystem, or you can add/remove some features based on configuration or datetime
89              
90             =head2 on_dry_run (self, method_name, argument_list)
91              
92             This method will receive the name of the method and the argument list from the original method. You must implement!
93              
94             =head1 ROLE PARAMETERS
95              
96             This Role is Parameterized, and we can choose the set of methods to apply the dry_run capability.
97              
98             =head2 methods ArrayRef(Str)
99              
100             This is the set of methods to be changed, must be an array ref of strings. Each method in this parameter will receive an extra code (using Moose 'around') to act as a Dry Run Method.
101              
102             =head1 SEE ALSO
103              
104             L<Moose::Role>, L<MooseX::Role::Parameterized>.
105              
106             =head1 BUGS AND LIMITATIONS
107              
108             There are no known bugs in this module.
109              
110             Please report problems to Tiago Peczenyj <tiago.peczenyj@gmail.com>, or (preferred)
111             to this package's RT tracker at <bug-MooseX-Role-DryRunnable@rt.cpan.org>.
112              
113             =head1 AUTHOR
114              
115             Tiago Peczenyj <tiago.peczenyj@gmail.com>
116              
117              
118             =head1 LICENSE AND COPYRIGHT
119              
120             Copyright (c) 2013 Tiago Peczenyj <tiago.peczenyj@gmail.com>
121              
122             This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.
123              
124             =cut