Sha256: 4583285cd9ec123d6e096c159fd7ef3b1f2a1a738a8c7d2937223348a9fe7556

Contents?: true

Size: 1.12 KB

Versions: 124

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

class ReeHash::TransformValues
  include Ree::FnDSL

  fn :transform_values

  doc("Transforms values with passed parameter (block). By default transforms deeply")
  contract(Hash, Kwargs[deep: Bool], Block => Hash)
  def call(hash, deep: true, &proc)
    recursively_transform_values(nil, hash, deep, &proc)
  end

  private

  def recursively_transform_values(parent_key, object, deep, &block)
    case object
    when Hash
      result = {}

      result.default = object.default
      result.default_proc = object.default_proc if object.default_proc

      object.each do |key, value|
        result[key] = if deep
          yield(
            key, recursively_transform_values(key, value, deep, &block)
          )
        else
          yield(key, value)
        end
      end

      result
    when Array
      if deep
        object.map do
          yield(
            parent_key, recursively_transform_values(
              parent_key, _1, deep, &block
            )
          )
        end
      else
        yield(parent_key, object)
      end
    else
      yield(parent_key, object)
    end
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.104 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.103 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.102 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.101 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.100 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.99 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.98 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.97 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.96 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.95 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.94 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.93 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.92 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.91 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.90 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.89 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.88 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.87 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.86 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb
ree_lib-1.0.85 lib/ree_lib/packages/ree_hash/package/ree_hash/functions/transform_values.rb