lib/dentaku/calculator.rb in dentaku-3.5.1 vs lib/dentaku/calculator.rb in dentaku-3.5.2

- old
+ new

@@ -8,35 +8,41 @@ module Dentaku class Calculator include StringCasing attr_reader :result, :memory, :tokenizer, :case_sensitive, :aliases, - :nested_data_support, :ast_cache + :nested_data_support, :ast_cache, :raw_date_literals def initialize(options = {}) clear @tokenizer = Tokenizer.new @case_sensitive = options.delete(:case_sensitive) @aliases = options.delete(:aliases) || Dentaku.aliases @nested_data_support = options.fetch(:nested_data_support, true) options.delete(:nested_data_support) + @raw_date_literals = options.fetch(:raw_date_literals, true) + options.delete(:raw_date_literals) @ast_cache = options @disable_ast_cache = false @function_registry = Dentaku::AST::FunctionRegistry.new end - def self.add_function(name, type, body) - Dentaku::AST::FunctionRegistry.default.register(name, type, body) + def self.add_function(name, type, body, callback = nil) + Dentaku::AST::FunctionRegistry.default.register(name, type, body, callback) end - def add_function(name, type, body) - @function_registry.register(name, type, body) + def self.add_functions(functions) + functions.each { |(name, type, body, callback)| add_function(name, type, body, callback) } + end + + def add_function(name, type, body, callback = nil) + @function_registry.register(name, type, body, callback) self end - def add_functions(fns) - fns.each { |(name, type, body)| add_function(name, type, body) } + def add_functions(functions) + functions.each { |(name, type, body, callback)| add_function(name, type, body, callback) } self end def disable_cache @disable_ast_cache = true @@ -92,12 +98,13 @@ def ast(expression) return expression.map { |e| ast(e) } if expression.is_a? Array @ast_cache.fetch(expression) { options = { + aliases: aliases, case_sensitive: case_sensitive, function_registry: @function_registry, - aliases: aliases + raw_date_literals: raw_date_literals } tokens = tokenizer.tokenize(expression, options) Parser.new(tokens, options).parse.tap do |node| @ast_cache[expression] = node if cache_ast?