Class: Soroban::Sheet
- Inherits:
-
Object
- Object
- Soroban::Sheet
- Defined in:
- lib/soroban/sheet.rb
Overview
A container for cells.
Instance Attribute Summary collapse
-
#bindings ⇒ Object
readonly
Returns the value of attribute bindings.
Instance Method Summary collapse
-
#bind(name, label) ⇒ Object
Bind one or more named variables to a cell.
-
#cells ⇒ Object
Return a hash of ‘label => contents` for each cell in the sheet.
-
#get(label_or_name) ⇒ Object
Retrieve the contents of a cell.
-
#initialize ⇒ Sheet
constructor
Creates a new sheet.
-
#method_missing(method, *args, &block) ⇒ Object
Used for calling dynamically defined functions, and for creating new cells (via ‘label=`).
-
#missing ⇒ Object
Return a list of referenced but undefined cells.
-
#set(label_or_range, contents) ⇒ Object
Set the contents of one or more cells or ranges.
-
#walk(range) ⇒ Object
Visit each cell in the supplied range, yielding its value.
Constructor Details
#initialize ⇒ Sheet
Creates a new sheet.
13 14 15 16 |
# File 'lib/soroban/sheet.rb', line 13 def initialize @cells = {} @bindings = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Used for calling dynamically defined functions, and for creating new cells (via ‘label=`).
20 21 22 23 24 25 26 27 |
# File 'lib/soroban/sheet.rb', line 20 def method_missing(method, *args, &block) if match = /^func_(.*)$/i.match(method.to_s) return Soroban::call(self, match[1], *args) elsif match = /^([a-z][\w]*)=$/i.match(method.to_s) return _add(match[1], args[0]) end super end |
Instance Attribute Details
#bindings ⇒ Object (readonly)
Returns the value of attribute bindings.
10 11 12 |
# File 'lib/soroban/sheet.rb', line 10 def bindings @bindings end |
Instance Method Details
#bind(name, label) ⇒ Object
Bind one or more named variables to a cell.
55 56 57 58 59 60 |
# File 'lib/soroban/sheet.rb', line 55 def bind(name, label) unless @cells.keys.include?(label.to_sym) raise Soroban::UndefinedError, "Cannot bind '#{name}' to non-existent cell '#{label}'" end _bind(name, label) end |
#cells ⇒ Object
Return a hash of ‘label => contents` for each cell in the sheet.
68 69 70 |
# File 'lib/soroban/sheet.rb', line 68 def cells Hash[@cells.keys.map { |label| label.to_s }.zip( @cells.keys.map { |label| eval("@#{label}.excel") } )] end |
#get(label_or_name) ⇒ Object
Retrieve the contents of a cell.
50 51 52 |
# File 'lib/soroban/sheet.rb', line 50 def get(label_or_name) _get(label_or_name, eval("@#{label_or_name}", binding)) end |
#missing ⇒ Object
Return a list of referenced but undefined cells.
73 74 75 |
# File 'lib/soroban/sheet.rb', line 73 def missing @cells.values.map.flatten.uniq - @cells.keys end |
#set(label_or_range, contents) ⇒ Object
Set the contents of one or more cells or ranges.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/soroban/sheet.rb', line 30 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 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 |