Module: ActiveScaffold::Helpers::NumberHelpers

Included in:
ViewHelpers
Defined in:
lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/number_helpers.rb

Overview

Provides methods for converting a number into a formatted string that currently represents one of the following forms: phone number, percentage, money, or precision level.

Instance Method Summary collapse

Instance Method Details

#usa_number_to_phone(number, options = {}) ⇒ Object

Formats a number into a US phone number string. The options can be a hash used to customize the format of the output. The area code can be surrounded by parentheses by setting :area_code to true; default is false The delimiter can be set using :delimiter; default is “-” Examples:

usa_number_to_phone(1235551234)   => 123-555-1234
usa_number_to_phone(1235551234, {:area_code => true})   => (123) 555-1234
usa_number_to_phone(1235551234, {:delimiter => " "})    => 123 555 1234
usa_number_to_phone(1235551234, {:area_code => true, :extension => 555})  => (123) 555-1234 x 555


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/number_helpers.rb', line 42

def usa_number_to_phone(number, options = {})
  return "" if number.nil?
  options[:area_code] ||= false
  options[:delimiter] ||= "-"
  options[:extension] ||= ""
  begin
    str = options[:area_code] == true ? number.to_s.gsub(/([0-9]{3})([0-9]{3})([0-9]{4})/,"(\\1) \\2#{options[:delimiter]}\\3") : number.to_s.gsub(/([0-9]{3})([0-9]{3})([0-9]{4})/,"\\1#{options[:delimiter]}\\2#{options[:delimiter]}\\3")
    options[:extension].to_s.strip.empty? ? str : "#{str} x #{options[:extension].to_s.strip}"
  rescue
    number
  end
end

#usa_number_to_ssn(number, options = {}) ⇒ Object

Formats a number into a US ssn number string. The options can be a hash used to customize the format of the output. The delimiter can be set using :delimiter; default is “-” Examples:

usa_number_to_ssn(123551234)   => 123-55-1234


10
11
12
13
14
15
16
17
18
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/number_helpers.rb', line 10

def usa_number_to_ssn(number, options = {})
  return "" if number.nil?
  delimiter = options[:delimiter] || "-"
  begin
    str = number.to_s.gsub(/([0-9]{3})([0-9]{2})([0-9]{4})/,"\\1#{delimiter}\\2#{delimiter}\\3")
  rescue
    number
  end
end

#usa_number_to_zip(number, options = {}) ⇒ Object

Formats a number into a US zip code string. The options can be a hash used to customize the format of the output. The delimiter can be set using :delimiter; default is “-” Examples:

usa_number_to_zip(123551234)   => 12355-1234


24
25
26
27
28
29
30
31
32
# File 'lib/six-updater-web/vendor/plugins/active_scaffold/lib/active_scaffold/helpers/number_helpers.rb', line 24

def usa_number_to_zip(number, options = {})
  return "" if number.nil?
  delimiter = options[:delimiter] || "-"
  begin
    str = number.to_s.gsub(/([0-9]{5})([0-9]{4})/,"\\1#{delimiter}\\2")
  rescue
    number
  end
end