<commit_before># encoding: utf-8
module EnumerateIt
  module ClassMethods
    def has_enumeration_for(attribute, options = {})
      self.enumerations = self.enumerations.dup

      define_enumeration_class attribute, options
      set_validations attribute, options
      create_enumeration_humanize_method options[:with], attribute
      store_enumeration options[:with], attribute

      if options[:create_helpers]
        create_helper_methods options[:with], attribute, options[:create_helpers]
        create_mutator_methods options[:with], attribute, options[:create_helpers]
        create_polymorphic_methods options[:with], attribute, options[:create_helpers]
      end

      if options[:create_scopes]
        create_scopes options[:with], attribute
      end
    end

    private
    def store_enumeration(klass, attribute)
      enumerations[attribute] = klass
    end

    def create_enumeration_humanize_method(klass, attribute_name)
      class_eval do
        define_method "#{attribute_name}_humanize" do
          values = klass.enumeration.values.detect { |v| v[0] == self.send(attribute_name) }

          values ? klass.translate(values[1]) : nil
        end
      end
    end

    def create_helper_methods(klass, attribute_name, helpers)
      prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]

      class_eval do
        klass.enumeration.keys.each do |option|
          define_method "#{prefix_name}#{option}?" do
            self.send(attribute_name) == klass.enumeration[option].first
          end
        end
      end
    end

    def create_scopes(klass, attribute_name)
      klass.enumeration.keys.each do |option|
        if respond_to? :scope
          scope option, lambda { where(attribute_name => klass.enumeration[option].first)}
        end
      end
    end

    def create_mutator_methods(klass, attribute_name, helpers)
      prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]

      class_eval do
        klass.enumeration.each_pair do |key, values|
          define_method "#{prefix_name}#{key}!" do
            self.send "#{attribute_name}=", values.first
          end
        end
      end
    end

    def create_polymorphic_methods(klass, attribute_name, helpers)
      class_eval do
        define_method "#{attribute_name}_object" do
          value = self.public_send(attribute_name)

          klass.const_get(klass.key_for(value).to_s.camelize).new if value
        end
      end
    end

    def define_enumeration_class(attribute, options)
      if options[:with].nil?
        inner_enum_class_name = attribute.to_s.camelize.to_sym
        options[:with] = self.constants.include?(inner_enum_class_name) ? self.const_get(inner_enum_class_name) : attribute.to_s.camelize.constantize
      end<commit_msg>creating only if polymorphic is set<commit_after># encoding: utf-8
module EnumerateIt
  module ClassMethods
    def has_enumeration_for(attribute, options = {})
      self.enumerations = self.enumerations.dup

      define_enumeration_class attribute, options
      set_validations attribute, options
      create_enumeration_humanize_method options[:with], attribute
      store_enumeration options[:with], attribute

      if options[:create_helpers]
        create_helper_methods options[:with], attribute, options[:create_helpers]
        create_mutator_methods options[:with], attribute, options[:create_helpers]
        create_polymorphic_methods options[:with], attribute, options[:create_helpers]
      end

      if options[:create_scopes]
        create_scopes options[:with], attribute
      end
    end

    private
    def store_enumeration(klass, attribute)
      enumerations[attribute] = klass
    end

    def create_enumeration_humanize_method(klass, attribute_name)
      class_eval do
        define_method "#{attribute_name}_humanize" do
          values = klass.enumeration.values.detect { |v| v[0] == self.send(attribute_name) }

          values ? klass.translate(values[1]) : nil
        end
      end
    end

    def create_helper_methods(klass, attribute_name, helpers)
      prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]

      class_eval do
        klass.enumeration.keys.each do |option|
          define_method "#{prefix_name}#{option}?" do
            self.send(attribute_name) == klass.enumeration[option].first
          end
        end
      end
    end

    def create_scopes(klass, attribute_name)
      klass.enumeration.keys.each do |option|
        if respond_to? :scope
          scope option, lambda { where(attribute_name => klass.enumeration[option].first)}
        end
      end
    end

    def create_mutator_methods(klass, attribute_name, helpers)
      prefix_name = "#{attribute_name}_" if helpers.is_a?(Hash) && helpers[:prefix]

      class_eval do
        klass.enumeration.each_pair do |key, values|
          define_method "#{prefix_name}#{key}!" do
            self.send "#{attribute_name}=", values.first
          end
        end
      end
    end

    def create_polymorphic_methods(klass, attribute_name, helpers)
      return unless helpers.is_a?(Hash) && helpers[:polymorphic]

      class_eval do
        define_method "#{attribute_name}_object" do
          value = self.public_send(attribute_name)

          klass.const_get(klass.key_for(value).to_s.camelize).new if value
        end
      end
    end

    def define_enumeration_class(attribute, options)
      if options[:with].nil?
        inner_enum_class_name = attribute.to_s.camelize.to_sym
        options[:with] = self.constants.include?(inner_enum_class_name) ? self.const_get(inner_enum_class_name) : attribute.to_s.camelize.constantize
      end
{'fraction_non_alphanumeric': 0.08959133775759692, 'fraction_numerical': 0.001047851903597625, 'mean_word_length': 3.06458481192335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5304112', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1692, 'n_words': 340}