Sha256: 3f46d2def9aac0bdf413a092bd7988f285becef9836f13623cc4d5c15316318a

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

class Hash::ValueChanger
  attr_accessor :options, :block

  def initialize(options, &block)
    @options = {
      recursive: true,
      skip_inner: true
    }.merge(options)

    @block = block
  end

  def change(object)
    if object.respond_to?(:change_values)
      change_hash(object)
    elsif is_iterable?(object)
      change_array(object)
    end
  end

  private

  def change_hash(original_hash)
    original_hash.tap do |hash|
      original_hash.each do |key, value|
        value = new_value(value)
        hash[key] = value
      end
    end
  end

  def change_array(array)
    array = array.to_a

    array.each.with_index do |value, index|
      if value.respond_to?(:change_values)
        value = value.change_values(options, &block)
      elsif is_iterable?(value)
        value = change_array(value)
      end
      array[index] = value
    end
  end

  def change_value?(value)
    !is_iterable?(value) || !options[:skip_inner]
  end

  def is_iterable?(value)
    value.respond_to?(:each)
  end

  def new_value(value)
    value = block.call(value) if change_value?(value)
    apply_recursion?(value) ? change(value) : value
  end

  def apply_recursion?(value)
    is_iterable?(value) && options[:recursive]
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
darthjee-core_ext-1.3.0 lib/hash/value_changer.rb
darthjee-core_ext-1.2.6 lib/hash/value_changer.rb
bidu-core_ext-1.2.5 lib/hash/value_changer.rb