Sha256: d51f53498ecb53537ac56f90e4c216b38433b7ed4be93194f1f2acc50b8fe958

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 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
  # This class will serve as the evaluator that gets shipped with the base framework.
  # Other packages can extend or create their own and plug them into the rendering
  # pipeline.
  # This, being a prototype for customizable evaluators, just provides basic evaluation:
  # - it can only handle hashes for value extraction
  # - if text is prefixed with a dollar sign and colon then it means it will be dynamically
  #   evaluated against the record.  For example: $:id
  class HashEvaluator
    PROPERTY_PREFIX = '$:'

    def value(object, expression)
      return object if expression.to_s.empty?
      return nil    unless object.is_a?(Hash)

      if object.key?(expression.to_s)
        object[expression.to_s]
      elsif object.key?(expression.to_s.to_sym)
        object[expression.to_s.to_sym]
      end
    end

    def text(object, expression)
      if expression.to_s.start_with?(PROPERTY_PREFIX)
        value(object, expression.to_s[2..-1])
      else
        expression
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proforma-1.0.0.pre.alpha lib/proforma/hash_evaluator.rb