Sha256: c9cf04ec5b0923ae0d67ab54f633f269142df5b6a70e09b6a97d16e81699c8bf

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2018-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 Logicality
  # Class that defines the main class-level API for this library.
  class Logic
    class << self
      def evaluate(expression, input = nil, resolver = nil)
        node        = get(expression)
        wrapper     = resolver_wrapper(input, resolver)
        interpreter = Interpreter::SimpleInterpreter.new(wrapper)

        interpreter.visit(node)
      end

      private

      def resolver_wrapper(input, resolver)
        if resolver
          ->(value) { resolver.call(value, input) }
        else
          ->(value) { object_resolver.call(value, input) }
        end
      end

      def object_resolver
        lambda do |value, input|
          return false unless input

          if input.respond_to?(value)
            !!input.send(value)
          elsif input.respond_to?(:[])
            !!input[value]
          else
            false
          end
        end
      end

      def cache
        @cache || {}
      end

      def set(expression, node)
        @cache = {} unless @cache

        @cache[expression] = node
      end

      def get(expression)
        return cache[expression] if cache[expression]

        lexer   = Lexer::RegexpLexer.new(expression)
        parser  = Parser::SimpleParser.new(lexer)

        set(expression, parser.parse)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logicality-1.0.5 lib/logicality/logic.rb