<commit_before>module Seek
  class Enumerations
    class << self
      Dir["#{File.expand_path('..', __FILE__)}/enumerations/*.yml"].each do |file|
        resource = File.basename(file, '.yml').to_sym

        send :define_method, resource, ->{
          result = instance_variable_get("@#{resource}") || instance_variable_set("@#{resource}", create_hash_from_yml(resource))

          result.define_singleton_method(:select_options) do
            result.map { |enumeration_item| [enumeration_item.description, enumeration_item.id] }
          end unless result.respond_to?(:select_options)

          result
        }
      end

      private

      def create_hash_from_yml(resource)
        file = File.expand_path("../enumerations/#{resource}.yml", __FILE__)
        to_open_struct(YAML.load_file(file)['enumerations'][resource.to_sym])
      end

      def to_open_struct(object)
        case object
          when Hash
            object = object.clone
            object.each do |key, value|
              object[key] = to_open_struct(value)
            end
            OpenStruct.new(object)
          when Array
            object = object.clone
            object.map! { |i| to_open_struct(i) }
          else
            object
        end
      end
    end
  end
end

# # In order to use the MarketSegment API:
# result = HTTParty.post(
#   'http://webservices.seek.com.au/MarketSegment.asmx/GetMarketClassifiers',
#   body: { 'marketSegment' => 'Main' }
# )
#
# h = result['SpecificClassification']['list'].inject({}) do |hash, list|
#   hash[list['value']] = list['listItem'].map { |i| i['value'] } if list['listItem']
#   hash
# end
<commit_msg>Improving 'select_options' method for enumerations<commit_after>module Seek
  class Enumerations
    class << self
      Dir["#{File.expand_path('..', __FILE__)}/enumerations/*.yml"].each do |file|
        resource = File.basename(file, '.yml').to_sym

        send :define_method, resource, ->{
          context = self
          enumeration = instance_variable_get("@#{resource}") || instance_variable_set("@#{resource}", create_hash_from_yml(resource))
          enumeration.define_singleton_method(:select_options) { context.send :define_select_options, resource, enumeration } unless enumeration.respond_to?(:select_options)

          enumeration
        }
      end

      private

      def create_hash_from_yml(resource)
        file = File.expand_path("../enumerations/#{resource}.yml", __FILE__)
        to_open_struct(YAML.load_file(file)['enumerations'][resource.to_sym])
      end

      def to_open_struct(object)
        case object
          when Hash
            object = object.clone
            object.each do |key, value|
              object[key] = to_open_struct(value)
            end
            OpenStruct.new(object)
          when Array
            object = object.clone
            object.map! { |i| to_open_struct(i) }
          else
            object
        end
      end

      def define_select_options(resource, enumeration)
        case resource
          when :nations
            enumeration.inject([]) do |result, nation|
              nation.states.each do |state|
                state.locations.each do |location|
                  result << ["#{nation.description} > #{state.description} > #{location.description}", location.id]
                end
              end && result
            end

          else enumeration.map { |enumeration_item| [enumeration_item.description, enumeration_item.id] }
        end
      end
    end
  end
end

# # In order to use the MarketSegment API:
# result = HTTParty.post(
#   'http://webservices.seek.com.au/MarketSegment.asmx/GetMarketClassifiers',
#   body: { 'marketSegment' => 'Main' }
# )
#
# h = result['SpecificClassification']['list'].inject({}) do |hash, list|
#   hash[list['value']] = list['listItem'].map { |i| i['value'] } if list['listItem']
#   hash
# end

{'fraction_non_alphanumeric': 0.12436159346271705, 'fraction_numerical': 0.0, 'mean_word_length': 2.4299474605954465, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4299034', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1139, 'n_words': 227}