Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
set and bind now accept hash arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Hutchens committed Apr 24, 2012
1 parent 1033990 commit 04f349f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
48 changes: 26 additions & 22 deletions lib/soroban/sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
require 'soroban/value_walker'
require 'soroban/cell'

# TODO: set and bind accept a hash
# TODO: bind can bind to a range (acts as an array)

module Soroban

# A container for cells.
Expand All @@ -31,22 +28,24 @@ def method_missing(method, *args, &block)
end

# Set the contents of one or more cells or ranges.
def set(label_or_range, contents)
unless range = Soroban::getRange(label_or_range)
return _add(label_or_range, contents)
end
fc, fr, tc, tr = range
if fc == tc || fr == tr
raise ArgumentError, "Expecting an array when setting #{label_or_range}" unless contents.kind_of? Array
cc, cr = fc, fr
contents.each do |item|
set("#{cc}#{cr}", item)
cc.next! if fr == tr
cr.next! if fc == tc
def set(options_hash)
options_hash.each do |label_or_range, contents|
unless range = Soroban::getRange(label_or_range)
return _add(label_or_range, contents)
end
fc, fr, tc, tr = range
if fc == tc || fr == tr
raise ArgumentError, "Expecting an array when setting #{label_or_range}" unless contents.kind_of? Array
cc, cr = fc, fr
contents.each do |item|
set("#{cc}#{cr}" => item)
cc.next! if fr == tr
cr.next! if fc == tc
end
raise Soroban::RangeError, "Supplied array doesn't match range length" if cc != tc && cr != tr
else
raise ArgumentError, "Can only set cells or 1-dimensional ranges of cells"
end
raise Soroban::RangeError, "Supplied array doesn't match range length" if cc != tc && cr != tr
else
raise ArgumentError, "Can only set cells or 1-dimensional ranges of cells"
end
end

Expand All @@ -56,11 +55,16 @@ def get(label_or_name)
end

# Bind one or more named variables to a cell.
def bind(name, label)
unless @cells.keys.include?(label.to_sym)
raise Soroban::UndefinedError, "Cannot bind '#{name}' to non-existent cell '#{label}'"
def bind(options_hash)
options_hash.each do |name, label_or_range|
if Soroban::range?(label_or_range)
else
unless @cells.keys.include?(label_or_range.to_sym)
raise Soroban::UndefinedError, "Cannot bind '#{name}' to non-existent cell '#{label_or_range}'"
end
_bind(name, label_or_range)
end
end
_bind(name, label)
end

# Visit each cell in the supplied range, yielding its value.
Expand Down
16 changes: 6 additions & 10 deletions spec/soroban_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
end

it "can set a value" do
sheet.set(:foo, 'hello')
sheet.set(:foo => 'hello')
sheet.foo.should eq('hello')
end

Expand All @@ -33,30 +33,26 @@
end

it "can set an array" do
sheet.set("A1:A5", [ 1, 2, 3, 4, 5 ])
sheet.set("A1:A5" => [ 1, 2, 3, 4, 5 ])
sheet.B1 = '=SUM(A1:A5)'
sheet.B1.should eq(15)
end

it "can set a hash" do
sheet.set("A1:A3", [ 'one', 'two', 'three' ])
sheet.set("B1:B3", [ 'mop', 'hai', 'bah' ])
sheet.set("A1:A3" => [ 'one', 'two', 'three' ], "B1:B3" => [ 'mop', 'hai', 'bah' ])
sheet.C1 = '=VLOOKUP("two", A1:B3, 2, 0)'
sheet.C1.should eq('hai')
end

it "can iterate over all cells" do
sheet.set("A1:A3", [ 1, 2, 3 ])
sheet.set("B1:B3", [ 4, 5, 6 ])
sheet.set("C1:C3", [ 7, 8, 9 ])
sheet.set("A1:A3" => [ 1, 2, 3 ], "B1:B3" => [ 4, 5, 6 ], "C1:C3" => [ 7, 8, 9 ])
sheet.cells.map { |label, contents| contents.to_i }.sort.should eq [1,2,3,4,5,6,7,8,9]
end

it "can bind variables to cells" do
sheet.A1 = 0
sheet.A2 = "=A1^2"
sheet.bind(:input, :A1)
sheet.bind(:output, :A2)
sheet.bind(:input => :A1, :output => :A2)
sheet.input = 5
sheet.output.should eq(25)
sheet.bindings.keys.should include :input
Expand Down Expand Up @@ -90,7 +86,7 @@

it "can reject valid ruby code in formulas" do
lambda {
sheet.set(:A1, "=3**2")
sheet.set(:A1 => "=3**2")
}.should raise_error(Soroban::ParseError)
end

Expand Down

0 comments on commit 04f349f

Please sign in to comment.