Module: HappyPhoneNumber

Extended by:
ActiveSupport::Concern
Defined in:
lib/happy_phone_number.rb,
lib/happy_phone_number/mask.rb,
lib/happy_phone_number/version.rb,
lib/happy_phone_number/be_format.rb,
lib/happy_phone_number/dk_format.rb,
lib/happy_phone_number/formatter.rb,
lib/happy_phone_number/fr_format.rb,
lib/happy_phone_number/is_format.rb,
lib/happy_phone_number/base_format.rb

Overview

Public: Happy Phone Number provides easy methods to format phone numbers from your Rails models. Virtually any country specific format could be supported, including national and international format. For the not (yet) supported countries, a general method using a simple mask is also provided.

Examples

class Contact < ActiveRecord::Base
  attr_accessible :email, :name, :phone, :fax
  happy_phone_number
end

<%= @contact.happy_phone(:fr) %>
# => "01 23 45 67 89"

<%= @contact.happy_phone('fr') %>
# => "01 23 45 67 89"

<%= @contact.happy_inter_phone(:fr) %>
# => "+33 1 23 45 67 89"

<%= @contact.happy_phone(:be) %>
# => "03 111 22 33"

<%= @contact.happy_mask_phone("#### ###-###") %>
# => "0123 456-789"

Defined Under Namespace

Modules: ClassMethods Classes: BaseFormat, BeFormat, DkFormat, Formatter, FrFormat, IsFormat, Mask

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Public: Format a phone number from a model.

args - Either a Symbol|String country or a String formatting mask.

A country must be in ISO 3166-1-alpha-2 format,
both lower and upper case are valids.

block - Not used.

Signature

happy_<field>(country, separator)
happy_inter_<field>(country, separator)
happy_mask_<field>(mask)

field - A field name of the model that holds a phone number as a

string.

country - Symbol or String in ISO 3166-1-alpha-2 format. separator - A String used to separate groups of number. mask - A String formatting mask.

Returns the String formatted phone number.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/happy_phone_number.rb', line 63

def method_missing(meth, *args, &block)
  if meth.to_s =~ /^happy_inter_(.+)$/
    happy_format(:international, $1, *args)
  elsif meth.to_s =~ /^happy_mask_(.+)$/
    happy_format(:mask, $1, *args)
  elsif meth.to_s =~ /^happy_(.+)$/
    happy_format(:national, $1, *args)
  else
    super
  end
end

Instance Method Details

#respond_to?(meth, include_private = false) ⇒ Boolean

Public: Make the happy_* methods respond to respond_to?.

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/happy_phone_number.rb', line 76

def respond_to?(meth, include_private = false)
  if meth.to_s =~ /^happy_.*$/
    true
  else
    super
  end
end