Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add config parameters for phones code

  • Loading branch information...
commit a6ce808536414841760ab3317524b8fa6f84d527 1 parent f303ed4
@rshadow rshadow authored
Showing with 39 additions and 17 deletions.
  1. +6 −0 debian/changelog
  2. +33 −17 lib/Mojolicious/Plugin/Human.pm
View
6 debian/changelog
@@ -1,3 +1,9 @@
+libmojolicious-plugin-human-perl (0.3) nowtaxi; urgency=low
+
+ * Add config parameters for phones code
+
+ -- Roman V. Nikolaev <rshadow@rambler.ru> Sat, 26 Jan 2013 15:15:56 +0400
+
libmojolicious-plugin-human-perl (0.2-1) nowtaxi; urgency=low
* Fix warnings
View
50 lib/Mojolicious/Plugin/Human.pm
@@ -10,7 +10,7 @@ use Carp;
use DateTime;
use DateTime::Format::DateParse;
-our $VERSION = '0.2';
+our $VERSION = '0.3';
=encoding utf-8
@@ -30,6 +30,9 @@ Mojolicious::Plugin::Human - Helpers to print values as human readable form.
datetime => '%d.%m.%Y %H:%M',
time => '%H:%M:%S',
date => '%d.%m.%Y',
+
+ phone_country => 1,
+ phone_region => 123,
});
=head1 DESCRIPTION
@@ -56,12 +59,18 @@ Set format for human readable date and time. Default: %F %H:%M
Set format for human readable time. Default: %H:%M:%S
-=item date
-
-=item time
+=item datetime
Set format for human readable date. Default: %F
+=item phone_country
+
+Set country code for phones functions. Default: 7
+
+=item phone_region
+
+Set region code for phones functions. Default: 495
+
=back
=head1 DATE AND TIME HELPERS
@@ -113,8 +122,8 @@ Get word base form and add some of suffix ($one, $two, $many) depends of $count
# Compiled regexp for placement level in the money functions
my $REGEXP_DIGIT = qr{^(-?\d+)(\d{3})};
-sub clean_phone($);
-sub human_phone($);
+sub clean_phone($$$);
+sub human_phone($$$);
sub date_parse($);
sub register {
@@ -129,6 +138,9 @@ sub register {
$conf->{time} //= '%H:%M:%S';
$conf->{date} //= '%F';
+ $conf->{phone_country} //= 7;
+ $conf->{phone_region} //= 495;
+
# Datetime
$app->helper(str2time => sub {
@@ -186,12 +198,16 @@ sub register {
my ($self, $str) = @_;
return '' unless $str;
my @phones = split /[\s,;:]+/, $str;
- return join ', ' => grep { $_ } map { human_phone $_ } @phones;
+ return join ', ' => grep { $_ } map {
+ human_phone $_, $conf->{phone_country}, $conf->{phone_region}
+ } @phones;
});
$app->helper(yandex_phone => sub {
my ($self, $phone) = @_;
- return clean_phone($phone) || '';
+ return clean_phone(
+ $phone, $conf->{phone_country}, $conf->{phone_region}
+ ) || '';
});
# Text
@@ -227,7 +243,7 @@ sub register {
=head1 INTERNAL FUNCIONS
-=head2 clean_phone
+=head2 clean_phone $phone, $country, $region
Clear phones. Fix first local digit 8 problem.
@@ -235,20 +251,20 @@ Return <undef> if phome not correct
=cut
-sub clean_phone($) {
- my ($phone) = @_;
+sub clean_phone($$$) {
+ my ($phone, $country, $region) = @_;
return undef unless $phone;
for ($phone) {
s/\D+//g;
- $_ = '495' . $_ if 7 == length;
+ $_ = $region . $_ if 7 == length;
return undef unless 10 <= length $phone;
if (11 == length $_) { # have a country code
- s/^8/7/;
+ s/^8/$country/;
} elsif (10 == length $_) { # havn`t country code
- s/^/7/;
+ s/^/$country/;
}
s/^/+/;
@@ -262,9 +278,9 @@ Make phone string in human readable form.
=cut
-sub human_phone($) {
- my ($phone) = @_;
- $phone = clean_phone $phone;
+sub human_phone($$$) {
+ my ($phone, $country, $region) = @_;
+ $phone = clean_phone $phone, $country, $region;
return $phone unless $phone;
$phone =~ s/(...)(...)(....)$/-$1-$2-$3/;
return $phone;
Please sign in to comment.
Something went wrong with that request. Please try again.