Class: TwitterCldr::Shared::PostalCodes

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/shared/postal_codes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(territory, regexp, ast) ⇒ PostalCodes

Returns a new instance of PostalCodes.



46
47
48
49
50
51
52
53
54
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 46

def initialize(territory, regexp, ast)
  @territory = territory
  @regexp = regexp
  if @regexp
    @single_regexp = build_regexp "\\A#{regexp.source}\\z"
    @multi_regexp = build_regexp "\\b#{regexp.source}\\b"
  end
  @ast = ast
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



44
45
46
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 44

def ast
  @ast
end

#multi_regexpObject (readonly)

Returns the value of attribute multi_regexp.



44
45
46
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 44

def multi_regexp
  @multi_regexp
end

#regexpObject (readonly)

Returns the value of attribute regexp.



44
45
46
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 44

def regexp
  @regexp
end

#single_regexpObject (readonly)

Returns the value of attribute single_regexp.



44
45
46
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 44

def single_regexp
  @single_regexp
end

#territoryObject (readonly)

Returns the value of attribute territory.



44
45
46
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 44

def territory
  @territory
end

Class Method Details

.for_territory(territory) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 19

def for_territory(territory)
  key = territory.to_s.downcase.to_sym
  if res = resource[key]
    territory_cache[key] ||= new(
      territory,
      res[:regex],
      TwitterCldr::Utils::RegexpAst.load(res[:ast])
    )
  else
    raise InvalidTerritoryError, "invalid territory"
  end
end

.territoriesObject



15
16
17
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 15

def territories
  resource.keys
end

Instance Method Details

#find_all(postal_codes) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 60

def find_all(postal_codes)
  # we cannot use String#scan here as some of the CLDR regular
  # expressions have capture groups while others don't making
  # it completely unpredictable what that method might return
  offset = 0; matches = []
  while match = multi_regexp.match(postal_codes, offset)
    matches << match[0]
    offset += match.offset(0)[1]
  end
  matches
end

#sample(sample_size = 1) ⇒ Object



72
73
74
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 72

def sample(sample_size = 1)
  generator.sample(sample_size)
end

#valid?(postal_code) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/twitter_cldr/shared/postal_codes.rb', line 56

def valid?(postal_code)
  !!(single_regexp && single_regexp =~ postal_code)
end