module Logicality

Copyright © 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.

Copyright © 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.

Copyright © 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.

Copyright © 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.

Copyright © 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 that defines the main class-level API for this library.

Copyright © 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.

Copyright © 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.

Copyright © 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.

Copyright © 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.

Copyright © 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.

Copyright © 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.

Constants

VERSION

Public Class Methods

evaluate(expression, input = nil, resolver = nil) click to toggle source
# File lib/logicality/logicality.rb, line 17
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 Class Methods

cache() click to toggle source
# File lib/logicality/logicality.rb, line 49
def cache
  @cache ||= {}
end
get(expression) click to toggle source
# File lib/logicality/logicality.rb, line 57
def get(expression)
  return cache[expression] if cache[expression]

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

  set(expression, parser.parse)
end
object_resolver() click to toggle source
# File lib/logicality/logicality.rb, line 35
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
resolver_wrapper(input, resolver) click to toggle source
# File lib/logicality/logicality.rb, line 27
def resolver_wrapper(input, resolver)
  if resolver
    ->(value) { resolver.call(value, input) }
  else
    ->(value) { object_resolver.call(value, input) }
  end
end
set(expression, node) click to toggle source
# File lib/logicality/logicality.rb, line 53
def set(expression, node)
  cache[expression] ||= node
end