Class: Factrey::Ref::Resolver
- Inherits:
-
Object
- Object
- Factrey::Ref::Resolver
- Defined in:
- lib/factrey/ref/resolver.rb
Overview
Resolver resolves Factrey::Refs and Defers.
Instance Method Summary collapse
-
#initialize(recursion_limit: nil) {|name| ... } ⇒ Resolver
constructor
A new instance of Resolver.
-
#resolve(object, recursion_count: 0) ⇒ Object
Traverse data recursively and resolve all Factrey::Refs and Defers.
Constructor Details
#initialize(recursion_limit: nil) {|name| ... } ⇒ Resolver
Returns a new instance of Resolver.
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.
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 |