Sha256: f05a954132bb43a73453422bccf8eb77864a55f66f805572944e33c22dfbb9c1

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Factrey
  class Ref
    # {Resolver} resolves {Ref}s and {Defer}s.
    class Resolver
      # @param recursion_limit [Integer, nil] how many recursions are allowed
      # @yieldparam name [Symbol] the name of the reference to be resolved
      def initialize(recursion_limit: nil, &handler)
        @recursion_limit = recursion_limit
        @handler = handler
      end

      # Traverse data recursively and resolve all {Ref}s and {Defer}s.
      #
      # This method supports recursive traversal for {Array} and {Hash}. For other structures, consider using {Defer}.
      # @param object [Object]
      # @param recursion_count [Integer]
      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
factrey-0.4.0 lib/factrey/ref/resolver.rb
factrey-0.3.0 lib/factrey/ref/resolver.rb
factrey-0.2.0 lib/factrey/ref/resolver.rb
factrey-0.1.0 lib/factrey/ref/resolver.rb