Sha256: 521bf33534472de1174c6e3284cd855e069d16b337a79f1640e4bc9294ec1b7f

Contents?: true

Size: 697 Bytes

Versions: 2

Compression:

Stored size: 697 Bytes

Contents

module ISO3166
  ##
  # Handles building the in memory store of countries data
  class Data
    @@cache = nil
    def initialize(alpha2)
      @alpha2 = alpha2.to_s.upcase
      Data.load_cache unless @@cache
    end

    def call
       @@cache[@alpha2]
    end

    def self.codes
      @@codes ||= Data.load_yaml(['data', 'countries.yaml']).freeze
    end

    private

    def self.datafile_path(file_array)
      File.join([File.dirname(__FILE__), '..'] + file_array)
    end

    def self.load_yaml(file_array)
      YAML.load_file(datafile_path(file_array))
    end

    def self.load_cache
      @@cache ||= Marshal.load(File.binread(datafile_path %w(cache countries )))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
countries-1.1.0 lib/countries/data.rb
countries-1.0.0 lib/countries/data.rb