Class: DiscoveryV1::Validation::TraverseObjectTree

Inherits:
Object
  • Object
show all
Defined in:
lib/discovery_v1/validation/traverse_object_tree.rb

Overview

Visit all objects in arbitrarily nested object tree of hashes and/or arrays

Class Method Summary collapse

Class Method Details

.call(object:, visitor:, path: []) ⇒ void

This method returns an undefined value.

Visit all objects in arbitrarily nested object tree of hashes and/or arrays

For each object, the visitor is called with the path to the object and the object itself.

Examples:

# In the examples below assume the elided code is the following:
visitor = -> (path:, object:) { puts "path: #{path}, object: #{obj}" }
DiscoveryV1::Validation::TraverseObjectTree.call(object:, visitor:)

Given a simple object (not very exciting)

object = 1
...
#=> path: [], object: 1

Given an array

object = [1, 2, 3]
...
#=> path: [], object: [1, 2, 3]
#=> path: [0], object: 1
#=> path: [1], object: 2
#=> path: [2], object: 3

Given a hash

object = { name: 'James', age: 42 }
...
#=> path: [], object: { name: 'James', age: 42 }
#=> path: [:name], object: James
#=> path: [:age], object: 42

Given an array of hashes

object = [{ name: 'James', age: 42 }, { name: 'Jane', age: 43 }]
...
#=> path: [], object: [{ name: 'James', age: 42 }, { name: 'Jane', age: 43 }]
#=> path: [0], object: { name: 'James', age: 42 }
#=> path: [0, :name], object: James
#=> path: [0, :age], object: 42
#=> path: [1], object: { name: 'Jane', age: 43 }
#=> path: [1, :name], object: Jane
#=> path: [1, :age], object: 43

Given a hash of hashes

object = { person1: { name: 'James', age: 42 }, person2: { name: 'Jane', age: 43 } }
...
#=> path: [], object: { person1: { name: 'James', age: 42 }, person2: { name: 'Jane', age: 43 } }
#=> path: [:person1], object: { name: 'James', age: 42 }
#=> path: [:person1, :name], object: James
#=> path: [:person1, :age], object: 42
#=> path: [:person2], object: { name: 'Jane', age: 43 }
#=> path: [:person2, :name], object: Jane
#=> path: [:person2, :age], object: 43

Parameters:

  • path (Array) (defaults to: [])

    the path to the object

  • object (Object)

    the object to visit

  • visitor (#call)

    the visitor to call for each object



70
71
72
73
74
75
76
77
# File 'lib/discovery_v1/validation/traverse_object_tree.rb', line 70

def self.call(object:, visitor:, path: [])
  visitor&.call(path:, object:)
  if object.is_a? Hash
    object.each { |k, obj| call(path: (path + [k]), object: obj, visitor:) }
  elsif object.is_a? Array
    object.each_with_index { |obj, k| call(path: (path + [k]), object: obj, visitor:) }
  end
end