dentaku 2.0.4 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/dentaku.rb +3 -0
- data/lib/dentaku/ast/arithmetic.rb +1 -1
- data/lib/dentaku/ast/combinators.rb +1 -1
- data/lib/dentaku/ast/grouping.rb +4 -0
- data/lib/dentaku/ast/negation.rb +5 -1
- data/lib/dentaku/ast/node.rb +4 -0
- data/lib/dentaku/token_scanner.rb +20 -8
- data/lib/dentaku/version.rb +1 -1
- data/spec/ast/node_spec.rb +3 -0
- data/spec/calculator_spec.rb +6 -0
- data/spec/token_scanner_spec.rb +16 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91e1c068c1ad4f4399eece3c6ac66ab9c17591cf
|
4
|
+
data.tar.gz: 2df26e17bb06fa076d30e56e880b5d5797df1dc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83222ea1e9505e2cdfa692f37142776fdb7be70cf39098d536b6a0c85958272421800f04e7876f9d7ba39bfe4d3eb19a49f2efa5b631b0ecddea4ed858c3e4ca
|
7
|
+
data.tar.gz: 32dc26ce15c748d7091c775356735fabf5abdf3f391db3407b663769e56c361247478edae5f3b3dc725aa97cb44b3a635e5212b293dc97af9940dedab83852a8
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## [v2.0.5] 2015-09-03
|
4
|
+
- fix bug with detecting unbound nodes
|
5
|
+
- silence warnings
|
6
|
+
- allow registration of custom token scanners
|
7
|
+
|
3
8
|
## [v2.0.4] 2015-09-03
|
4
9
|
- fix BigDecimal conversion bug
|
5
10
|
- add caching for bulk expression solving dependency order
|
@@ -85,6 +90,7 @@
|
|
85
90
|
## [v0.1.0] 2012-01-20
|
86
91
|
- initial release
|
87
92
|
|
93
|
+
[v2.0.5]: https://github.com/rubysolo/dentaku/compare/v2.0.4...v2.0.5
|
88
94
|
[v2.0.4]: https://github.com/rubysolo/dentaku/compare/v2.0.3...v2.0.4
|
89
95
|
[v2.0.3]: https://github.com/rubysolo/dentaku/compare/v2.0.1...v2.0.3
|
90
96
|
[v2.0.1]: https://github.com/rubysolo/dentaku/compare/v2.0.0...v2.0.1
|
data/lib/dentaku.rb
CHANGED
data/lib/dentaku/ast/grouping.rb
CHANGED
data/lib/dentaku/ast/negation.rb
CHANGED
@@ -26,10 +26,14 @@ module Dentaku
|
|
26
26
|
40
|
27
27
|
end
|
28
28
|
|
29
|
+
def dependencies(context={})
|
30
|
+
@node.dependencies(context)
|
31
|
+
end
|
32
|
+
|
29
33
|
private
|
30
34
|
|
31
35
|
def valid_node?(node)
|
32
|
-
node.
|
36
|
+
node.dependencies.any? || node.type == :numeric
|
33
37
|
end
|
34
38
|
end
|
35
39
|
end
|
data/lib/dentaku/ast/node.rb
CHANGED
@@ -41,16 +41,26 @@ module Dentaku
|
|
41
41
|
]
|
42
42
|
end
|
43
43
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
def register_default_scanners
|
45
|
+
register_scanners(available_scanners)
|
46
|
+
end
|
47
|
+
|
48
|
+
def register_scanners(scanner_ids)
|
49
|
+
@scanners = scanner_ids.each_with_object({}) do |id, scanners|
|
50
|
+
scanners[id] = self.send(id)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def register_scanner(id, scanner)
|
55
|
+
@scanners[id] = scanner
|
56
|
+
end
|
57
|
+
|
58
|
+
def scanners=(scanner_ids)
|
59
|
+
@scanners.select! { |k,v| scanner_ids.include?(k) }
|
48
60
|
end
|
49
61
|
|
50
62
|
def scanners
|
51
|
-
@scanners
|
52
|
-
self.send(scanner)
|
53
|
-
}
|
63
|
+
@scanners.values
|
54
64
|
end
|
55
65
|
|
56
66
|
def whitespace
|
@@ -112,8 +122,10 @@ module Dentaku
|
|
112
122
|
end
|
113
123
|
|
114
124
|
def identifier
|
115
|
-
new(:identifier, '\w+\b', lambda { |raw| raw.strip.downcase
|
125
|
+
new(:identifier, '\w+\b', lambda { |raw| raw.strip.downcase })
|
116
126
|
end
|
117
127
|
end
|
128
|
+
|
129
|
+
register_default_scanners
|
118
130
|
end
|
119
131
|
end
|
data/lib/dentaku/version.rb
CHANGED
data/spec/ast/node_spec.rb
CHANGED
data/spec/calculator_spec.rb
CHANGED
@@ -221,6 +221,12 @@ describe Dentaku::Calculator do
|
|
221
221
|
expect(calculator.evaluate('round(-1.23, 1)')).to eq(BigDecimal.new('-1.2'))
|
222
222
|
expect(calculator.evaluate('NOT(some_boolean) AND -1 > 3', some_boolean: true)).to be_falsey
|
223
223
|
end
|
224
|
+
|
225
|
+
it 'evaluates functions with stored variables' do
|
226
|
+
calculator.store("multi_color" => true, "number_of_sheets" => 5000, "sheets_per_minute_black" => 2000, "sheets_per_minute_color" => 1000)
|
227
|
+
result = calculator.evaluate('number_of_sheets / if(multi_color, sheets_per_minute_color, sheets_per_minute_black)')
|
228
|
+
expect(result).to eq(5)
|
229
|
+
end
|
224
230
|
end
|
225
231
|
|
226
232
|
describe 'math functions' do
|
data/spec/token_scanner_spec.rb
CHANGED
@@ -3,8 +3,13 @@ require 'dentaku/token_scanner'
|
|
3
3
|
describe Dentaku::TokenScanner do
|
4
4
|
let(:whitespace) { described_class.new(:whitespace, '\s') }
|
5
5
|
let(:numeric) { described_class.new(:numeric, '(\d+(\.\d+)?|\.\d+)',
|
6
|
-
|
6
|
+
->(raw) { raw =~ /\./ ? BigDecimal.new(raw) : raw.to_i })
|
7
7
|
}
|
8
|
+
let(:custom) { described_class.new(:identifier, '#\w+\b',
|
9
|
+
->(raw) { raw.gsub('#', '').to_sym })
|
10
|
+
}
|
11
|
+
|
12
|
+
after { described_class.register_default_scanners }
|
8
13
|
|
9
14
|
it 'returns a token for a matching string' do
|
10
15
|
token = whitespace.scan(' ').first
|
@@ -29,12 +34,20 @@ describe Dentaku::TokenScanner do
|
|
29
34
|
it 'allows customizing available scanners' do
|
30
35
|
described_class.scanners = [:whitespace, :numeric]
|
31
36
|
expect(described_class.scanners.length).to eq 2
|
32
|
-
described_class.scanners = described_class.available_scanners
|
33
37
|
end
|
34
38
|
|
35
39
|
it 'ignores invalid scanners' do
|
36
40
|
described_class.scanners = [:whitespace, :numeric, :fake]
|
37
41
|
expect(described_class.scanners.length).to eq 2
|
38
|
-
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'uses a custom scanner' do
|
45
|
+
described_class.scanners = [:whitespace, :numeric]
|
46
|
+
described_class.register_scanner(:custom, custom)
|
47
|
+
expect(described_class.scanners.length).to eq 3
|
48
|
+
|
49
|
+
token = custom.scan('#apple + #pear').first
|
50
|
+
expect(token.category).to eq(:identifier)
|
51
|
+
expect(token.value).to eq(:apple)
|
39
52
|
end
|
40
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dentaku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Solomon White
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
141
|
version: '0'
|
142
142
|
requirements: []
|
143
143
|
rubyforge_project: dentaku
|
144
|
-
rubygems_version: 2.4.
|
144
|
+
rubygems_version: 2.4.5.1
|
145
145
|
signing_key:
|
146
146
|
specification_version: 4
|
147
147
|
summary: A formula language parser and evaluator
|