kalculator 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kalculator.rb +1 -0
- data/lib/kalculator/evaluator.rb +3 -5
- data/lib/kalculator/nested_lookup.rb +25 -0
- data/lib/kalculator/parser.rb +1 -1
- data/lib/kalculator/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cb0411bb21bb99550cde9115cb53218b956d7fa107779b8e318446114fa6f3e
|
4
|
+
data.tar.gz: 1aad75a31f459f8a6b3c2390b073ff507c27d5ec56b5bf017d91c5f14ff41bce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b25b44e6d18de63b49db47effbf77661d1ae1556e5b61745be03bd3a813bda3afe7218815af54132ac59ffee4e32314d6bdb7ff993ca4255da7b713ac64633cd
|
7
|
+
data.tar.gz: dbede4204b904eb0f9969da284facdc0b91827878e87430dfa10d45bccc3e7a9fa373fa78cf26d63c7d6f96f38f604d074dbc622fb5fe01ca19b5bc08d881733
|
data/lib/kalculator.rb
CHANGED
data/lib/kalculator/evaluator.rb
CHANGED
@@ -81,11 +81,9 @@ class Kalculator
|
|
81
81
|
array.inject(0){|sum, num| sum + num}
|
82
82
|
end
|
83
83
|
|
84
|
-
def variable(_,
|
85
|
-
|
86
|
-
|
87
|
-
source[name]
|
88
|
-
end
|
84
|
+
def variable(_, name)
|
85
|
+
raise UndefinedVariableError, "undefined variable #{name}" unless @data_source.key?(name)
|
86
|
+
@data_source[name]
|
89
87
|
end
|
90
88
|
end
|
91
89
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Kalculator
|
2
|
+
class NestedLookup
|
3
|
+
def initialize(data_source)
|
4
|
+
@data_source = data_source
|
5
|
+
end
|
6
|
+
|
7
|
+
def key?(name)
|
8
|
+
names = name.split(".")
|
9
|
+
source = @data_source
|
10
|
+
names.all? do |name|
|
11
|
+
break false unless source.key?(name)
|
12
|
+
source = source[name]
|
13
|
+
true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def [](name)
|
18
|
+
names = name.split(".")
|
19
|
+
names.inject(@data_source) do |source, name|
|
20
|
+
break nil unless source.key?(name)
|
21
|
+
source[name]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/kalculator/parser.rb
CHANGED
@@ -20,7 +20,7 @@ class Kalculator
|
|
20
20
|
|
21
21
|
clause('NUMBER') { |n| [:number, n] }
|
22
22
|
clause('STRING') { |s| [:string, s] }
|
23
|
-
clause('IDENT') { |n| [:variable, n
|
23
|
+
clause('IDENT') { |n| [:variable, n] }
|
24
24
|
clause('TRUE') { |n| [:boolean, true] }
|
25
25
|
clause('FALSE') { |n| [:boolean, false] }
|
26
26
|
|
data/lib/kalculator/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kalculator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Ries
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rltk
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- lib/kalculator/evaluator.rb
|
95
95
|
- lib/kalculator/formula.rb
|
96
96
|
- lib/kalculator/lexer.rb
|
97
|
+
- lib/kalculator/nested_lookup.rb
|
97
98
|
- lib/kalculator/parser.rb
|
98
99
|
- lib/kalculator/version.rb
|
99
100
|
homepage: https://github.com/mmmries/kalculator
|