Class: Dentaku::Calculator

Inherits:
Object
  • Object
show all
Includes:
StringCasing
Defined in:
lib/dentaku/calculator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StringCasing

#standardize_case

Constructor Details

#initialize(options = {}) ⇒ Calculator

Returns a new instance of Calculator.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dentaku/calculator.rb', line 14

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)
  @ast_cache = options
  @disable_ast_cache = false
  @function_registry = Dentaku::AST::FunctionRegistry.new
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



12
13
14
# File 'lib/dentaku/calculator.rb', line 12

def aliases
  @aliases
end

#case_sensitiveObject (readonly)

Returns the value of attribute case_sensitive.



12
13
14
# File 'lib/dentaku/calculator.rb', line 12

def case_sensitive
  @case_sensitive
end

#memoryObject (readonly)

Returns the value of attribute memory.



12
13
14
# File 'lib/dentaku/calculator.rb', line 12

def memory
  @memory
end

#nested_data_supportObject (readonly)

Returns the value of attribute nested_data_support.



12
13
14
# File 'lib/dentaku/calculator.rb', line 12

def nested_data_support
  @nested_data_support
end

#resultObject (readonly)

Returns the value of attribute result.



12
13
14
# File 'lib/dentaku/calculator.rb', line 12

def result
  @result
end

#tokenizerObject (readonly)

Returns the value of attribute tokenizer.



12
13
14
# File 'lib/dentaku/calculator.rb', line 12

def tokenizer
  @tokenizer
end

Class Method Details

.add_function(name, type, body) ⇒ Object



26
27
28
# File 'lib/dentaku/calculator.rb', line 26

def self.add_function(name, type, body)
  Dentaku::AST::FunctionRegistry.default.register(name, type, body)
end

Instance Method Details

#add_function(name, type, body) ⇒ Object



30
31
32
33
# File 'lib/dentaku/calculator.rb', line 30

def add_function(name, type, body)
  @function_registry.register(name, type, body)
  self
end

#add_functions(fns) ⇒ Object



35
36
37
38
# File 'lib/dentaku/calculator.rb', line 35

def add_functions(fns)
  fns.each { |(name, type, body)| add_function(name, type, body) }
  self
end

#ast(expression) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dentaku/calculator.rb', line 85

def ast(expression)
  @ast_cache.fetch(expression) {
    options = {
      case_sensitive: case_sensitive,
      function_registry: @function_registry,
      aliases: aliases
    }

    tokens = tokenizer.tokenize(expression, options)
    Parser.new(tokens, options).parse.tap do |node|
      @ast_cache[expression] = node if cache_ast?
    end
  }
end

#cache_ast?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/dentaku/calculator.rb', line 152

def cache_ast?
  Dentaku.cache_ast? && !@disable_ast_cache
end

#clearObject



144
145
146
# File 'lib/dentaku/calculator.rb', line 144

def clear
  @memory = {}
end

#clear_cache(pattern = :all) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dentaku/calculator.rb', line 100

def clear_cache(pattern = :all)
  case pattern
  when :all
    @ast_cache = {}
  when String
    @ast_cache.delete(pattern)
  when Regexp
    @ast_cache.delete_if { |k, _| k =~ pattern }
  else
    raise ::ArgumentError
  end
end

#dependencies(expression, context = {}) ⇒ Object



78
79
80
81
82
83
# File 'lib/dentaku/calculator.rb', line 78

def dependencies(expression, context = {})
  if expression.is_a? Array
    return expression.flat_map { |e| dependencies(e, context) }
  end
  store(context) { ast(expression).dependencies(memory) }
end

#disable_cacheObject



40
41
42
43
44
45
# File 'lib/dentaku/calculator.rb', line 40

def disable_cache
  @disable_ast_cache = true
  yield(self) if block_given?
ensure
  @disable_ast_cache = false
end

#empty?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/dentaku/calculator.rb', line 148

def empty?
  memory.empty?
end

#evaluate(expression, data = {}, &block) ⇒ Object



47
48
49
50
51
# File 'lib/dentaku/calculator.rb', line 47

def evaluate(expression, data = {}, &block)
  evaluate!(expression, data)
rescue UnboundVariableError, Dentaku::ArgumentError => ex
  block.call(expression, ex) if block_given?
end

#evaluate!(expression, data = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dentaku/calculator.rb', line 53

def evaluate!(expression, data = {}, &block)
  return expression.map { |e|
    evaluate(e, data, &block)
  } if expression.is_a? Array

  store(data) do
    node = expression
    node = ast(node) unless node.is_a?(AST::Node)
    unbound = node.dependencies - memory.keys
    unless unbound.empty?
      raise UnboundVariableError.new(unbound),
            "no value provided for variables: #{unbound.join(', ')}"
    end
    node.value(memory)
  end
end

#solve(expression_hash, &block) ⇒ Object



74
75
76
# File 'lib/dentaku/calculator.rb', line 74

def solve(expression_hash, &block)
  BulkExpressionSolver.new(expression_hash, self).solve(&block)
end

#solve!(expression_hash) ⇒ Object



70
71
72
# File 'lib/dentaku/calculator.rb', line 70

def solve!(expression_hash)
  BulkExpressionSolver.new(expression_hash, self).solve!
end

#store(key_or_hash, value = nil) ⇒ Object Also known as: bind



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dentaku/calculator.rb', line 113

def store(key_or_hash, value = nil)
  restore = Hash[memory]

  if value.nil?
    key_or_hash = FlatHash.from_hash(key_or_hash) if nested_data_support
    key_or_hash.each do |key, val|
      memory[standardize_case(key.to_s)] = val
    end
  else
    memory[standardize_case(key_or_hash.to_s)] = value
  end

  if block_given?
    begin
      result = yield
      @memory = restore
      return result
    rescue => e
      @memory = restore
      raise e
    end
  end

  self
end

#store_formula(key, formula) ⇒ Object



140
141
142
# File 'lib/dentaku/calculator.rb', line 140

def store_formula(key, formula)
  store(key, ast(formula))
end