Sha256: 2d4e219dabfdbd2319ee30038d20125ad6345487d5e0e2f722b1ab9ddb02c6d6

Contents?: true

Size: 1.4 KB

Versions: 7

Compression:

Stored size: 1.4 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Shared

    class InvalidTerritoryError < StandardError; end

    class PostalCodes

      class << self

        def territories
          resource.keys
        end

        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

        private

        def territory_cache
          @territory_cache ||= {}
        end

        def resource
          @resource ||= TwitterCldr.get_resource(:shared, :postal_codes)
        end

      end

      attr_reader :territory, :regexp, :ast

      def initialize(territory, regexp, ast)
        @territory = territory
        @regexp = regexp
        @ast = ast
      end

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

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

      private

      def generator
        generator_cache[territory] ||= PostalCodeGenerator.new(ast)
      end

      def generator_cache
        @@generator_cache ||= {}
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
twitter_cldr-3.0.10 lib/twitter_cldr/shared/postal_codes.rb
twitter_cldr-3.0.9 lib/twitter_cldr/shared/postal_codes.rb
twitter_cldr-3.0.8 lib/twitter_cldr/shared/postal_codes.rb
twitter_cldr-3.0.7 lib/twitter_cldr/shared/postal_codes.rb
twitter_cldr-3.0.6 lib/twitter_cldr/shared/postal_codes.rb
twitter_cldr-3.0.5 lib/twitter_cldr/shared/postal_codes.rb
twitter_cldr-3.0.4 lib/twitter_cldr/shared/postal_codes.rb