| 1 | package JIRA::REST::Class::Factory; | ||
| 2 | use parent qw( Class::Factory::Enhanced ); | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.010; | ||
| 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 | use JIRA::REST::Class::FactoryTypes qw( %TYPES ); | ||
| 23 | JIRA::REST::Class::Factory->add_factory_type( %TYPES ); | ||
| 24 | |||
| 25 | use Carp; | ||
| 26 | use DateTime::Format::Strptime; | ||
| 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 | my $self = shift; | ||
| 36 | my $args = shift; | ||
| 37 | my @keys = keys %$args; | ||
| 38 | @{$self}{@keys} = @{$args}{@keys}; | ||
| 39 | 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 | my ( $self, $object_type, @args ) = @_; | ||
| 56 | my $class = $self->get_factory_class( $object_type ); | ||
| 57 | my $obj = $class->new( @args ); | ||
| 58 | $obj->init( $self ); # make sure we pass the factory into init() | ||
| 59 | 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 | my ( $self, $date ) = @_; | ||
| 73 | return unless $date; | ||
| 74 | my $pattern = '%FT%T.%N%z'; | ||
| 75 | state $parser = DateTime::Format::Strptime->new( pattern => $pattern ); | ||
| 76 | return ( | ||
| 77 | $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 | my ( $class, $err, @args ) = @_; | ||
| 91 | |||
| 92 | # start the stacktrace where we called make_object() | ||
| 93 | local $Carp::CarpLevel = $Carp::CarpLevel + 1; | ||
| 94 | 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 |