class TwitterCldr::Shared::PostalCodes

Attributes

ast_source[R]
multi_regexp[R]
regexp[R]
single_regexp[R]
territory[R]

Public Class Methods

for_territory(territory) click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 20
def for_territory(territory)
  key = territory.to_s.downcase.to_sym
  if res = resource[key]
    territory_cache[key] ||= new(
      territory, res[:regex], res[:ast]
    )
  else
    raise InvalidTerritoryError, "invalid territory"
  end
end
new(territory, regexp, ast_source) click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 45
def initialize(territory, regexp, ast_source)
  @territory = territory
  @regexp = regexp

  if @regexp
    @single_regexp = build_regexp "\\A#{regexp.source}\\z"
    @multi_regexp = build_regexp "\\b#{regexp.source}\\b"
  end

  @ast_source = ast_source
end
territories() click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 16
def territories
  resource.keys
end

Private Class Methods

resource() click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 37
def resource
  @resource ||= TwitterCldr.get_resource(:shared, :postal_codes)
end
territory_cache() click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 33
def territory_cache
  @territory_cache ||= {}
end

Public Instance Methods

find_all(postal_codes) click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 61
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
has_generator?() click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 77
def has_generator?
  !!ast_source
end
sample(sample_size = 1) click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 73
def sample(sample_size = 1)
  generator.sample(sample_size)
end
valid?(postal_code) click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 57
def valid?(postal_code)
  !!(single_regexp && single_regexp =~ postal_code)
end

Private Instance Methods

ast() click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 91
def ast
  @ast ||= begin
    if has_generator?
      TwitterCldr::Utils::RegexpAst.load(ast_source)
    else
      raise MissingPostcodeGeneratorError,
        "Couldn't find a postcode generator for territory #{territory}"
    end
  end
end
build_regexp(regexp_str, modifiers = '') click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 83
def build_regexp(regexp_str, modifiers = '')
  Regexp.new(regexp_str, modifiers)
end
generator() click to toggle source
# File lib/twitter_cldr/shared/postal_codes.rb, line 87
def generator
  @generator ||= PostalCodeGenerator.new(ast)
end