Class: Phoney::Region

Inherits:
Object
  • Object
show all
Defined in:
lib/phoney/region.rb

Constant Summary collapse

REGION_FILE =
File.join(File.dirname(__FILE__), '..', 'data', 'regions.bin')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Region

Returns a new instance of Region.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/phoney/region.rb', line 36

def initialize(options={})
  @country_abbr = options[:country_abbr]
  @country_code = options[:country_code]

  @trunk_prefixes   = options[:trunk_prefixes]
  @dialout_prefixes = options[:dialout_prefixes]

  @rule_sets = SortedSet.new
  
  (options[:rule_sets]||[]).each do |rule_group|
    @rule_sets.add RuleGroup.new(rule_group[:significant_digits], rule_group[:rules])
  end
end

Instance Attribute Details

#country_abbrObject (readonly)

Returns the value of attribute country_abbr.



7
8
9
# File 'lib/phoney/region.rb', line 7

def country_abbr
  @country_abbr
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



7
8
9
# File 'lib/phoney/region.rb', line 7

def country_code
  @country_code
end

#dialout_prefixesObject (readonly)

Returns the value of attribute dialout_prefixes.



8
9
10
# File 'lib/phoney/region.rb', line 8

def dialout_prefixes
  @dialout_prefixes
end

#rule_setsObject (readonly)

Returns the value of attribute rule_sets.



9
10
11
# File 'lib/phoney/region.rb', line 9

def rule_sets
  @rule_sets
end

#trunk_prefixesObject (readonly)

Returns the value of attribute trunk_prefixes.



8
9
10
# File 'lib/phoney/region.rb', line 8

def trunk_prefixes
  @trunk_prefixes
end

Class Method Details

.[](param) ⇒ Object



31
32
33
# File 'lib/phoney/region.rb', line 31

def [](param)
  find(param)
end

.allObject



21
22
23
# File 'lib/phoney/region.rb', line 21

def all
  @@regions.empty? ? load : @@regions
end

.find(param) ⇒ Object



25
26
27
28
29
# File 'lib/phoney/region.rb', line 25

def find(param)    
  all.detect do |region|
    region.country_code.to_s == param.to_s || region.country_abbr.to_s == param.to_s
  end
end

.loadObject



12
13
14
15
16
17
18
19
# File 'lib/phoney/region.rb', line 12

def load
  @@regions = []
  Marshal.load(File.read(REGION_FILE)).each_pair do |key, region_hash|
    new_region = Region.new region_hash
    @@regions.push new_region
  end
  @@regions
end

Instance Method Details

#to_sObject



50
51
52
# File 'lib/phoney/region.rb', line 50

def to_s
  country_abbr
end