File Coverage

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


line stmt bran cond sub pod time code
1             package JIRA::REST::Class::Factory;
2 4     4   1557 use parent qw( Class::Factory::Enhanced );
  4         244  
  4         44  
3 4     4   10681 use strict;
  4         9  
  4         82  
4 4     4   20 use warnings;
  4         7  
  4         94  
5 4     4   71 use 5.010;
  4         14  
6              
7             our $VERSION = '0.11';
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   354 use JIRA::REST::Class::FactoryTypes qw( %TYPES );
  4         10  
  4         533  
23             JIRA::REST::Class::Factory->add_factory_type( %TYPES );
24              
25 4     4   25 use Carp;
  4         10  
  4         203  
26 4     4   2383 use DateTime::Format::Strptime;
  4         1878904  
  4         87  
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 1469 my $self = shift;
36 80         135 my $args = shift;
37 80         254 my @keys = keys %$args;
38 80         156 @{$self}{@keys} = @{$args}{@keys};
  80         189  
  80         161  
39 80         1023 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 128 my ( $self, $object_type, @args ) = @_;
56 36         128 my $class = $self->get_factory_class( $object_type );
57 36         510 my $obj = $class->new( @args );
58 36         474 $obj->init( $self ); # make sure we pass the factory into init()
59 36         164 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> pattern
67             #pod C<%FT%T.%N%z>
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           my $pattern = '%FT%T.%N%z';
75 0           state $parser = DateTime::Format::Strptime->new( pattern => $pattern );
76             return (
77 0   0       $parser->parse_datetime( $date )
78             or
79             confess qq{Unable to parse date "$date" using pattern "$pattern"}
80             );
81             }
82              
83             #pod =internal_method B<factory_error>
84             #pod
85             #pod Throws errors from the factory with stack traces
86             #pod
87             #pod =cut
88              
89             sub factory_error {
90 0     0 1   my ( $class, $err, @args ) = @_;
91              
92             # start the stacktrace where we called make_object()
93 0           local $Carp::CarpLevel = $Carp::CarpLevel + 1;
94 0           Carp::confess "$err\n", @args;
95             }
96              
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =for :stopwords Packy Anderson Alexandr Alexey Ciornii Melezhik
106              
107             =head1 NAME
108              
109             JIRA::REST::Class::Factory - A factory class for building all the other classes in L<JIRA::REST::Class|JIRA::REST::Class>.
110              
111             =head1 VERSION
112              
113             version 0.11
114              
115             =head1 DESCRIPTION
116              
117             This module imports a hash of object type package names from L<JIRA::REST::Class::FactoryTypes|JIRA::REST::Class::FactoryTypes>.
118              
119             =head1 INTERNAL METHODS
120              
121             =head2 B<init>
122              
123             Initialize the factory object. Just copies all the elements in the hashref that were passed in to the object itself.
124              
125             =head2 B<get_factory_class>
126              
127             Inherited method from L<Class::Factory|Class::Factory/Factory_Methods>.
128              
129             =head2 B<make_object>
130              
131             A tweaked version of C<make_object_for_type> from
132             L<Class::Factory::Enhanced|Class::Factory::Enhanced/make_object_for_type>
133             that calls C<init()> with a copy of the factory.
134              
135             =head2 B<make_date>
136              
137             Make it easy to get L<DateTime|DateTime> objects from the factory. Parses
138             JIRA date strings, which are in a format that can be parsed by the
139             L<DateTime::Format::Strptime|DateTime::Format::Strptime> pattern
140             C<%FT%T.%N%z>
141              
142             =head2 B<factory_error>
143              
144             Throws errors from the factory with stack traces
145              
146             =head1 RELATED CLASSES
147              
148             =over 2
149              
150             =item * L<JIRA::REST::Class|JIRA::REST::Class>
151              
152             =item * L<JIRA::REST::Class::FactoryTypes|JIRA::REST::Class::FactoryTypes>
153              
154             =back
155              
156             =head1 AUTHOR
157              
158             Packy Anderson <packy@cpan.org>
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is Copyright (c) 2017 by Packy Anderson.
163              
164             This is free software, licensed under:
165              
166             The Artistic License 2.0 (GPL Compatible)
167              
168             =cut