Class: Phony::TrunkCode

Inherits:
Object
  • Object
show all
Defined in:
lib/phony/trunk_code.rb

Instance Method Summary collapse

Constructor Details

#initialize(code, options = {}) ⇒ TrunkCode

Parameters:

* code: The trunk code, e.g. 0.

Options:

* normalize: [true (default), false] Remove the trunk code when normalizing (only use if number scheme is defined unambiguously).
* split: [true, false (default)] Remove the trunk code when splitting (only use if number scheme is defined unambiguously).
* format: [true (default), false] Add the trunk code when formatting (passing `false` will not add it).


13
14
15
16
17
18
19
# File 'lib/phony/trunk_code.rb', line 13

def initialize(code, options = {})
  @code = code
  @trunk_code_replacement = /\A#{code.gsub(/%s/, '')}/
  @normalize = options[:normalize] || options[:normalize].nil?
  @split     = options[:split]
  @format    = options[:format] || options[:format].nil?
end

Instance Method Details

#format(space, force = nil) ⇒ Object

Format the trunk code using the spaces given.



45
46
47
48
49
50
51
52
53
# File 'lib/phony/trunk_code.rb', line 45

def format(space, force = nil)
  return unless force || @format
    if @code.size > 1
      (@code % space).gsub(/\D/, ' ')
    else
      @code
    end
  
end

#normalize(national_number, options = {}) ⇒ Object

Normalize normalizes the given national number.



38
39
40
41
# File 'lib/phony/trunk_code.rb', line 38

def normalize(national_number, options = {})
  national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @normalize && options[:cc]
  national_number
end

#split(national_number) ⇒ Object

Split gets a number without country code and splits it into its parts.



31
32
33
34
# File 'lib/phony/trunk_code.rb', line 31

def split(national_number)
  national_number.gsub! @trunk_code_replacement, EMPTY_STRING if @split
  [self, national_number]
end

#|(other) ⇒ Object

Prepends itself to the other codes.



23
24
25
26
# File 'lib/phony/trunk_code.rb', line 23

def |(other)
  other.codes.unshift self
  other
end