dentaku 3.3.3 → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -7
  3. data/.travis.yml +4 -4
  4. data/CHANGELOG.md +34 -2
  5. data/README.md +4 -2
  6. data/dentaku.gemspec +1 -1
  7. data/lib/dentaku.rb +16 -5
  8. data/lib/dentaku/ast.rb +4 -0
  9. data/lib/dentaku/ast/access.rb +3 -1
  10. data/lib/dentaku/ast/arithmetic.rb +7 -2
  11. data/lib/dentaku/ast/array.rb +3 -1
  12. data/lib/dentaku/ast/case/case_else.rb +12 -4
  13. data/lib/dentaku/ast/case/case_switch_variable.rb +8 -0
  14. data/lib/dentaku/ast/case/case_then.rb +12 -4
  15. data/lib/dentaku/ast/case/case_when.rb +12 -4
  16. data/lib/dentaku/ast/function.rb +10 -1
  17. data/lib/dentaku/ast/functions/all.rb +36 -0
  18. data/lib/dentaku/ast/functions/any.rb +36 -0
  19. data/lib/dentaku/ast/functions/avg.rb +2 -2
  20. data/lib/dentaku/ast/functions/filter.rb +36 -0
  21. data/lib/dentaku/ast/functions/map.rb +36 -0
  22. data/lib/dentaku/ast/functions/pluck.rb +29 -0
  23. data/lib/dentaku/ast/functions/round.rb +1 -1
  24. data/lib/dentaku/ast/functions/rounddown.rb +1 -1
  25. data/lib/dentaku/ast/functions/roundup.rb +1 -1
  26. data/lib/dentaku/ast/functions/ruby_math.rb +49 -3
  27. data/lib/dentaku/ast/functions/string_functions.rb +52 -4
  28. data/lib/dentaku/ast/grouping.rb +3 -1
  29. data/lib/dentaku/ast/identifier.rb +6 -4
  30. data/lib/dentaku/bulk_expression_solver.rb +36 -25
  31. data/lib/dentaku/calculator.rb +14 -6
  32. data/lib/dentaku/date_arithmetic.rb +1 -1
  33. data/lib/dentaku/exceptions.rb +3 -3
  34. data/lib/dentaku/flat_hash.rb +7 -0
  35. data/lib/dentaku/parser.rb +2 -1
  36. data/lib/dentaku/tokenizer.rb +1 -1
  37. data/lib/dentaku/version.rb +1 -1
  38. data/spec/ast/arithmetic_spec.rb +19 -5
  39. data/spec/ast/avg_spec.rb +4 -0
  40. data/spec/ast/filter_spec.rb +18 -0
  41. data/spec/ast/map_spec.rb +15 -0
  42. data/spec/ast/max_spec.rb +13 -0
  43. data/spec/ast/min_spec.rb +13 -0
  44. data/spec/ast/mul_spec.rb +5 -0
  45. data/spec/ast/negation_spec.rb +18 -2
  46. data/spec/ast/round_spec.rb +10 -0
  47. data/spec/ast/rounddown_spec.rb +10 -0
  48. data/spec/ast/roundup_spec.rb +10 -0
  49. data/spec/ast/string_functions_spec.rb +35 -0
  50. data/spec/ast/sum_spec.rb +5 -0
  51. data/spec/bulk_expression_solver_spec.rb +27 -0
  52. data/spec/calculator_spec.rb +130 -0
  53. data/spec/dentaku_spec.rb +14 -8
  54. data/spec/parser_spec.rb +13 -0
  55. data/spec/tokenizer_spec.rb +24 -5
  56. metadata +26 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3028783062dbbe592ef71ed7bc388e57fb3af383e8cb897893dc858c4b772f63
4
- data.tar.gz: c940b16dcfdc9e0d1658a6b38b7521e7a17882c9519a1771d4117600a614100a
3
+ metadata.gz: 3dadbc209e58b2d2206dfaf48b83be3224db37f71d60cf590aadb63be9f4bebd
4
+ data.tar.gz: 86530c9c3333139994775a6bde16202005d89161a0b98670db58bcdda47f8e55
5
5
  SHA512:
6
- metadata.gz: 7e9633152d5e43e11b52db3a91e8c012816c2cf956e5e3906ce2c058c44d5733fc2668d57049dbc3c44d07155c12bd3bcd8f416e4012986ddcd937bd46cae49c
7
- data.tar.gz: bf89bf9096af4720832a7c904ce981a98beb69388996a1d29b190ce279fb8248eddc56175c5cc77646a36c65a0acc78dcd6c98fffb04ad0773cae22630eb3fdd
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/Tab:
91
+ Layout/IndentationStyle:
97
92
  Enabled: true
98
93
 
99
94
  # Blank lines should not have any spaces.
100
- Layout/TrailingBlankLines:
95
+ Layout/TrailingEmptyLines:
101
96
  Enabled: true
102
97
 
103
98
  # No trailing whitespace.
data/.travis.yml CHANGED
@@ -1,10 +1,10 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  rvm:
4
- - 2.3.8
5
- - 2.4.4
6
- - 2.5.3
7
- - 2.6.0
4
+ - 2.5.9
5
+ - 2.6.7
6
+ - 2.7.3
7
+ - 3.0.1
8
8
  before_install:
9
9
  - gem update bundler
10
10
  - gem update --system
data/CHANGELOG.md CHANGED
@@ -1,6 +1,34 @@
1
1
  # Change Log
2
2
 
3
- ## [Unreleased]
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
- [Unreleased]: https://github.com/rubysolo/dentaku/compare/v3.3.2...HEAD
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-2018 Solomon White
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.rubyforge_project = "dentaku"
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'
@@ -1,6 +1,8 @@
1
+ require_relative "./node"
2
+
1
3
  module Dentaku
2
4
  module AST
3
- class Access
5
+ class Access < Node
4
6
  def self.arity
5
7
  2
6
8
  end
@@ -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
- l.public_send(operator, r)
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)
@@ -1,6 +1,8 @@
1
+ require_relative "./node"
2
+
1
3
  module Dentaku
2
4
  module AST
3
- class Array
5
+ class Array < Node
4
6
  def self.arity
5
7
  end
6
8
 
@@ -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
@@ -16,6 +16,14 @@ module Dentaku
16
16
  def self.arity
17
17
  1
18
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
@@ -12,7 +12,16 @@ module Dentaku
12
12
  end
13
13
 
14
14
  def dependencies(context = {})
15
- @args.flat_map { |a| a.dependencies(context) }
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)