- Timestamp:
- 03/30/09 00:04:44 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Geo-Direction-Name/trunk/lib/Geo/Direction/Name.pm
r21013 r31677 5 5 use Carp; 6 6 7 use version; our $VERSION = qv('0.0. 3');7 use version; our $VERSION = qv('0.0.4'); 8 8 use Scalar::Util qw(looks_like_number); 9 9 use Class::Inspector; … … 19 19 20 20 sub new { 21 my $class = shift; 22 my $locale = shift || "en_US"; 21 my ( $class, $opt ) = @_; 23 22 24 23 my $self = bless {}, $class; 25 24 26 $self->locale($locale); 25 my $spec; 26 my $locale; 27 if ( $opt && ref($opt) ) { 28 $spec = $opt->{spec}; 29 $locale = $opt->{locale}; 30 } else { 31 # For backward compatibility 32 $locale = $opt; 33 } 34 35 $self->spec( $spec ); 36 $self->locale( $locale ); 27 37 28 38 $self; 29 39 } 30 40 41 sub spec { 42 my $self = shift; 43 my $spec = shift; 44 my $noerr = shift; 45 my $errstr = "Specification class(subclass of Geo::Direction::Name::Spec) must be set"; 46 $spec ||= 'default' unless ( $self->{spec} ); 47 48 if ( $spec ) { 49 $errstr = "Geo::Direction::Name not support this specification now: " . $spec; 50 delete $self->{spec}; 51 my $class = __PACKAGE__ . '::Spec' . ( $spec eq 'default' ? '' : '::' . ucfirst($spec) ) ; 52 if( Class::Inspector->loaded($class) || $class->require ) { 53 $self->{spec} = $class->new; 54 } 55 } 56 57 croak $errstr unless ( $noerr || $self->{spec} ); 58 $self->{spec}; 59 } 60 31 61 sub locale { 32 my $self = shift; 33 if ($_[0]) { 34 my $locale = Geo::Direction::Name->load($_[0]); 35 36 croak("Geo::Direction::Name not support this locale now: " . $_[0]) unless ($locale); 37 38 $self->{locale} = $locale; 39 } 40 41 $self->{locale}; 62 my $self = shift; 63 64 $self->spec->locale( @_ ); 42 65 } 43 66 44 67 sub to_string { 45 68 my $self = shift; 46 my $direction = shift; 47 my $option = shift || {}; 48 49 my $abbr = defined($option->{abbreviation}) ? $option->{abbreviation} : 1; 50 my $devide = $option->{devide} || 8; 51 52 croak ("Direction value must be a number") unless (looks_like_number($direction)); 53 croak ("Abbreviation parameter must be 0 or 1") if ( $abbr !~ /^[01]$/ ); 54 my $log2 = log($devide)/log(2); 55 croak ("Devide parameter must be 4, 8, 16 or 32") if ( $log2 !~ /^[2345]$/ ); 56 57 $direction += 180 / $devide; 58 59 while ($direction < 0.0 || $direction >= 360.0) { 60 $direction += $direction < 0 ? 360.0 : -360.0; 61 } 62 63 my $i = int($direction * $devide / 360) * (32 / $devide); 64 65 $self->locale->string($i,$abbr); 69 70 $self->spec->to_string( @_ ); 66 71 } 67 72 68 73 sub from_string { 69 74 my $self = shift; 70 my $string = shift; 71 72 $self->locale->direction($string); 73 } 74 75 sub load { 76 my $class = shift; 77 my $locale = shift; 78 79 ($locale) = split(/\./,$locale); 80 my ($lang) = split(/_/,$locale); 81 82 foreach my $class (map { "Geo::Direction::Name::Locale::" . $_ } ($locale, $lang)) { 83 if( Class::Inspector->loaded($class) || $class->require) { 84 return $class->new; 85 } 86 } 87 return; 88 } 75 76 $self->spec->from_string( @_ ); 77 } 78 89 79 90 80 1; # Magic true value required at end of module … … 151 141 =item * new( [LOCALE] ) 152 142 143 Old interface (Backward compatible). 153 144 Return the Geo::Direction::Name object. 154 145 LOCALE is optional, default is "en_US". 146 147 =item * new( [OPTION] ) 148 149 New interface. 150 OPTION is hash reference, could be set two values. 151 Default specification is used. 152 153 =over 8 154 155 =item * spec 156 157 Set original spec name if want. 158 If not set, use default specification. 159 160 =item * locale 161 162 Set locale. 163 164 =back 155 165 156 166 =back … … 179 189 Default is 8. 180 190 191 After version 0.0.4, you can set other deviding number by using non-default specification. 192 193 181 194 =back 182 195 … … 191 204 192 205 =over 4 206 207 =item * spec 193 208 194 209 =item * locale
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)