<commit_before># frozen_string_literal: true

module Wardrobe
  class UnavailableOptionError < StandardError; end

  # Attribute class
  class Attribute
    attr_reader :name, :klass, :options, :ivar_name, :setter_name, :store,
                :setters, :getters

    def initialize(name, klass, defining_object, config, **options)
      @name = name
      @ivar_name = "@#{name}"
      @setter_name = "#{name}="
      @klass = validate_klass(klass)
      @options = validate_and_coerce_options(options, config, defining_object)
      @getters ||= build_getter_array(defining_object)
      @setters ||= build_setter_array(defining_object)
      freeze
    end

    def merge(other, defining_object, config)
      merged_options = merge_options(other.options)
      self.class.new(name, other.klass, defining_object, config, merged_options)
    end

    def wardrobe?
      klass.respond_to?(:wardrobe_config)
    end

    private

    def validate_klass(klass)
      if klass.is_a?(Array) && klass.count != 1
        raise StandardError, %(
          `Array#{klass.map(&:name)}' contains two many classes.
          No more than one is allowed.
        )
      elsif klass.is_a?(Hash)
        # TODO: Validate hash
      end
      klass
    end

    def merge_options(other)
      merged_options = options.dup
      other.each do |key, value|
        next if merged_options[key] == value
        if merged_options[key]
          raise 'FIX ME! Currently only hash merge is supported' unless value.is_a?(Hash)
          merged_options[key] = merged_options[key].merge(value)
        else
          merged_options[key] = value.dup<commit_msg>Added support for merging set option<commit_after># frozen_string_literal: true

module Wardrobe
  class UnavailableOptionError < StandardError; end

  # Attribute class
  class Attribute
    attr_reader :name, :klass, :options, :ivar_name, :setter_name, :store,
                :setters, :getters

    def initialize(name, klass, defining_object, config, **options)
      @name = name
      @ivar_name = "@#{name}"
      @setter_name = "#{name}="
      @klass = validate_klass(klass)
      @options = validate_and_coerce_options(options, config, defining_object)
      @getters ||= build_getter_array(defining_object)
      @setters ||= build_setter_array(defining_object)
      freeze
    end

    def merge(other, defining_object, config)
      merged_options = merge_options(other.options)
      self.class.new(name, other.klass, defining_object, config, merged_options)
    end

    def wardrobe?
      klass.respond_to?(:wardrobe_config)
    end

    private

    def validate_klass(klass)
      if klass.is_a?(Array) && klass.count != 1
        raise StandardError, %(
          `Array#{klass.map(&:name)}' contains two many classes.
          No more than one is allowed.
        )
      elsif klass.is_a?(Hash)
        # TODO: Validate hash
      end
      klass
    end

    def merge_options(other)
      merged_options = options.dup
      other.each do |key, value|
        next if merged_options[key] == value
        if merged_options[key]
          case value
          when Hash, Set
            merged_options[key] = merged_options[key].merge(value)
          else
            raise 'FIX ME! Currently only hash and set merge is supported'
          end
        else
          merged_options[key] = value.dup
{'fraction_non_alphanumeric': 0.09420505200594353, 'fraction_numerical': 0.0005943536404160475, 'mean_word_length': 2.6907894736842106, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7117567', 'n_tokens_mistral': 1006, 'n_tokens_neox': 1023, 'n_words': 251}