<commit_before>module ISO3166; end

class ISO3166::Country
  Setup = ISO3166::Setup.new

  AttrReaders = [
    :number,
    :alpha2,
    :alpha3,
    :name,
    :names,
    :latitude,
    :longitude,
    :continent,
    :region,
    :subregion,
    :world_region,
    :country_code,
    :national_destination_code_lengths,
    :national_number_lengths,
    :international_prefix,
    :national_prefix,
    :address_format,
    :translations,
    :translated_names,
    :ioc,
    :gec,
    :un_locode,
    :languages,
    :nationality,
    :dissolved_on,
    :eu_member,
    :alt_currency,
    :vat_rates,
    :postal_code,
    :min_longitude,
    :min_latitude,
    :max_longitude,
    :max_latitude,
    :latitude_dec,
    :longitude_dec
  ]

  AttrReaders.each do |meth|
    define_method meth do
      @data[meth.to_s]
    end
  end

  attr_reader :data

  def initialize(country_data)
    @data = country_data.is_a?(Hash) ? country_data : ISO3166::Data.new(country_data).call
  end

  def valid?
    !(@data.nil? || @data.empty?)
  end

  alias_method :zip, :postal_code
  alias_method :zip?, :postal_code
  alias_method :postal_code?, :postal_code

  def ==(other)
    other == data
  end

  def currency
    ISO4217::Currency.from_code(@data['currency'])
  end

  def currency_code
    @data['currency']
  end

  def subdivisions
    @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) : {}
  end

  alias_method :states, :subdivisions

  def subdivisions?
    File.exist?(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml"))
  end

  def in_eu?
    @data['eu_member'].nil? ? false : @data['eu_member']
  end

  def to_s
    @data['name']
  end

  def translation(locale = 'en')
    @data['translations'][locale.downcase]
  end

  private

  class << self
    def new(country_data)
      if country_data.is_a?(Hash) || Setup.data.keys.include?(country_data.to_s.upcase)
        super
      end
    end

    def codes
      Setup.codes
    end

    def all(&blk)
      blk ||= proc {|alpha2, d| ISO3166::Country.new(d)}
      Setup.data.map &blk
    end

    alias_method :countries, :all

    def all_translated(locale = 'en')
      translations(locale).values
    end

    def all_names_with_codes(locale = 'en')
      ISO3166::Country.all.map do |c|
        [(c.translation(locale) || c.name ).html_safe, c.alpha2]
      end.sort_by { |d| d[0] }
    end

    def translations(locale = 'en')
      I18nData.countries(locale.upcase)
    end

    def search(query)<commit_msg>fix ruby warning and use codes method<commit_after>module ISO3166; end

class ISO3166::Country
  Setup = ISO3166::Setup.new

  AttrReaders = [
    :number,
    :alpha2,
    :alpha3,
    :name,
    :names,
    :latitude,
    :longitude,
    :continent,
    :region,
    :subregion,
    :world_region,
    :country_code,
    :national_destination_code_lengths,
    :national_number_lengths,
    :international_prefix,
    :national_prefix,
    :address_format,
    :translations,
    :translated_names,
    :ioc,
    :gec,
    :un_locode,
    :languages,
    :nationality,
    :dissolved_on,
    :eu_member,
    :alt_currency,
    :vat_rates,
    :postal_code,
    :min_longitude,
    :min_latitude,
    :max_longitude,
    :max_latitude,
    :latitude_dec,
    :longitude_dec
  ]

  AttrReaders.each do |meth|
    define_method meth do
      @data[meth.to_s]
    end
  end

  attr_reader :data

  def initialize(country_data)
    @data = country_data.is_a?(Hash) ? country_data : ISO3166::Data.new(country_data).call
  end

  def valid?
    !(@data.nil? || @data.empty?)
  end

  alias_method :zip, :postal_code
  alias_method :zip?, :postal_code
  alias_method :postal_code?, :postal_code

  def ==(other)
    other == data
  end

  def currency
    ISO4217::Currency.from_code(@data['currency'])
  end

  def currency_code
    @data['currency']
  end

  def subdivisions
    @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml")) : {}
  end

  alias_method :states, :subdivisions

  def subdivisions?
    File.exist?(File.join(File.dirname(__FILE__), '..', 'data', 'subdivisions', "#{alpha2}.yaml"))
  end

  def in_eu?
    @data['eu_member'].nil? ? false : @data['eu_member']
  end

  def to_s
    @data['name']
  end

  def translation(locale = 'en')
    @data['translations'][locale.downcase]
  end

  private

  class << self
    def new(country_data)
      if country_data.is_a?(Hash) || codes.include?(country_data.to_s.upcase)
        super
      end
    end

    def codes
      Setup.codes
    end

    def all(&blk)
      blk ||= proc {|alpha2, d| ISO3166::Country.new(d)}
      Setup.data.map(&blk)
    end

    alias_method :countries, :all

    def all_translated(locale = 'en')
      translations(locale).values
    end

    def all_names_with_codes(locale = 'en')
      ISO3166::Country.all.map do |c|
        [(c.translation(locale) || c.name ).html_safe, c.alpha2]
      end.sort_by { |d| d[0] }
    end

    def translations(locale = 'en')
      I18nData.countries(locale.upcase)
    end

    def search(query)
{'fraction_non_alphanumeric': 0.1316143929189917, 'fraction_numerical': 0.014238984029247642, 'mean_word_length': 2.961890243902439, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7625535', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1878, 'n_words': 356}