<commit_before>require 'virtus/mapper/version'
require 'virtus'
require 'active_support/core_ext/hash/indifferent_access'

HWIA = ActiveSupport::HashWithIndifferentAccess

module Virtus
  module Mapper

    attr_reader :mapped_attributes

    def initialize(attrs={})
      @mapped_attributes = HWIA.new(attrs)
      super(prepare_attributes_for_assignment!(@mapped_attributes))
    end

    # A workaround for https://github.com/solnic/virtus/issues/266
    def extend_with(mod)
      attr_set = attribute_set
      self.extend(mod) # Virtus modifies attribute_set
      set_attr_values(attribute_set.collect(&:name))
      update_attribute_set(attr_set)
    end

    private

    def prepare_attributes_for_assignment!(attrs)
      nil_value_keys = attrs.collect { |k, v| k if v.nil? }.compact
      attrs.tap do |h|
        attributes_to_map_by_symbol(attrs).each do |att|
          h[att.name] = h.delete(from(att))
        end
        attributes_to_map_by_call.each do |att|
          h[att.name] = from(att).call(h)
        end
      end.delete_if { |k, v| !nil_value_keys.include?(k) && v.nil? }
    end

    def attributes_to_map_by_symbol(attrs)
      attributes_to_map.select do |att|
        !from(att).respond_to?(:call) &&
        !attrs.has_key?(att.name)
      end
    end

    def attributes_to_map_by_call
      attributes_to_map.select { |att| from(att).respond_to?(:call) }
    end

    def attributes_to_map
      attribute_set.select { |att| !(from(att).nil?) }
    end

    def from(attribute)
      attribute.options[:from]
    end

    def update_attribute_set(attr_set)
      attr_set.merge(attribute_set)
      self.attribute_set = attr_set
    end

    def attribute_set=(attr_set)
      instance_variable_set(:@attribute_set, attr_set)<commit_msg>[TS][84710626] Extract method, use attr_reader<commit_after>require 'virtus/mapper/version'
require 'virtus'
require 'active_support/core_ext/hash/indifferent_access'

HWIA = ActiveSupport::HashWithIndifferentAccess

module Virtus
  module Mapper

    attr_reader :mapped_attributes, :nil_value_keys

    def initialize(attrs={})
      @mapped_attributes = HWIA.new(attrs)
      @nil_value_keys = @mapped_attributes.collect { |k, v| k if v.nil? }.compact
      super(prepare_attributes_for_assignment!(@mapped_attributes))
    end

    # A workaround for https://github.com/solnic/virtus/issues/266
    def extend_with(mod)
      attr_set = attribute_set
      self.extend(mod) # Virtus modifies attribute_set
      set_attr_values(attribute_set.collect(&:name))
      update_attribute_set(attr_set)
    end

    private

    def prepare_attributes_for_assignment!(attrs)
      attrs.tap do |h|
        attributes_to_map_by_symbol(attrs).each do |att|
          h[att.name] = h.delete(from(att))
        end
        attributes_to_map_by_call.each do |att|
          h[att.name] = from(att).call(h)
        end
      end.delete_if { |k, v| delete_nil_value_for?(k, v) }
    end

    def delete_nil_value_for?(k, v)
      !nil_value_keys.include?(k) && v.nil?
    end

    def attributes_to_map_by_symbol(attrs)
      attributes_to_map.select do |att|
        !from(att).respond_to?(:call) &&
        !attrs.has_key?(att.name)
      end
    end

    def attributes_to_map_by_call
      attributes_to_map.select { |att| from(att).respond_to?(:call) }
    end

    def attributes_to_map
      attribute_set.select { |att| !(from(att).nil?) }
    end

    def from(attribute)
      attribute.options[:from]
    end

    def update_attribute_set(attr_set)
      attr_set.merge(attribute_set)
      self.attribute_set = attr_set
    end

    def attribute_set=(attr_set)
      instance_variable_set(:@attribute_set, attr_set)
{'fraction_non_alphanumeric': 0.10316815597075549, 'fraction_numerical': 0.0037909558624424585, 'mean_word_length': 3.2313860252004583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2089481', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1234, 'n_words': 214}