Sha256: efd02cbb039f7dbec1ea82f25371318fe67d3e521fb380db9c2e3a9c90da598b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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 '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.pre.alpha lib/proforma/extended_evaluator.rb