File Coverage

lib/JIRA/REST/Class/Factory.pm
Criterion Covered Total %
statement 32 40 80.0
branch 0 4 0.0
condition 0 3 0.0
subroutine 9 11 81.8
pod 4 4 100.0
total 45 62 72.5


line stmt bran cond sub pod time code
1             package JIRA::REST::Class::Factory;
2 4     4   1116 use parent qw( Class::Factory::Enhanced );
  4         228  
  4         31  
3 4     4   7548 use strict;
  4         6  
  4         63  
4 4     4   16 use warnings;
  4         6  
  4         77  
5 4     4   51 use 5.010;
  4         12  
6              
7             our $VERSION = '0.12';
8             our $SOURCE = 'CPAN';
9             ## $SOURCE = 'GitHub'; # COMMENT
10             # the line above will be commented out by Dist::Zilla
11              
12             # ABSTRACT: A factory class for building all the other classes in L<JIRA::REST::Class|JIRA::REST::Class>.
13              
14             #pod =head1 DESCRIPTION
15             #pod
16             #pod This module imports a hash of object type package names from L<JIRA::REST::Class::FactoryTypes|JIRA::REST::Class::FactoryTypes>.
17             #pod
18             #pod =cut
19              
20             # we import the list of every class this factory knows how to make
21             #
22 4     4   261 use JIRA::REST::Class::FactoryTypes qw( %TYPES );
  4         6  
  4         386  
23             JIRA::REST::Class::Factory->add_factory_type( %TYPES );
24              
25 4     4   26 use Carp;
  4         5  
  4         168  
26 4     4   1373 use DateTime::Format::Strptime;
  4         1610649  
  4         16  
27              
28             #pod =internal_method B<init>
29             #pod
30             #pod Initialize the factory object. Just copies all the elements in the hashref that were passed in to the object itself.
31             #pod
32             #pod =cut
33              
34             sub init {
35 80     80 1 1229 my $self = shift;
36 80         100 my $args = shift;
37 80         203 my @keys = keys %$args;
38 80         115 @{$self}{@keys} = @{$args}{@keys};
  80         148  
  80         137  
39 80         876 return $self;
40             }
41              
42             #pod =internal_method B<get_factory_class>
43             #pod
44             #pod Inherited method from L<Class::Factory|Class::Factory/Factory_Methods>.
45             #pod
46             #pod =internal_method B<make_object>
47             #pod
48             #pod A tweaked version of C<make_object_for_type> from
49             #pod L<Class::Factory::Enhanced|Class::Factory::Enhanced/make_object_for_type>
50             #pod that calls C<init()> with a copy of the factory.
51             #pod
52             #pod =cut
53              
54             sub make_object {
55 36     36 1 97 my ( $self, $object_type, @args ) = @_;
56 36         88 my $class = $self->get_factory_class( $object_type );
57 36         382 my $obj = $class->new( @args );
58 36         376 $obj->init( $self ); # make sure we pass the factory into init()
59 36         215 return $obj;
60             }
61              
62             #pod =internal_method B<make_date>
63             #pod
64             #pod Make it easy to get L<DateTime|DateTime> objects from the factory. Parses
65             #pod JIRA date strings, which are in a format that can be parsed by the
66             #pod L<DateTime::Format::Strptime|DateTime::Format::Strptime> patterns
67             #pod C<%FT%T.%N%z> or C<%F>
68             #pod
69             #pod =cut
70              
71             sub make_date {
72 0     0 1   my ( $self, $date ) = @_;
73 0 0         return unless $date;
74 0 0         my $pattern = ( $date =~ m/\dt\d/ ) ? '%FT%T.%N%z' : '%F';
75              
76 0           my $parser = DateTime::Format::Strptime->new(
77             pattern => $pattern,
78             on_error => 'croak',
79             );
80             return (
81 0   0       $parser->parse_datetime( $date )
82             or
83             confess qq{Unable to parse date "$date" using pattern "$pattern"}
84             );
85             }
86              
87             #pod =internal_method B<factory_error>
88             #pod
89             #pod Throws errors from the factory with stack traces
90             #pod
91             #pod =cut
92              
93             sub factory_error {
94 0     0 1   my ( $class, $err, @args ) = @_;
95              
96             # start the stacktrace where we called make_object()
97 0           local $Carp::CarpLevel = $Carp::CarpLevel + 1;
98 0           Carp::confess "$err\n", @args;
99             }
100              
101             1;
102              
103             __END__
104              
105             =pod
106              
107             =encoding UTF-8
108              
109             =for :stopwords Packy Anderson Alexandr Alexey Ciornii Heumann Manni Melezhik
110              
111             =head1 NAME
112              
113             JIRA::REST::Class::Factory - A factory class for building all the other classes in L<JIRA::REST::Class|JIRA::REST::Class>.
114              
115             =head1 VERSION
116              
117             version 0.12
118              
119             =head1 DESCRIPTION
120              
121             This module imports a hash of object type package names from L<JIRA::REST::Class::FactoryTypes|JIRA::REST::Class::FactoryTypes>.
122              
123             =head1 INTERNAL METHODS
124              
125             =head2 B<init>
126              
127             Initialize the factory object. Just copies all the elements in the hashref that were passed in to the object itself.
128              
129             =head2 B<get_factory_class>
130              
131             Inherited method from L<Class::Factory|Class::Factory/Factory_Methods>.
132              
133             =head2 B<make_object>
134              
135             A tweaked version of C<make_object_for_type> from
136             L<Class::Factory::Enhanced|Class::Factory::Enhanced/make_object_for_type>
137             that calls C<init()> with a copy of the factory.
138              
139             =head2 B<make_date>
140              
141             Make it easy to get L<DateTime|DateTime> objects from the factory. Parses
142             JIRA date strings, which are in a format that can be parsed by the
143             L<DateTime::Format::Strptime|DateTime::Format::Strptime> patterns
144             C<%FT%T.%N%z> or C<%F>
145              
146             =head2 B<factory_error>
147              
148             Throws errors from the factory with stack traces
149              
150             =head1 RELATED CLASSES
151              
152             =over 2
153              
154             =item * L<JIRA::REST::Class|JIRA::REST::Class>
155              
156             =item * L<JIRA::REST::Class::FactoryTypes|JIRA::REST::Class::FactoryTypes>
157              
158             =back
159              
160             =head1 AUTHOR
161              
162             Packy Anderson <packy@cpan.org>
163              
164             =head1 COPYRIGHT AND LICENSE
165              
166             This software is Copyright (c) 2017 by Packy Anderson.
167              
168             This is free software, licensed under:
169              
170             The Artistic License 2.0 (GPL Compatible)
171              
172             =cut