soroban 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -6,7 +6,6 @@ group :development do
6
6
  gem "rubyXL", "~> 1.2.7"
7
7
  gem "nokogiri", ">= 1.4.4"
8
8
  gem "rubyzip", ">= 0.9.4"
9
- # gem "rcov", ">= 0"
10
9
  end
11
10
 
12
11
  group :test do
data/README.md CHANGED
@@ -40,7 +40,7 @@ puts s.C1 # => 50
40
40
  Bindings
41
41
  --------
42
42
 
43
- Soroban allows you to bind meaningful variable names to individual cells and to ranges of cells. When bound to a range, variables act as an array,.
43
+ Soroban allows you to bind meaningful variable names to individual cells and to ranges of cells. When bound to a range, variables act as an array.
44
44
 
45
45
  ```ruby
46
46
  s.set(:A1 => 'hello', 'B1:B5' => [1,2,3,4,5])
@@ -77,7 +77,7 @@ s.set("D1:D5" => [1,2,3,4,5])
77
77
 
78
78
  s.missing # => []
79
79
 
80
- s.cells # => {"F1"=>"= E1 + SUM(D1:D5)", "E1"=>"= D1 ^ D2", "D1"=>"1", "D2"=>"2", "D3"=>"3", "D4"=>"4", "D5"=>"5"}
80
+ s.cells # => {:F1=>"= E1 + SUM(D1:D5)", :E1=>"= D1 ^ D2", :D1=>"1", :D2=>"2", :D3=>"3", :D4=>"4", :D5=>"5"}
81
81
  ```
82
82
 
83
83
  Importers
@@ -86,17 +86,25 @@ Importers
86
86
  Soroban has a built-in importer for xlsx files. It requires the [RubyXL](https://github.com/gilt/rubyXL) gem. Use it as follows:
87
87
 
88
88
  ```ruby
89
- require 'rubyXL'
90
-
91
89
  BINDINGS = {
92
- :gravity => :B1,
90
+ :planet => :B1,
93
91
  :mass => :B2,
94
- :force => :B10
92
+ :force => :B3
95
93
  }
96
94
 
97
- s = Soroban::Import::rubyXL("/Users/kranzky/Desktop/Physics.xlsx", 0, BINDINGS )
95
+ s = Soroban::Import::rubyXL("files/Physics.xlsx", 0, BINDINGS )
96
+
97
+ s.planet = 'Earth'
98
+ s.mass = 80
99
+ puts s.force # => 783.459251241996
100
+
101
+ s.planet = 'Venus'
102
+ s.mass = 80
103
+ puts s.force # => 710.044826106394
98
104
  ```
99
105
 
106
+ The above example parses the first sheet of Physics.xlsx, which you can [download](https://github.com/agworld/soroban/raw/master/files/Physics.xlsx).
107
+
100
108
  This import process returns a new Soroban::Sheet object that contains all the
101
109
  cells required to calculate the values of the bound variables, and which has the
102
110
  bindings set up correctly.
data/Soroban.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "soroban"
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jason Hutchens"]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "Rakefile",
29
29
  "Soroban.gemspec",
30
30
  "VERSION",
31
+ "files/Physics.xlsx",
31
32
  "lib/soroban.rb",
32
33
  "lib/soroban/cell.rb",
33
34
  "lib/soroban/error.rb",
@@ -54,6 +55,7 @@ Gem::Specification.new do |s|
54
55
  "lib/soroban/sheet.rb",
55
56
  "lib/soroban/value_walker.rb",
56
57
  "spec/documentation_spec.rb",
58
+ "spec/import_spec.rb",
57
59
  "spec/soroban_spec.rb",
58
60
  "spec/spec_helper.rb"
59
61
  ]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
Binary file
data/lib/soroban/cell.rb CHANGED
@@ -30,6 +30,8 @@ module Soroban
30
30
  raise Soroban::RecursionError, "Loop detected when evaluating '#{@excel}'" if @touched
31
31
  @touched = true
32
32
  eval(@ruby, @binding)
33
+ rescue TypeError, RangeError, ZeroDivisionError
34
+ nil
33
35
  ensure
34
36
  @touched = false
35
37
  end
@@ -3,8 +3,11 @@
3
3
  Soroban::define :VLOOKUP => lambda { |value, range, col, inexact|
4
4
  fc, fr, tc, tr = Soroban::getRange(range)
5
5
  i = walk("#{fc}#{fr}:#{fc}#{tr}").find_index(value)
6
- return nil if i.nil?
7
- (0...i).each { fr.next! }
8
- (1...col).each { fc.next! }
9
- eval("@#{fc}#{fr}.get")
6
+ if i.nil?
7
+ nil
8
+ else
9
+ (0...i).each { fr.next! }
10
+ (1...col).each { fc.next! }
11
+ eval("@#{fc}#{fr}.get")
12
+ end
10
13
  }
@@ -41,7 +41,7 @@ module Soroban
41
41
  def self.getPos(label)
42
42
  # TODO: fix for labels such as "BC42"
43
43
  match = /^([a-zA-Z]+)([\d]+)$/.match(label.to_s)
44
- return match[2].to_i - 1, match[1].upcase[0]-"A"[0]
44
+ return match[2].to_i - 1, match[1].upcase[0].ord-"A"[0].ord
45
45
  end
46
46
 
47
47
  # Return an array of values for the supplied arguments (which may be numbers, labels and ranges).
@@ -46,7 +46,6 @@ module Soroban
46
46
  data = cell.formula rescue nil
47
47
  data = "=#{data}" unless data.nil?
48
48
  data ||= cell.value.to_s rescue nil
49
- puts "#{label} => #{row},#{col} = #{data}"
50
49
  @model.set(label.to_sym => data)
51
50
  end
52
51
 
@@ -12,9 +12,15 @@ module Soroban
12
12
 
13
13
  # Yield the label of each cell referenced by the supplied range.
14
14
  def each
15
- (@fc..@tc).each do |col|
16
- (@fr..@tr).each do |row|
17
- yield "#{col}#{row}"
15
+ col, row = @fc, @fr
16
+ while true do
17
+ yield "#{col}#{row}"
18
+ break if row == @tr && col == @tc
19
+ if row == @tr
20
+ row = @fr
21
+ col = col.next
22
+ else
23
+ row = row.next
18
24
  end
19
25
  end
20
26
  end
@@ -51,14 +51,7 @@ module Soroban
51
51
  "'#{value}'"
52
52
  end
53
53
  def extract(value)
54
- fc, fr, tc, tr = Soroban::getRange(value)
55
- retval = []
56
- (fc..tc).each do |cc|
57
- (fr..tr).each do |cr|
58
- retval << "#{cc}#{cr}".to_sym
59
- end
60
- end
61
- retval
54
+ LabelWalker.new(value).map { |label| "#{label}".to_sym }
62
55
  end
63
56
  end
64
57
 
@@ -0,0 +1,27 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Documentation", :if => Gem.available?("rubyXL") do
4
+
5
+ it "can import xlsx files using RubyXL" do
6
+
7
+ BINDINGS = {
8
+ :planet => :B1,
9
+ :mass => :B2,
10
+ :force => :B3
11
+ }
12
+
13
+ s = Soroban::Import::rubyXL("files/Physics.xlsx", 0, BINDINGS )
14
+
15
+ s.planet = 'Earth'
16
+ s.mass = 80
17
+ puts s.force # => 783.459251241996
18
+ s.force.should be_within(0.01).of(783.46)
19
+
20
+ s.planet = 'Venus'
21
+ s.mass = 80
22
+ puts s.force # => 710.044826106394
23
+ s.force.should be_within(0.01).of(710.04)
24
+
25
+ end
26
+
27
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soroban
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Hutchens
@@ -102,6 +102,7 @@ files:
102
102
  - Rakefile
103
103
  - Soroban.gemspec
104
104
  - VERSION
105
+ - files/Physics.xlsx
105
106
  - lib/soroban.rb
106
107
  - lib/soroban/cell.rb
107
108
  - lib/soroban/error.rb
@@ -128,6 +129,7 @@ files:
128
129
  - lib/soroban/sheet.rb
129
130
  - lib/soroban/value_walker.rb
130
131
  - spec/documentation_spec.rb
132
+ - spec/import_spec.rb
131
133
  - spec/soroban_spec.rb
132
134
  - spec/spec_helper.rb
133
135
  homepage: https://github.com/agworld/soroban