Class: Factrey::Ref::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/factrey/ref/resolver.rb

Overview

Resolver resolves Factrey::Refs and Defers.

Instance Method Summary collapse

Constructor Details

#initialize(recursion_limit: nil) {|name| ... } ⇒ Resolver

Returns a new instance of Resolver.

Parameters:

  • recursion_limit (Integer, nil) (defaults to: nil)

    how many recursions are allowed

Yield Parameters:

  • name (Symbol)

    the name of the reference to be resolved



9
10
11
12
# File 'lib/factrey/ref/resolver.rb', line 9

def initialize(recursion_limit: nil, &handler)
  @recursion_limit = recursion_limit
  @handler = handler
end

Instance Method Details

#resolve(object, recursion_count: 0) ⇒ Object

Traverse data recursively and resolve all Factrey::Refs and Defers.

This method supports recursive traversal for Array and Hash. For other structures, consider using Defer.

Parameters:

  • object (Object)
  • recursion_count (Integer) (defaults to: 0)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/factrey/ref/resolver.rb', line 19

def resolve(object, recursion_count: 0)
  return object if !@recursion_limit.nil? && @recursion_limit < recursion_count

  recursion_count += 1
  case object
  when Array
    object.map { resolve(_1, recursion_count:) }
  when Hash
    object.to_h { |key, value| [resolve(key, recursion_count:), resolve(value, recursion_count:)] }
  when Ref
    @handler.call(object.name)
  when Defer
    object.body.call(*object.refs.map { resolve(_1) })
  else
    object
  end
end