class RspecApiDocs::Resource::Example::DeepHashSet

Attributes

hash[R]
keys[R]
node[R]
value[R]

Public Class Methods

call(*args) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example/deep_hash_set.rb, line 7
def self.call(*args)
  new(*args).call
end
new(hash, keys, value) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example/deep_hash_set.rb, line 11
def initialize(hash, keys, value)
  @hash = hash
  @keys = keys
  @value = value
  @node = []
end

Public Instance Methods

call() click to toggle source
# File lib/rspec_api_docs/formatter/resource/example/deep_hash_set.rb, line 18
def call
  keys.each_with_index do |key, index|
    case
    when key.nil?
      deep_set_value_at_array(index)
      break
    when index == keys.size - 1
      set_value_at(key)
    else
      node << key
    end
  end

  hash
end

Private Instance Methods

deep_find(hash, keys) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example/deep_hash_set.rb, line 52
def deep_find(hash, keys)
  keys.inject(hash) { |h, k| h && h[k] }
end
deep_set_value_at_array(index) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example/deep_hash_set.rb, line 38
def deep_set_value_at_array(index)
  array = deep_find(hash, node)
  array && array.each do |inner_hash|
    DeepHashSet.call(inner_hash, keys[index+1..-1], value)
  end
end
set_value_at(key) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example/deep_hash_set.rb, line 45
def set_value_at(key)
  part = deep_find(hash, node)
  if part.is_a?(Hash) && !part[key].nil?
    part[key] = value
  end
end