Sha256: 5ca3ff670313b2067c52744c9b80d50fb4dfdc0db46004ad6c7c9c12bb7e8d6d

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

#
# 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 Logic
    class << self

      def evaluate(expression, input, 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
          lambda { |value| resolver.call(value, input) }
        else
          lambda { |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

3 entries across 3 versions & 1 rubygems

Version Path
logicality-1.0.1 lib/logicality/logic.rb
logicality-1.0.0 lib/logicality/logic.rb
logicality-0.0.1 lib/logicality/logic.rb