dentaku 3.3.3 → 3.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -7
- data/.travis.yml +4 -4
- data/CHANGELOG.md +34 -2
- data/README.md +4 -2
- data/dentaku.gemspec +1 -1
- data/lib/dentaku.rb +16 -5
- data/lib/dentaku/ast.rb +4 -0
- data/lib/dentaku/ast/access.rb +3 -1
- data/lib/dentaku/ast/arithmetic.rb +7 -2
- data/lib/dentaku/ast/array.rb +3 -1
- data/lib/dentaku/ast/case/case_else.rb +12 -4
- data/lib/dentaku/ast/case/case_switch_variable.rb +8 -0
- data/lib/dentaku/ast/case/case_then.rb +12 -4
- data/lib/dentaku/ast/case/case_when.rb +12 -4
- data/lib/dentaku/ast/function.rb +10 -1
- data/lib/dentaku/ast/functions/all.rb +36 -0
- data/lib/dentaku/ast/functions/any.rb +36 -0
- data/lib/dentaku/ast/functions/avg.rb +2 -2
- data/lib/dentaku/ast/functions/filter.rb +36 -0
- data/lib/dentaku/ast/functions/map.rb +36 -0
- data/lib/dentaku/ast/functions/pluck.rb +29 -0
- data/lib/dentaku/ast/functions/round.rb +1 -1
- data/lib/dentaku/ast/functions/rounddown.rb +1 -1
- data/lib/dentaku/ast/functions/roundup.rb +1 -1
- data/lib/dentaku/ast/functions/ruby_math.rb +49 -3
- data/lib/dentaku/ast/functions/string_functions.rb +52 -4
- data/lib/dentaku/ast/grouping.rb +3 -1
- data/lib/dentaku/ast/identifier.rb +6 -4
- data/lib/dentaku/bulk_expression_solver.rb +36 -25
- data/lib/dentaku/calculator.rb +14 -6
- data/lib/dentaku/date_arithmetic.rb +1 -1
- data/lib/dentaku/exceptions.rb +3 -3
- data/lib/dentaku/flat_hash.rb +7 -0
- data/lib/dentaku/parser.rb +2 -1
- data/lib/dentaku/tokenizer.rb +1 -1
- data/lib/dentaku/version.rb +1 -1
- data/spec/ast/arithmetic_spec.rb +19 -5
- data/spec/ast/avg_spec.rb +4 -0
- data/spec/ast/filter_spec.rb +18 -0
- data/spec/ast/map_spec.rb +15 -0
- data/spec/ast/max_spec.rb +13 -0
- data/spec/ast/min_spec.rb +13 -0
- data/spec/ast/mul_spec.rb +5 -0
- data/spec/ast/negation_spec.rb +18 -2
- data/spec/ast/round_spec.rb +10 -0
- data/spec/ast/rounddown_spec.rb +10 -0
- data/spec/ast/roundup_spec.rb +10 -0
- data/spec/ast/string_functions_spec.rb +35 -0
- data/spec/ast/sum_spec.rb +5 -0
- data/spec/bulk_expression_solver_spec.rb +27 -0
- data/spec/calculator_spec.rb +130 -0
- data/spec/dentaku_spec.rb +14 -8
- data/spec/parser_spec.rb +13 -0
- data/spec/tokenizer_spec.rb +24 -5
- metadata +26 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3dadbc209e58b2d2206dfaf48b83be3224db37f71d60cf590aadb63be9f4bebd
|
4
|
+
data.tar.gz: 86530c9c3333139994775a6bde16202005d89161a0b98670db58bcdda47f8e55
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f132401168218a3c021124ddabb56bbf485550bdff60a03a291ad811d8628e25caab2ba36e4bed4f8148bde048e56bed1fa31480774f2c19617a79282da81280
|
7
|
+
data.tar.gz: 3b6a8fc5e40988a9442be9dbdf890dc813e13b95fc2d0cb2f60252b2492d07fdf4ba47cdfedd0a85c002aaaa23a83bffb141b8f8f31fbf518678439d55c1e795
|
data/.rubocop.yml
CHANGED
@@ -8,11 +8,6 @@ AllCops:
|
|
8
8
|
Style/AndOr:
|
9
9
|
Enabled: true
|
10
10
|
|
11
|
-
# Do not use braces for hash literals when they are the last argument of a
|
12
|
-
# method call.
|
13
|
-
Style/BracesAroundHashParameters:
|
14
|
-
Enabled: true
|
15
|
-
|
16
11
|
# Align `when` with `case`.
|
17
12
|
Layout/CaseIndentation:
|
18
13
|
Enabled: true
|
@@ -93,11 +88,11 @@ Style/StringLiterals:
|
|
93
88
|
EnforcedStyle: double_quotes
|
94
89
|
|
95
90
|
# Detect hard tabs, no hard tabs.
|
96
|
-
Layout/
|
91
|
+
Layout/IndentationStyle:
|
97
92
|
Enabled: true
|
98
93
|
|
99
94
|
# Blank lines should not have any spaces.
|
100
|
-
Layout/
|
95
|
+
Layout/TrailingEmptyLines:
|
101
96
|
Enabled: true
|
102
97
|
|
103
98
|
# No trailing whitespace.
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,34 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
-
## [
|
3
|
+
## [v3.4.2]
|
4
|
+
- add FILTER function
|
5
|
+
- add concurrent-ruby dependency to make global calculator object thread safe
|
6
|
+
- add Ruby 3 support
|
7
|
+
- allow formulas to access intermediate context values
|
8
|
+
- fix incorrect Ruby Math function return type
|
9
|
+
- fix context mutation bug
|
10
|
+
- fix dependency resolution bug
|
11
|
+
|
12
|
+
## [v3.4.1] 2020-12-12
|
13
|
+
- prevent extra evaluations in bulk expression solver
|
14
|
+
|
15
|
+
## [v3.4.0] 2020-12-07
|
16
|
+
- allow access to intermediate values of flattened hashes
|
17
|
+
- catch invalid array syntax in the parse phase
|
18
|
+
- drop support for Ruby < 2.5, add support for Ruby 2.7
|
19
|
+
- add support for subtracting date literals
|
20
|
+
- improve error handling
|
21
|
+
- improve math function implementation
|
22
|
+
- add caching for calculated variable values
|
23
|
+
- allow custom unbound variable handling block at Dentaku module level
|
24
|
+
- add enum functions `ANY`, `ALL`, `MAP` and `PLUCK`
|
25
|
+
- allow self-referential formulas in bulk expression solver
|
26
|
+
- misc internal fixes and enhancements
|
27
|
+
|
28
|
+
## [v3.3.4] 2019-11-21
|
29
|
+
- bugfix release
|
30
|
+
|
31
|
+
## [v3.3.3] 2019-11-20
|
4
32
|
- date / duration addition and subtraction
|
5
33
|
- validate arity for custom functions with variable arity
|
6
34
|
- make AST serializable with Marshal.dump
|
@@ -184,7 +212,11 @@
|
|
184
212
|
## [v0.1.0] 2012-01-20
|
185
213
|
- initial release
|
186
214
|
|
187
|
-
[
|
215
|
+
[v3.4.2]: https://github.com/rubysolo/dentaku/compare/v3.4.1...v3.4.2
|
216
|
+
[v3.4.1]: https://github.com/rubysolo/dentaku/compare/v3.4.0...v3.4.1
|
217
|
+
[v3.4.0]: https://github.com/rubysolo/dentaku/compare/v3.3.4...v3.4.0
|
218
|
+
[v3.3.4]: https://github.com/rubysolo/dentaku/compare/v3.3.3...v3.3.4
|
219
|
+
[v3.3.3]: https://github.com/rubysolo/dentaku/compare/v3.3.2...v3.3.3
|
188
220
|
[v3.3.2]: https://github.com/rubysolo/dentaku/compare/v3.3.1...v3.3.2
|
189
221
|
[v3.3.1]: https://github.com/rubysolo/dentaku/compare/v3.3.0...v3.3.1
|
190
222
|
[v3.3.0]: https://github.com/rubysolo/dentaku/compare/v3.2.1...v3.3.0
|
data/README.md
CHANGED
@@ -152,6 +152,8 @@ Selections: `CASE` (syntax see [spec](https://github.com/rubysolo/dentaku/blob/m
|
|
152
152
|
|
153
153
|
String: `LEFT`, `RIGHT`, `MID`, `LEN`, `FIND`, `SUBSTITUTE`, `CONCAT`, `CONTAINS`
|
154
154
|
|
155
|
+
Collection: `MAP`, `FILTER`, `ALL`, `ANY`, `PLUCK`
|
156
|
+
|
155
157
|
RESOLVING DEPENDENCIES
|
156
158
|
----------------------
|
157
159
|
|
@@ -284,7 +286,7 @@ using Calculator#add_functions.
|
|
284
286
|
FUNCTION ALIASES
|
285
287
|
----------------
|
286
288
|
|
287
|
-
Every function can be aliased by synonyms. For example, it can be useful if
|
289
|
+
Every function can be aliased by synonyms. For example, it can be useful if
|
288
290
|
your application is multilingual.
|
289
291
|
|
290
292
|
```ruby
|
@@ -321,7 +323,7 @@ LICENSE
|
|
321
323
|
|
322
324
|
(The MIT License)
|
323
325
|
|
324
|
-
Copyright © 2012-
|
326
|
+
Copyright © 2012-2019 Solomon White
|
325
327
|
|
326
328
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
327
329
|
this software and associated documentation files (the ‘Software’), to deal in
|
data/dentaku.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
Dentaku is a parser and evaluator for mathematical formulas
|
15
15
|
DESC
|
16
16
|
|
17
|
-
s.
|
17
|
+
s.add_dependency('concurrent-ruby')
|
18
18
|
|
19
19
|
s.add_development_dependency('codecov')
|
20
20
|
s.add_development_dependency('pry')
|
data/lib/dentaku.rb
CHANGED
@@ -1,23 +1,26 @@
|
|
1
1
|
require "bigdecimal"
|
2
|
+
require "concurrent"
|
2
3
|
require "dentaku/calculator"
|
3
4
|
require "dentaku/version"
|
4
5
|
|
5
6
|
module Dentaku
|
6
7
|
@enable_ast_caching = false
|
7
8
|
@enable_dependency_order_caching = false
|
9
|
+
@enable_identifier_caching = false
|
8
10
|
@aliases = {}
|
9
11
|
|
10
|
-
def self.evaluate(expression, data = {})
|
11
|
-
calculator.evaluate(expression, data)
|
12
|
+
def self.evaluate(expression, data = {}, &block)
|
13
|
+
calculator.value.evaluate(expression, data, &block)
|
12
14
|
end
|
13
15
|
|
14
|
-
def self.evaluate!(expression, data = {})
|
15
|
-
calculator.evaluate!(expression, data)
|
16
|
+
def self.evaluate!(expression, data = {}, &block)
|
17
|
+
calculator.value.evaluate!(expression, data, &block)
|
16
18
|
end
|
17
19
|
|
18
20
|
def self.enable_caching!
|
19
21
|
enable_ast_cache!
|
20
22
|
enable_dependency_order_cache!
|
23
|
+
enable_identifier_cache!
|
21
24
|
end
|
22
25
|
|
23
26
|
def self.enable_ast_cache!
|
@@ -36,6 +39,14 @@ module Dentaku
|
|
36
39
|
@enable_dependency_order_caching
|
37
40
|
end
|
38
41
|
|
42
|
+
def self.enable_identifier_cache!
|
43
|
+
@enable_identifier_caching = true
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.cache_identifier?
|
47
|
+
@enable_identifier_caching
|
48
|
+
end
|
49
|
+
|
39
50
|
def self.aliases
|
40
51
|
@aliases
|
41
52
|
end
|
@@ -45,7 +56,7 @@ module Dentaku
|
|
45
56
|
end
|
46
57
|
|
47
58
|
def self.calculator
|
48
|
-
@calculator ||= Dentaku::Calculator.new
|
59
|
+
@calculator ||= Concurrent::ThreadLocalVar.new { Dentaku::Calculator.new }
|
49
60
|
end
|
50
61
|
end
|
51
62
|
|
data/lib/dentaku/ast.rb
CHANGED
@@ -15,15 +15,19 @@ require_relative './ast/array'
|
|
15
15
|
require_relative './ast/grouping'
|
16
16
|
require_relative './ast/case'
|
17
17
|
require_relative './ast/function_registry'
|
18
|
+
require_relative './ast/functions/all'
|
18
19
|
require_relative './ast/functions/and'
|
20
|
+
require_relative './ast/functions/any'
|
19
21
|
require_relative './ast/functions/avg'
|
20
22
|
require_relative './ast/functions/count'
|
21
23
|
require_relative './ast/functions/duration'
|
22
24
|
require_relative './ast/functions/if'
|
25
|
+
require_relative './ast/functions/map'
|
23
26
|
require_relative './ast/functions/max'
|
24
27
|
require_relative './ast/functions/min'
|
25
28
|
require_relative './ast/functions/not'
|
26
29
|
require_relative './ast/functions/or'
|
30
|
+
require_relative './ast/functions/pluck'
|
27
31
|
require_relative './ast/functions/round'
|
28
32
|
require_relative './ast/functions/rounddown'
|
29
33
|
require_relative './ast/functions/roundup'
|
data/lib/dentaku/ast/access.rb
CHANGED
@@ -31,7 +31,12 @@ module Dentaku
|
|
31
31
|
def value(context = {})
|
32
32
|
l = cast(left.value(context))
|
33
33
|
r = cast(right.value(context))
|
34
|
-
|
34
|
+
begin
|
35
|
+
l.public_send(operator, r)
|
36
|
+
rescue ::TypeError => e
|
37
|
+
# Right cannot be converted to a suitable type for left. e.g. [] + 1
|
38
|
+
raise Dentaku::ArgumentError.for(:incompatible_type, value: r, for: l.class), e.message
|
39
|
+
end
|
35
40
|
end
|
36
41
|
|
37
42
|
private
|
@@ -60,7 +65,7 @@ module Dentaku
|
|
60
65
|
end
|
61
66
|
|
62
67
|
def valid_right?
|
63
|
-
valid_node?(right) || right.type == :duration
|
68
|
+
valid_node?(right) || right.type == :duration || right.type == :datetime
|
64
69
|
end
|
65
70
|
|
66
71
|
def validate_value(val)
|
data/lib/dentaku/ast/array.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
module Dentaku
|
2
2
|
module AST
|
3
3
|
class CaseElse < Node
|
4
|
-
def self.arity
|
5
|
-
1
|
6
|
-
end
|
7
|
-
|
8
4
|
def initialize(node)
|
9
5
|
@node = node
|
10
6
|
end
|
@@ -16,6 +12,18 @@ module Dentaku
|
|
16
12
|
def dependencies(context = {})
|
17
13
|
@node.dependencies(context)
|
18
14
|
end
|
15
|
+
|
16
|
+
def self.arity
|
17
|
+
1
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.min_param_count
|
21
|
+
1
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.max_param_count
|
25
|
+
1
|
26
|
+
end
|
19
27
|
end
|
20
28
|
end
|
21
29
|
end
|
@@ -1,10 +1,6 @@
|
|
1
1
|
module Dentaku
|
2
2
|
module AST
|
3
3
|
class CaseThen < Node
|
4
|
-
def self.arity
|
5
|
-
1
|
6
|
-
end
|
7
|
-
|
8
4
|
def initialize(node)
|
9
5
|
@node = node
|
10
6
|
end
|
@@ -16,6 +12,18 @@ module Dentaku
|
|
16
12
|
def dependencies(context = {})
|
17
13
|
@node.dependencies(context)
|
18
14
|
end
|
15
|
+
|
16
|
+
def self.arity
|
17
|
+
1
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.min_param_count
|
21
|
+
1
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.max_param_count
|
25
|
+
1
|
26
|
+
end
|
19
27
|
end
|
20
28
|
end
|
21
29
|
end
|
@@ -1,10 +1,6 @@
|
|
1
1
|
module Dentaku
|
2
2
|
module AST
|
3
3
|
class CaseWhen < Operation
|
4
|
-
def self.arity
|
5
|
-
1
|
6
|
-
end
|
7
|
-
|
8
4
|
def initialize(node)
|
9
5
|
@node = node
|
10
6
|
end
|
@@ -16,6 +12,18 @@ module Dentaku
|
|
16
12
|
def dependencies(context = {})
|
17
13
|
@node.dependencies(context)
|
18
14
|
end
|
15
|
+
|
16
|
+
def self.arity
|
17
|
+
1
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.min_param_count
|
21
|
+
1
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.max_param_count
|
25
|
+
1
|
26
|
+
end
|
19
27
|
end
|
20
28
|
end
|
21
29
|
end
|
data/lib/dentaku/ast/function.rb
CHANGED
@@ -12,7 +12,16 @@ module Dentaku
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def dependencies(context = {})
|
15
|
-
|
15
|
+
deferred = deferred_args
|
16
|
+
@args.each_with_index
|
17
|
+
.reject { |_, i| deferred.include? i }
|
18
|
+
.flat_map { |a, _| a.dependencies(context) }
|
19
|
+
end
|
20
|
+
|
21
|
+
# override if your function implementation needs to defer evaluation of
|
22
|
+
# any arguments
|
23
|
+
def deferred_args
|
24
|
+
[]
|
16
25
|
end
|
17
26
|
|
18
27
|
def self.get(name)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative '../function'
|
2
|
+
require_relative '../../exceptions'
|
3
|
+
|
4
|
+
module Dentaku
|
5
|
+
module AST
|
6
|
+
class All < Function
|
7
|
+
def self.min_param_count
|
8
|
+
3
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.max_param_count
|
12
|
+
3
|
13
|
+
end
|
14
|
+
|
15
|
+
def deferred_args
|
16
|
+
[1, 2]
|
17
|
+
end
|
18
|
+
|
19
|
+
def value(context = {})
|
20
|
+
collection = @args[0].value(context)
|
21
|
+
item_identifier = @args[1].identifier
|
22
|
+
expression = @args[2]
|
23
|
+
|
24
|
+
Array(collection).all? do |item_value|
|
25
|
+
expression.value(
|
26
|
+
context.merge(
|
27
|
+
FlatHash.from_hash_with_intermediates(item_identifier => item_value)
|
28
|
+
)
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Dentaku::AST::Function.register_class(:all, Dentaku::AST::All)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative '../function'
|
2
|
+
require_relative '../../exceptions'
|
3
|
+
|
4
|
+
module Dentaku
|
5
|
+
module AST
|
6
|
+
class Any < Function
|
7
|
+
def self.min_param_count
|
8
|
+
3
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.max_param_count
|
12
|
+
3
|
13
|
+
end
|
14
|
+
|
15
|
+
def deferred_args
|
16
|
+
[1, 2]
|
17
|
+
end
|
18
|
+
|
19
|
+
def value(context = {})
|
20
|
+
collection = @args[0].value(context)
|
21
|
+
item_identifier = @args[1].identifier
|
22
|
+
expression = @args[2]
|
23
|
+
|
24
|
+
Array(collection).any? do |item_value|
|
25
|
+
expression.value(
|
26
|
+
context.merge(
|
27
|
+
FlatHash.from_hash_with_intermediates(item_identifier => item_value)
|
28
|
+
)
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Dentaku::AST::Function.register_class(:any, Dentaku::AST::Any)
|