File Coverage

lib/DateTimeX/Mashup/Shiras.pm
Criterion Covered Total %
statement 42 42 100.0
branch 7 10 70.0
condition n/a
subroutine 11 11 100.0
pod 0 1 0.0
total 60 64 93.7


line stmt bran cond sub pod time code
1             package DateTimeX::Mashup::Shiras;
2             our $AUTHORITY = 'cpan:JANDREW';
3 1     1   645444 use version 0.77; our $VERSION = version->declare("v0.36.10");
  1         24  
  1         7  
4              
5             if( $ENV{ Smart_Comments } ){
6 1     1   139 use Smart::Comments -ENV;
  1         2  
  1         8  
7             ### <where> - Smart-Comments turned on for DateTimeX-Mashup-Shiras: $VERSION
8             }
9              
10 1     1   827 use 5.010;
  1         7  
11 1     1   539 use MooseX::Role::Parameterized;
  1         50674  
  1         3  
12 1         9 use Types::Standard qw(
13             Bool
14             Str
15             ArrayRef
16 1     1   23422 );
  1         2  
17 1     1   637 use lib '../../../lib', '../../lib';
  1         1  
  1         9  
18 1         9 use DateTimeX::Mashup::Shiras::Types v0.30 qw(
19             WeekDay
20             DateTimeDate
21            
22             WeekDayFromStr
23             DateTimeDateFromHashRef
24             DateTimeDateFromArrayRef
25             DateTimeDateFromNum
26             DateTimeDateFromStr
27 1     1   635 );
  1         15  
28              
29             my @datearray = qw(
30             date_one
31             date_two
32             date_three
33             date_four
34             );
35             my @attribute_list;
36              
37             #########1 Import and set up the attributes to be built 6#########7#########8#########9
38            
39             parameter date_attributes =>(
40             isa => ArrayRef,
41             predicate => '_has_date_attributes',
42             );
43              
44             role{
45             ( my $input, ) = @_;
46             ##### <where> - Loaded ref: $input
47             if( $input->_has_date_attributes ){
48             @attribute_list = @{$input->date_attributes};
49             ### <where> - Ref has a date attribute list: @attribute_list
50             }else{
51             @attribute_list = @datearray;
52             ### <where> - Using the default attribute list: @attribute_list
53             }
54              
55             # build public attributes from the list
56             for my $dateattribute ( @attribute_list ) {
57             my $predicate = 'has_' . $dateattribute;
58             my $reader = 'get_' . $dateattribute;
59             my $writer = 'set_' . $dateattribute;
60             has $dateattribute =>(
61             isa => DateTimeDate->plus_coercions(
62             DateTimeDateFromHashRef,
63             DateTimeDateFromArrayRef,
64             DateTimeDateFromNum,
65             DateTimeDateFromStr,
66             ),
67             coerce => 1,
68             predicate => $predicate,
69             reader => $reader,
70             writer => $writer,
71             trigger => sub{ $_[3] = $dateattribute; _load_day( @_ ); },
72             );
73             }
74            
75             # build private attributes from the list
76             for my $terminator ( '_wkstart', '_wkend' ) {
77             for my $datename ( @attribute_list, 'today' ) {
78             my $attributename = $datename . $terminator;
79             my $reader = 'get_' . $attributename;
80             my $writer = '_set_' . $attributename;
81             has '_' . $attributename =>( #
82             is => 'ro',
83             isa => DateTimeDate->plus_coercions(
84             DateTimeDateFromHashRef,
85             DateTimeDateFromArrayRef,
86             DateTimeDateFromNum,
87             DateTimeDateFromStr,
88             ),
89             reader => $reader,
90             writer => $writer,
91             );
92             }
93             }
94             };
95              
96             #########1 Other Public Attributes 4#########5#########6#########7#########8#########9
97              
98             has 'week_end' =>(
99             isa => WeekDay->plus_coercions( WeekDayFromStr ),
100             coerce => 1,
101             default => 'Fri',# Use Friday as the end of the week, (Saturday would start the next week)
102             reader => '_get_weekend',
103             );
104              
105             #########1 Public Methods 3#########4#########5#########6#########7#########8#########9
106              
107             sub get_now{### for real time checking - get_today is when the module started
108             #~ my ( $self ) = @_;
109             #### <where> - Reached get_now ...
110 1     1 0 4986 return DateTimeDateFromStr->( 'now' );
111             }
112              
113             #########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
114              
115             # set up a private attribute with a public getter for 'today'
116             has '_today' => (
117             is => 'ro',
118             isa => DateTimeDate->plus_coercions( DateTimeDateFromStr ),
119             required => 1,
120             lazy => 1,
121             coerce => 1,
122             default => 'today',
123             reader => 'get_today',
124             writer => '_set_today',
125             );
126              
127             #########1 Private Methods 3#########4#########5#########6#########7#########8#########9
128              
129             sub _load_day{
130 7     7   9 my ( $self, $newvalue, $oldvalue, $basedate ) = @_;
131             #### <where> - Reached _load_day with: $newvalue
132 7         20 my $weekday = $newvalue->day_of_week;
133 7         26 my ( $daysfromweekstart, $daystoweekend ) = $self->_find_weekend( $weekday );
134             #### <where> - to weekend: $daystoweekend
135             #### <where> - to weekstart: $daysfromweekstart
136 7         16 my $dtweekstart = $newvalue->clone;
137 7         64 $dtweekstart->subtract( days => $daysfromweekstart );
138 7         2305 my $dtweekend = $newvalue->clone;
139 7         54 $dtweekend->add( days => $daystoweekend );
140 7         2433 my $wkstartsetter = '_set_' . $basedate . '_wkstart';
141 7         200 $self->$wkstartsetter( $dtweekstart );
142 7         19 my $wkendsetter = '_set_' . $basedate . '_wkend';
143 7         149 $self->$wkendsetter( $dtweekend );
144             #### <where> - week start day: $dtweekstart
145             #### <where> - week end day: $dtweekend
146 7         69 return $newvalue;
147             }
148              
149             sub _find_weekend{
150 7     7   6 my ( $self, $weekday ) = @_;
151 7         144 my $weekend = $self->_get_weekend;
152             #### <where> - Reached _find_weekend
153 7 100       18 my $daystoweekend =
    50          
154             ( $weekday == $weekend ) ?
155             0 :
156             ( $weekday > $weekend ) ?
157             ( 7 - $weekday + $weekend ):
158             ( $weekend - $weekday ) ;
159 7 50       15 my $weekstart =
160             ( $weekend == 7 ) ?
161             1 : $weekend + 1;
162 7 50       16 my $daysfromweekstart =
    100          
163             ( $weekday == $weekstart ) ?
164             0 :
165             ( $weekday < $weekstart ) ?
166             ( 7 - $weekstart + $weekday ):
167             ( $weekday - $weekstart ) ;
168             #### <where> - to week end: $daysfromweekstart
169             #### <where> - to week start: $daystoweekend
170 7         10 return ( $daysfromweekstart, $daystoweekend );
171             }
172              
173             #########1 Phinish Strong 3#########4#########5#########6#########7#########8#########9
174              
175 1     1   1332 no Moose::Role;
  1         1  
  1         10  
176              
177             1;
178             # The preceding line will help the module return a true value
179              
180             #########1 Main POD starts 3#########4#########5#########6#########7#########8#########9
181              
182             __END__
183              
184             =head1 NAME
185              
186             DateTimeX::Mashup::Shiras - A Moose role with date attributes
187              
188             =begin html
189              
190             <a href="https://www.perl.org">
191             <img src="https://img.shields.io/badge/perl-5.10+-brightgreen.svg" alt="perl version">
192             </a>
193              
194             <a href="https://travis-ci.org/jandrew/DateTimeX-Mashup-Shiras">
195             <img alt="Build Status" src="https://travis-ci.org/jandrew/DateTimeX-Mashup-Shiras.png?branch=master" alt='Travis Build'/>
196             </a>
197              
198             <a href='https://coveralls.io/r/jandrew/DateTimeX-Mashup-Shiras?branch=master'>
199             <img src='https://coveralls.io/repos/jandrew/DateTimeX-Mashup-Shiras/badge.svg?branch=master' alt='Coverage Status' />
200             </a>
201              
202             <a href='https://github.com/jandrew/DateTimeX-Mashup-Shiras'>
203             <img src="https://img.shields.io/github/tag/jandrew/DateTimeX-Mashup-Shiras.svg?label=github level" alt="github level"/>
204             </a>
205              
206             <a href="https://metacpan.org/pod/DateTimeX::Mashup::Shiras">
207             <img src="https://badge.fury.io/pl/DateTimeX-Mashup-Shiras.svg?label=cpan version" alt="CPAN version" height="20">
208             </a>
209              
210             <a href='http://cpants.cpanauthors.org/dist/DateTimeX-Mashup-Shiras'>
211             <img src='http://cpants.cpanauthors.org/dist/DateTimeX-Mashup-Shiras.png' alt='kwalitee' height="20"/>
212             </a>
213              
214             =end html
215              
216             =head1 SYNOPSIS
217            
218             package MyPackage;
219             use Moose;
220             with 'DateTimeX::Mashup::Shiras' =>{
221             date_attributes =>[ qw(
222             start_date end_date
223             ) ],
224             };
225             no Moose;
226             __PACKAGE__->meta->make_immutable;
227              
228             #!env perl
229             my $firstinst = MyPackage->new(
230             'start_date' => '8/26/00',
231             );
232             print $firstinst->get_start_date->format_cldr( "yyyy-MMMM-d" ) . "\n";
233             print $firstinst->get_start_date_wkend->ymd( '' ) . "\n";
234             print $firstinst->get_start_date_wkstart->ymd( '' ) . "\n";
235             print $firstinst->set_end_date( '11-September-2001' ) . "\n";
236             print $firstinst->get_end_date_wkstart->dmy( '' ) . "\n";
237             print $firstinst->set_start_date( -1299767400 ) . "\n";
238             print $firstinst->set_start_date( 36764.54167 ) . "\n";
239             print $firstinst->set_start_date( 0 ) . "\n";
240             print $firstinst->set_start_date( 60 ) . "\n";
241            
242             #######################################
243             # Output of SYNOPSIS
244             # 01:2000-August-26
245             # 02:20000901
246             # 03:20000826
247             # 04:2001-09-11T00:00:00
248             # 05:08092001
249             # 06:1928-10-24T09:30:00
250             # 07:2000-08-26T13:00:00
251             # 09:1970-01-01T00:00:00
252             # 09:1970-01-01T00:01:00
253             #######################################
254            
255             =head1 DESCRIPTION
256              
257             L<Shiras|http://en.wikipedia.org/wiki/Moose#Subspecies> - A small subspecies of
258             Moose found in the western United States.
259              
260             This is a Moose Role (L<Moose::Manual::Roles>) that can add date based attributes
261             with some built in date converions to your Moose class. It also provides the
262             traditional today, now, and weekend date calculation for the executed day.
263              
264             The date conversion functionality comes from three different DateTime::Format
265             packages using L<Type::Tiny> coersion. The three modules are;
266             L<DateTime::Format::Flexible>, L<DateTime::Format::Epoch>, and L<DateTimeX::Format::Excel>.
267             The choice between them is managed by L<DateTimeX::Mashup::Shiras::Types> as a type
268             coersion. As a general rule all input strings are parsed by ::Format::Flexible. All
269             numbers are parsed either by ::Format::Excel or by ::Format::Epoch. See the type
270             package for the details and corner cases. Since all the succesful date 'getters'
271             return DateTime objects, all the L<DateTime> methods can be applied directly.
272             ex. $inst-E<gt>get_today_wkend-E<gt>ymd( "/" ).
273              
274             =head2 Warnings
275              
276             B<1.> Double digit years in some date text strings are problematic. This package assumes
277             that all double digit dates are no more than 20 years in the future of processing time
278             (or more than 80 years before processing time)
279              
280             =head2 Parameters
281              
282             This is a L<MooseX::Role::Parameterized> role. The following parameters are passed as
283             keys to a hash_ref when calling B<with 'DateTimeX::Mashup::Shiras' =E<gt>{ %args }>.
284              
285             =head3 date_attributes
286              
287             =over
288              
289             B<Definition:> This is any array ref of the requested date attributes for the target
290             class consuming this role. To review the behavior of each named attribute review the
291             documentation for L<$named_attribute|/$named_attribute> below.
292              
293             B<Default> if this key is not called the role will set up the following four attributes;
294             [ qw( date_one date_two date_three date_four )] (Yes the count four is arbitrary)
295              
296             B<Range> any string that can be treated as an attribute name.
297              
298             =back
299              
300             =head2 Attributes
301              
302             Data passed to new when creating an instance of the consuming class. For modification of
303             these attributes see the listed L</Methods> of the instance.
304              
305             =head3 $named_attribute
306              
307             =over
308              
309             B<Definition:> these are date attributes set to the type 'DateTimeDate'.
310             See the L<Type|DateTimeX::Mashup::Shiras::Types> Class for more details.
311              
312             B<Default> empty
313              
314             B<Range> epoch numbers, DateTime definition HashRefs, Date Epoch ArrayRefs, and
315             human readable strings
316              
317             =back
318              
319             =head3 week_end
320              
321             =over
322              
323             B<Definition:> This holds the definition of the last day of the week
324              
325             B<Default> 'Friday'
326              
327             B<Range> This will accept either day names, day abbreviations
328             (no periods), or day integers (1 = Monday, 7 = Sunday )
329              
330             =back
331              
332             =head2 Methods
333              
334             Methods are used to manipulate both the public and private attributes of this role.
335             All attributes are set as 'ro' so other than ->new( ) these methods are the only way
336             to change or clear attributes. See L<Moose::Manual::Roles> for generic implementation
337             instructions.
338              
339             =head3 set_${named_attribute}( $date )
340              
341             =over
342              
343             B<Definition:> This is the way to change (or set) the various dates.
344              
345             B<Accepts:> Any $date data that can be coerced by L<supported ::Format
346             |/DESCRIPTION> modules.
347              
348             B<Returns:> the equivalent DateTime object
349              
350             =back
351              
352             =head3 get_(${named_attribute}|today|now)->format_command( 'format' )
353              
354             =over
355              
356             B<Definition:> This is how you can call various dates and format their
357             output. example $self->get_today->ymd( "-" ). B<Note:> 'today' and 'now'
358             are special attribute cases and do not need to be defined to be retrieved.
359              
360             B<Returns:> a DateTime object
361              
362             =back
363              
364             =head3 get_(${named_attribute}|today)_(wkend|wkstart)
365              
366             =over
367              
368             B<Definition:> This is a way to call the equivalent start and end of the
369             week definded by the given 'week_end' attribute value. 'now' is not included
370             in this list.
371              
372             B<Returns:> a DateTime object
373              
374             =back
375              
376             =head1 GLOBAL VARIABLES
377              
378             =head2 $ENV{Smart_Comments}
379              
380             The module uses L<Smart::Comments> if the '-ENV' option is set. The 'use' is
381             encapsulated in an if block triggered by an environmental variable to comfort
382             non-believers. Setting the variable $ENV{Smart_Comments} in a BEGIN block will
383             load and turn on smart comment reporting. There are three levels of 'Smartness'
384             available in this module '###', '####', and '#####'.
385              
386             =head1 BUILD / INSTALL from Source
387            
388             B<1.> Download a compressed file with this package code from your favorite source
389              
390             =over
391              
392             L<Meta::CPAN|https://metacpan.org/pod/DateTimeX::Mashup::Shiras>
393              
394             L<github|https://github.com/jandrew/DateTimeX-Mashup-Shiras>
395              
396             L<CPAN|http://search.cpan.org/~jandrew/DateTimeX-Mashup-Shiras/>
397              
398             =back
399            
400             B<3.> Extract the code from the compressed file.
401              
402             =over
403              
404             If you are using tar on a .tar.gz file this should work:
405              
406             tar -zxvf DateTimeX-Mashup-Shiras-v0.xx.tar.gz
407            
408             =back
409              
410             B<4.> Change (cd) into the extracted directory
411              
412             B<5.> Run the following
413              
414             =over
415              
416             (for Windows find what version of make was used to compile your perl)
417              
418             perl -V:make
419            
420             (then for Windows substitute the correct make function (s/make/dmake/g)? below)
421            
422             =back
423              
424             >perl Makefile.PL
425              
426             >make
427              
428             >make test
429              
430             >make install # As sudo/root
431              
432             >make clean
433              
434             =head1 SUPPORT
435              
436             L<github DateTimeX-Mashup-Shiras/issues|https://github.com/jandrew/DateTimeX-Mashup-Shiras/issues>
437              
438             =head1 TODO
439              
440             =over
441              
442             B<1.> Add L<Log::Shiras|https://github.com/jandrew/Log-Shiras> debugging in exchange for
443             L<Smart::Comments>
444              
445             =back
446              
447             =head1 AUTHOR
448              
449             =over
450              
451             Jed Lund
452              
453             jandrew@cpan.org
454              
455             =back
456              
457             =head1 CONTRIBUTORS
458              
459             This is the (likely incomplete) list of people who have helped
460             make this distribution what it is, either via code contributions,
461             patches, bug reports, help with troubleshooting, etc. A huge
462             'thank you' to all of them.
463              
464             =over
465              
466             L<Toby Inkster|https://github.com/tobyink>
467              
468             =back
469              
470             =head1 COPYRIGHT
471              
472             This program is free software; you can redistribute
473             it and/or modify it under the same terms as Perl itself.
474              
475             The full text of the license can be found in the
476             LICENSE file included with this module.
477              
478             This software is copyrighted (c) 2013, 2015 by Jed Lund.
479              
480             =head1 DEPENDENCIES
481              
482             =over
483              
484             B<5.010> - (L<perl>)
485              
486             L<version>
487              
488             L<MooseX::Role::Parameterized>
489              
490             L<Type::Tiny>
491              
492             L<DateTimeX::Mashup::Shiras::Types>
493              
494             L<DateTime|https://metacpan.org/module/DateTime>
495              
496             L<DateTime::Format::Epoch|https://metacpan.org/module/DateTime::Format::Epoch>
497              
498             L<DateTime::Format::Excel|https://metacpan.org/module/DateTime::Format::Excel>
499              
500             L<DateTime::Format::Flexible|https://metacpan.org/module/DateTime::Format::Flexible>
501              
502             =back
503              
504             =head1 SEE ALSO
505              
506             =over
507              
508             L<Time::Piece>
509              
510             L<MooseX::Types::Perl>
511              
512             L<Date::Parse>
513              
514             L<Date::Manip::Date>
515              
516             L<DateTimeX::Format>
517              
518             =back
519              
520             =cut
521              
522             #########1 Main POD ends 3#########4#########5#########6#########7#########8#########9