Sha256: 29c6560228685efca67ab9bc41ba38bc28519b5f7865735b245427989e8e7b1e

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2019-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module Proforma
  class ExtendedEvaluator
    # This class is also meant to be plugged into Stringento to provide value resolution.
    class Resolver
      DOT_NOTATION_SEPARATOR = '.'

      def resolve(value, input)
        traverse(input, value.to_s.split(DOT_NOTATION_SEPARATOR))
      end

      private

      def traverse(object, through)
        pointer = object

        through.each do |t|
          next unless pointer

          pointer = get(pointer, t)
        end

        pointer
      end

      def get(object, key)
        if object.is_a?(Hash)
          indifferent_hash_get(object, key)
        elsif object.respond_to?(key)
          object.send(key)
        end
      end

      def indifferent_hash_get(hash, key)
        if hash.key?(key.to_s)
          hash[key.to_s]
        elsif hash.key?(key.to_s.to_sym)
          hash[key.to_s.to_sym]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proforma-extended-evaluator-1.0.0.pre.alpha lib/proforma/extended_evaluator/resolver.rb