File Coverage

blib/lib/Time/Duration/Object.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1 1     1   55672 use strict;
  1         10  
  1         24  
2 1     1   5 use warnings;
  1         1  
  1         47  
3             package Time::Duration::Object 0.302;
4             # ABSTRACT: Time::Duration, but an object
5              
6 1     1   430 use Time::Duration 1.02;
  1         1579  
  1         117  
7              
8             #pod =head1 SYNOPSIS
9             #pod
10             #pod use Time::Duration::Object;
11             #pod
12             #pod my $duration = Time::Duration::Object->new($end_time - $start_time);
13             #pod
14             #pod =head1 DESCRIPTION
15             #pod
16             #pod This module provides an object-oriented interface to Time::Duration. Sure,
17             #pod it's overkill, and Time::Duration is plenty useful without OO, but this
18             #pod interface makes it easy to use Time::Duration with Class::DBI, and that's a
19             #pod good thing.
20             #pod
21             #pod =head1 METHODS
22             #pod
23             #pod =head2 C< new($seconds) >
24             #pod
25             #pod This returns a new Time::Duration::Object for the given number of seconds.
26             #pod
27             #pod =cut
28              
29             sub new {
30 3     3 1 996 my ($class, $duration) = @_;
31 3 100       10 return unless defined $duration;
32 2         5 bless \$duration => $class;
33             }
34              
35             #pod =head2 C< seconds >
36             #pod
37             #pod This returns the number of seconds in the duration (i.e., the argument you
38             #pod passed to your call to C.)
39             #pod
40             #pod =cut
41              
42             sub seconds {
43 1     1 1 315 return ${(shift)};
  1         9  
44             }
45              
46             #pod =head2 C
47             #pod
48             #pod =head2 C
49             #pod
50             #pod =head2 C
51             #pod
52             #pod =head2 C
53             #pod
54             #pod =head2 C
55             #pod
56             #pod =head2 C
57             #pod
58             #pod =head2 C
59             #pod
60             #pod =head2 C
61             #pod
62             #pod =head2 C
63             #pod
64             #pod =head2 C
65             #pod
66             #pod These methods all perform the function of the same name from Time::Duration.
67             #pod
68             #pod =cut
69              
70             {
71             ## no critic (ProhibitNoStrict ProhibitNoWarnings)
72 1     1   14 no strict 'refs';
  1         3  
  1         27  
73 1     1   5 no warnings 'redefine';
  1         2  
  1         218  
74             my @methods = map { $_, "$_\_exact" } qw(duration ago from_now later earlier);
75             for (@methods) {
76             my $method = \&{"Time::Duration::$_"};
77             *{$_} = sub {
78 8     8   19 unshift @_, ${(shift)};
  8         16  
79 8         21 my $result = &$method(@_);
80 8         742 bless \$result => 'Time::Duration::_Result';
81             }
82             }
83             }
84              
85             package Time::Duration::_Result 0.302;
86              
87             #pod =head2 as_string
88             #pod
89             #pod Time::Duration::Object methods don't return strings, they return an object that
90             #pod stringifies. If you can't deal with that and don't want to stringify by
91             #pod concatenating an empty string, you can call C instead.
92             #pod
93             #pod my $duration = Time::Duration::Object->new(8000);
94             #pod print $duration->ago->as_string; # 2 hours and 13 minutes ago
95             #pod
96             #pod =cut
97              
98 6     6   877 sub as_string { ${ $_[0] } }
  6         49  
99              
100             #pod =head2 concise
101             #pod
102             #pod This method can be called on the result of the above methods, trimming down the
103             #pod ouput. For example:
104             #pod
105             #pod my $duration = Time::Duration::Object->new(8000);
106             #pod print $duration->ago; # 2 hours and 13 minutes ago
107             #pod print $duration->ago->concise # 2hr13m ago
108             #pod
109             #pod =cut
110              
111             sub concise {
112 1     1   753 my $self = shift;
113 1         2 Time::Duration::concise(${$self});
  1         4  
114             }
115              
116             use overload
117 1         5 '""' => 'as_string',
118 1     1   957 fallback => 1;
  1         836  
119              
120             #pod =head1 SEE ALSO
121             #pod
122             #pod Obviously, this module would be useless without Sean Burke's super-useful
123             #pod L. There are those, I'm sure, who will think that even I
124             #pod that module...
125             #pod
126             #pod =cut
127              
128             1;
129              
130             __END__