Class: Darthjee::CoreExt::Hash::ValueChanger Private

Inherits:
Object
  • Object
show all
Defined in:
lib/darthjee/core_ext/hash/value_changer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class responsible for changing values on a hash

Examples:

changer = Darthjee::CoreExt::Hash::ValueChanger.new(
  recursive: false,
  skip_inner: false,
  &:class
)

hash = { a: 1, b: { c: 2 }, d: [{ e: 1 }] }
changer.change(hash)  # {
                      #   a: Integer,
                      #   b: Hash,
                      #   d: Array
                      # }
changer = Darthjee::CoreExt::Hash::ValueChanger.new do |value|
  value.to_s.size
end

hash = { a: 15, b: { c: 2 }, d: [{ e: 100 }] }
changer.change(hash)  # {
                      #   a: 2,
                      #   b: { c: 1 },
                      #   d: [{ e: 3 }]
                      # }
changer = Darthjee::CoreExt::Hash::ValueChanger.new(
  skip_inner: true
) do |value|
  value.to_s.size
end

hash = { a: 15, b: { c: 2 }, d: [{ e: 100 }] }
changer.change(hash)  # {
                      #   a: 2,
                      #   b: 11,
                      #   d: 7
                      # }
changer = Darthjee::CoreExt::Hash::ValueChanger.new(
  recursive: true
) do |value|
  value.to_s.size
end

hash = { a: 15, b: { c: 2 }, d: [{ e: 100 }] }
changer.change(hash)  # {
                      #   a: 2,
                      #   b: { c: 1 },
                      #   d: [{ e: 3 }]
                      # }
changer = Darthjee::CoreExt::Hash::ValueChanger.new do |value|
  value.to_s.size
end

array = [15, { c: 2 }, [{ e: 100 }]]

changer.change(array) # [
                      #   2,
                      #   { c: 1 },
                      #   [{ e: 3 }]
                      # ]

Author:

  • darthjee

Instance Method Summary collapse

Constructor Details

#initialize(recursive: true, skip_inner: true, &block) ⇒ ValueChanger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ValueChanger.

Examples:

changer = Darthjee::CoreExt::Hash::ValueChanger.new(
  recursive: false,
  skip_inner: false,
  &:class
)

hash = { a: 1, b: { c: 2 }, d: [{ e: 1 }] }
changer.change(hash)  # {
                      #   a: Integer,
                      #   b: Hash,
                      #   d: Array
                      # }

Parameters:

  • recursive (::TrueClass, ::FalseClass) (defaults to: true)

    flag telling to apply transformation recursively

  • skip_inner (::TrueClass, ::FalseClass) (defaults to: true)

    flag telling to not apply change block call to inner hashes

  • block (::Proc)

    block to be called when changing the values



45
46
47
48
49
50
# File 'lib/darthjee/core_ext/hash/value_changer.rb', line 45

def initialize(recursive: true, skip_inner: true, &block)
  @recursive = recursive
  @skip_inner = skip_inner

  @block = block
end

Instance Method Details

#change(object) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Change the given object

Examples:

changer = Darthjee::CoreExt::Hash::ValueChanger.new do |value|
  value.to_s.size
end

hash = { a: 15, b: { c: 2 }, d: [{ e: 100 }] }
changer.change(hash)  # {
                      #   a: 2,
                      #   b: { c: 1 },
                      #   d: [{ e: 3 }]
                      # }
changer = Darthjee::CoreExt::Hash::ValueChanger.new(
  skip_inner: true
) do |value|
  value.to_s.size
end

hash = { a: 15, b: { c: 2 }, d: [{ e: 100 }] }
changer.change(hash)  # {
                      #   a: 2,
                      #   b: 11,
                      #   d: 7
                      # }
changer = Darthjee::CoreExt::Hash::ValueChanger.new(
  recursive: true
) do |value|
  value.to_s.size
end

hash = { a: 15, b: { c: 2 }, d: [{ e: 100 }] }
changer.change(hash)  # {
                      #   a: 2,
                      #   b: { c: 1 },
                      #   d: [{ e: 3 }]
                      # }
changer = Darthjee::CoreExt::Hash::ValueChanger.new do |value|
  value.to_s.size
end

array = [15, { c: 2 }, [{ e: 100 }]]

changer.change(array) # [
                      #   2,
                      #   { c: 1 },
                      #   [{ e: 3 }]
                      # ]

Parameters:

  • object (::Hash/::Array)

    object to have it’s values changed

Returns:

  • the resulting object (hash or array) with it`s values changed



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/darthjee/core_ext/hash/value_changer.rb', line 111

def change(object)
  if object.is_a?(Hash)
    change_hash(object)
  elsif object.is_a?(Array)
    change_array(object)
  elsif iterable?(object)
    change_iterator(object)
  else
    new_value(object)
  end
end