Sha256: e2b1bbbf514d364de579740ba4154369eb52d117f42c5d36b1cc0f53f6ecf363

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 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.
#

require 'bigdecimal'
require 'forwardable'
require 'stringento'

require_relative 'extended_evaluator/formatter'
require_relative 'extended_evaluator/resolver'

module Proforma
  # This class provides robust functionality for value resolution and text templating.
  # For value resolution it uses its own dot-notation and
  # message-based object traversal algorithm.
  # For text templating it uses the Stringento library.
  class ExtendedEvaluator
    attr_reader :formatter, :resolver

    def initialize(formatter: Formatter.new, resolver: Resolver.new)
      raise ArgumentError, 'formatter is required'  unless formatter
      raise ArgumentError, 'resolver is required'   unless resolver

      @formatter  = formatter
      @resolver   = resolver

      freeze
    end

    def value(object, expression)
      resolver.resolve(expression, object)
    end

    def text(object, expression)
      record = object.is_a?(Array) || object.nil? ? {} : object

      Stringento.evaluate(
        expression.to_s,
        record,
        resolver: resolver,
        formatter: formatter
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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