class RemoveDataAttributes::Processor

Constants

DATA_ATTRIBUTE_KEYS
DATA_ATTRIBUTE_PATTERN

Public Class Methods

new(data_attributes) click to toggle source
# File lib/remove_data_attributes/processor.rb, line 12
def initialize(data_attributes)
  valid_attribute_names = Array(data_attributes)
    .map(&DATA_ATTRIBUTE_PATTERN.method(:match))
    .select(&:itself)
    .map { |md| md[:attribute_name] }

  @target_keys = valid_attribute_names.map { |name| "data-#{name}" }.to_set
  @target_keys_in_nested_hash = valid_attribute_names.map(&method(:normalize)).to_set
end

Public Instance Methods

call(hash) click to toggle source
# File lib/remove_data_attributes/processor.rb, line 22
def call(hash)
  new_hash = hash.reject { |key, _| @target_keys.include?(key.to_s) }

  DATA_ATTRIBUTE_KEYS.each do |key|
    nested_hash = hash[key]
    next unless nested_hash.is_a?(::Hash)

    new_hash[key] = nested_hash
      .reject { |key, _| @target_keys_in_nested_hash.include?(normalize(key)) }
  end

  new_hash
end

Private Instance Methods

normalize(attribute_name) click to toggle source

:reek: UtilityFunction

# File lib/remove_data_attributes/processor.rb, line 39
def normalize(attribute_name)
  attribute_name.to_s.dasherize
end