File Coverage

blib/lib/Geo/Address/Mail.pm
Criterion Covered Total %
statement 33 33 100.0
branch 2 2 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Geo::Address::Mail;
2 3     3   36742 use warnings;
  3         8  
  3         121  
3 3     3   19 use strict;
  3         32  
  3         942  
4 3     3   2485 use Moose;
  3         741035  
  3         25  
5              
6 3     3   24447 use Class::MOP;
  3         9  
  3         66  
7 3     3   1277 use MooseX::Storage;
  3         66541  
  3         25  
8              
9             with qw(MooseX::Storage::Deferred);
10             with qw(MooseX::Clone);
11              
12             our $VERSION = '0.04';
13              
14             =head1 NAME
15              
16             Geo::Address::Mail - A Mailing Address on Earth
17              
18             =head1 SYNOPSIS
19              
20             Geo::Address::Mail provides a generic object representation of a mailing
21             address that may be subclassed to provide more specific typing of attributes.
22             The core class, Geo::Address::Mail provides common, loosely typed attributes
23             and methods.
24              
25             use Geo::Address::Mail::US;
26              
27             my $add = Geo::Address::Mail::US->new(
28             name => 'Cory G Watson',
29             street => '123 Main St',
30             city => 'Testville',
31             postal_code => '12345'
32             );
33              
34             =head1 SUBCLASSING
35              
36             The real reason for Geo::Address::Mail is to provide a common class that can
37             be used to build mailing address objects for other countries.
38              
39             Subclasses are expected additional type refinement and attributes.
40             For example, L<Geo::Address::Mail::US> uses a more specific type for
41             validation USPS ZIP codes and adds a C<street2> attribute for an optional
42             additional line of addressing.
43              
44             =head2 SUBCLASS NAMING
45              
46             Subclasses should be named with C<Geo::Address::Mail> and the two-letter
47             ISO-3166 country codes.
48              
49             =head1 ADDITIONAL USES
50              
51             Using a common address object enables a family of distributions that provide
52             interesting address functionality such as L<Geo::Address::Mail::Standardizer>.
53              
54             =head1 ATTRIBUTES
55              
56             =head2 city
57              
58             The city/town/village/municipality in which this address resides.
59              
60             =cut
61              
62             has 'city' => (
63             is => 'rw',
64             isa => 'Str'
65             );
66              
67             =head2 company
68              
69             The name of the company that is to receive the mail piece.
70              
71             =cut
72              
73             has 'company' => (
74             is => 'rw',
75             isa => 'Str'
76             );
77              
78             =head2 country
79              
80             The country in which this address resides. This is likely not necessary
81             unless the address is in a different country.
82              
83             =cut
84              
85             has 'country' => (
86             is => 'rw',
87             isa => 'Str'
88             );
89              
90             =head2 name
91              
92             The name of the person that is to receive the mail piece.
93              
94             =cut
95              
96             has 'name' => (
97             is => 'rw',
98             isa => 'Str'
99             );
100              
101             =head2 postal_code
102              
103             The postal code of the address. Called the ZIP code in the US.
104              
105             =cut
106              
107             has 'postal_code' => (
108             is => 'rw',
109             isa => 'Str',
110             );
111              
112             =head2 street
113              
114             The number and name of the street that is to receive the mail piece.
115              
116             2020 Main St
117              
118             =cut
119              
120             has 'street' => (
121             is => 'rw',
122             isa => 'Str'
123             );
124              
125             =head1 METHODS
126              
127             =head2 clone(%prams)
128              
129             Close this Geo::Address::Mail instance, replacing any of the attributes
130             specified in the new instance.
131              
132             =cut
133              
134             =head2 new_for_country ($code, ...)
135              
136             Attempts to load and instantiate a subclass of Geo::Address::Mail based on the
137             provided two-letter code (upper or lower case). Any remaining arguments are
138             passed to the constructor of the specified class. B<Note:> you will have to
139             have the class for the specified country accessible.
140              
141             # Instantiate a US address
142             my $usaddr = Geo::Address::Mail->new_for_country('US', { ... });
143              
144             =cut
145              
146             sub new_for_country {
147 1     1 1 16 my $self = shift;
148 1         3 my $code = shift;
149              
150             # Upper-case it, just in case
151 1         3 $code = uc($code);
152              
153 1         4 my $class = 'Geo::Address::Mail::'. $code;
154 1         7 Class::MOP::load_class($class);
155 1         92 return $class->new(@_);
156             }
157              
158             =head2 standardize ($partial_name_of_standardizer_class, ...)
159              
160             Shortcut to use a L<Geo::Address::Mail::Standardizer>. You can provide
161             either the partial name (the bits B<after> Standardizer, e.g. 'USPS::AMS' for
162             L<Geo::Address::Mail::Standardizer::USPS::AMS>) or a full class name (prefixed
163             with a +, e.g. '+My::Standardizer'). Returns whatever is returned by the
164             C<standardize> method of the requested Standardizer implementation.
165              
166             B<Note:> Any argument passed after the name of the class will be passed on
167             to the constructer of the loaded standardizer.
168              
169             =cut
170              
171             sub standardize {
172 2     2 1 1256 my $self = shift;
173 2         6 my $std = shift;
174              
175 2         5 my $stdclass = $std;
176 2 100       12 if($std =~ /^\+/) {
177 1         6 $stdclass =~ s/^\+//g;
178             } else {
179 1         4 $stdclass = 'Geo::Address::Mail::Standardizer::'.$stdclass;
180             }
181              
182 2         14 Class::MOP::load_class($stdclass);
183 2         22727 my $instance = $stdclass->new(@_);
184 2         747 return $instance->standardize($self);
185             }
186              
187             __PACKAGE__->meta->make_immutable;
188 3     3   1937 no Moose;
  3         8  
  3         20  
189             1;
190              
191             =head1 AUTHOR
192              
193             Cory G Watson, C<< <gphat at cpan.org> >>
194              
195             =head1 ACKNOWLEDGEMENTS
196              
197             =head1 COPYRIGHT & LICENSE
198              
199             Copyright 2010 Cory G Watson.
200              
201             This program is free software; you can redistribute it and/or modify it
202             under the terms of either: the GNU General Public License as published
203             by the Free Software Foundation; or the Artistic License.
204              
205             See http://dev.perl.org/licenses/ for more information.
206              
207             =cut