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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df50bd8eebb0124d8656b68a187b42963c2a8d1a4e14018e79f9b2e12fa9e77e
4
- data.tar.gz: e414204de2fca16a6ee2313508dc4c10aa9755ddc9fd95ab77083c72b33c1687
3
+ metadata.gz: 3cb0411bb21bb99550cde9115cb53218b956d7fa107779b8e318446114fa6f3e
4
+ data.tar.gz: 1aad75a31f459f8a6b3c2390b073ff507c27d5ec56b5bf017d91c5f14ff41bce
5
5
  SHA512:
6
- metadata.gz: f2bdd16d1393e2f1991aa0240cbe4ea761fd4b3db0bb1ec2711646fd855305dac8eff52d9c6a271a2a6df39c5eaffaf9623ade7ba9984b7ac122c0454cd9e4d4
7
- data.tar.gz: 74102a128ee39aa879a5380a0c7934b0d5bc11ee618553431808e39dd390c873c4a6246338e587c6c7a4de4051622564444d11cd005dca6927e95845c06763eb
6
+ metadata.gz: b25b44e6d18de63b49db47effbf77661d1ae1556e5b61745be03bd3a813bda3afe7218815af54132ac59ffee4e32314d6bdb7ff993ca4255da7b713ac64633cd
7
+ data.tar.gz: dbede4204b904eb0f9969da284facdc0b91827878e87430dfa10d45bccc3e7a9fa373fa78cf26d63c7d6f96f38f604d074dbc622fb5fe01ca19b5bc08d881733
data/lib/kalculator.rb CHANGED
@@ -4,6 +4,7 @@ require "kalculator/errors"
4
4
  require "kalculator/evaluator"
5
5
  require "kalculator/formula"
6
6
  require "kalculator/lexer"
7
+ require "kalculator/nested_lookup"
7
8
  require "kalculator/parser"
8
9
  require "kalculator/version"
9
10
 
@@ -81,11 +81,9 @@ class Kalculator
81
81
  array.inject(0){|sum, num| sum + num}
82
82
  end
83
83
 
84
- def variable(_, names)
85
- names.inject(@data_source) do |source, name|
86
- raise UndefinedVariableError, "undefined variable #{names.join(".")} (could not find #{name} in #{source}" unless source.key?(name)
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
@@ -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.split(".")] }
23
+ clause('IDENT') { |n| [:variable, n] }
24
24
  clause('TRUE') { |n| [:boolean, true] }
25
25
  clause('FALSE') { |n| [:boolean, false] }
26
26
 
@@ -1,3 +1,3 @@
1
1
  class Kalculator
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
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.3.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-04-25 00:00:00.000000000 Z
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