happy_phone_number 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +7 -5
- data/lib/happy_phone_number.rb +17 -8
- data/lib/happy_phone_number/formatter.rb +4 -4
- data/lib/happy_phone_number/version.rb +1 -1
- metadata +2 -2
data/README.markdown
CHANGED
@@ -37,6 +37,11 @@ Or in uppercase if you prefer:
|
|
37
37
|
<%= @contact.happy_phone(:FR) %>
|
38
38
|
#=> "01 23 45 67 89"
|
39
39
|
|
40
|
+
Or maybe with a string:
|
41
|
+
|
42
|
+
<%= @contact.happy_phone('fr') %>
|
43
|
+
#=> "01 23 45 67 89"
|
44
|
+
|
40
45
|
If you want an international format:
|
41
46
|
|
42
47
|
<%= @contact.happy_inter_phone(:fr) %>
|
@@ -75,19 +80,16 @@ Now, imagine you live in Belgium, where phone numbers could have 2 formats:
|
|
75
80
|
And if *Happy Phone Number* don't know your country, you could use a
|
76
81
|
**mask formatting**:
|
77
82
|
|
78
|
-
<%= @contact.
|
83
|
+
<%= @contact.happy_mask_phone("#### ###-###") %>
|
79
84
|
#=> "0123 456-789"
|
80
85
|
|
81
|
-
### Caveats
|
82
|
-
|
83
|
-
*Mask formatting* doesn't work for international phone number format.
|
84
86
|
|
85
87
|
Install
|
86
88
|
-------------------------
|
87
89
|
|
88
90
|
Add this line in your Gemfile:
|
89
91
|
|
90
|
-
gem 'happy_phone_number'
|
92
|
+
gem 'happy_phone_number'
|
91
93
|
|
92
94
|
|
93
95
|
Usage
|
data/lib/happy_phone_number.rb
CHANGED
@@ -18,13 +18,16 @@ require 'happy_phone_number/base_format'
|
|
18
18
|
# <%= @contact.happy_phone(:fr) %>
|
19
19
|
# # => "01 23 45 67 89"
|
20
20
|
#
|
21
|
+
# <%= @contact.happy_phone('fr') %>
|
22
|
+
# # => "01 23 45 67 89"
|
23
|
+
#
|
21
24
|
# <%= @contact.happy_inter_phone(:fr) %>
|
22
25
|
# # => "+33 1 23 45 67 89"
|
23
26
|
#
|
24
27
|
# <%= @contact.happy_phone(:be) %>
|
25
28
|
# # => "03 111 22 33"
|
26
29
|
#
|
27
|
-
# <%= @contact.
|
30
|
+
# <%= @contact.happy_mask_phone("#### ###-###") %>
|
28
31
|
# # => "0123 456-789"
|
29
32
|
module HappyPhoneNumber
|
30
33
|
extend ActiveSupport::Concern
|
@@ -39,23 +42,29 @@ module HappyPhoneNumber
|
|
39
42
|
|
40
43
|
# Public: Format a phone number from a model.
|
41
44
|
#
|
42
|
-
# args[0] - Either a Symbol country or a String formatting mask.
|
43
|
-
# A country
|
45
|
+
# args[0] - Either a Symbol|String country or a String formatting mask.
|
46
|
+
# A country must be in ISO 3166-1-alpha-2 format,
|
44
47
|
# both lower and upper case are valids.
|
45
48
|
# block - Not used.
|
46
49
|
#
|
47
50
|
# Signature
|
48
51
|
#
|
49
|
-
# happy_<field>(
|
50
|
-
# happy_inter_<field>(
|
52
|
+
# happy_<field>(country, separator)
|
53
|
+
# happy_inter_<field>(country, separator)
|
54
|
+
# happy_mask_<field>(mask)
|
51
55
|
#
|
52
|
-
# field
|
53
|
-
#
|
56
|
+
# field - A field name of the model that holds a phone number as a
|
57
|
+
# string.
|
58
|
+
# country - Symbol or String in ISO 3166-1-alpha-2 format.
|
59
|
+
# separator - A String used to separate groups of number.
|
60
|
+
# mask - A String formatting mask.
|
54
61
|
#
|
55
62
|
# Returns the String formatted phone number.
|
56
63
|
def method_missing(meth, *args, &block)
|
57
64
|
if meth.to_s =~ /^happy_inter_(.+)$/
|
58
65
|
happy_format(:international, $1, *args)
|
66
|
+
elsif meth.to_s =~ /^happy_mask_(.+)$/
|
67
|
+
happy_format(:mask, $1, *args)
|
59
68
|
elsif meth.to_s =~ /^happy_(.+)$/
|
60
69
|
happy_format(:national, $1, *args)
|
61
70
|
else
|
@@ -76,7 +85,7 @@ module HappyPhoneNumber
|
|
76
85
|
|
77
86
|
# Launch the formatting process.
|
78
87
|
#
|
79
|
-
# type - Either :national or :
|
88
|
+
# type - Either :national, :international or :mask.
|
80
89
|
# meth - The String field from the model.
|
81
90
|
# args - Argument(s) passed to the formatting method, like a country,
|
82
91
|
# a mask string, the separator.
|
@@ -8,9 +8,9 @@ module HappyPhoneNumber
|
|
8
8
|
# Internal: Initialize the formatter facade.
|
9
9
|
#
|
10
10
|
# phone - A String phone number to format.
|
11
|
-
# type -
|
12
|
-
# country_or_mask -
|
13
|
-
#
|
11
|
+
# type - :national | :international | :mask.
|
12
|
+
# country_or_mask - The country as Symbol or String in
|
13
|
+
# ISO 3166-1-alpha-2, or a String mask.
|
14
14
|
# separator - Optional, a String to separate groups of digits
|
15
15
|
# the final result.
|
16
16
|
def initialize phone, type, country_or_mask, separator
|
@@ -24,7 +24,7 @@ module HappyPhoneNumber
|
|
24
24
|
#
|
25
25
|
# Returns the String formatted phone number.
|
26
26
|
def format
|
27
|
-
if @
|
27
|
+
if @type == :mask
|
28
28
|
use_mask
|
29
29
|
else
|
30
30
|
use_formatter
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: happy_phone_number
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|