Class: Soroban::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/soroban/sheet.rb

Overview

A container for cells.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSheet

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

#bindingsObject (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

#cellsObject

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

#missingObject

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

#walk(range) ⇒ Object

Visit each cell in the supplied range, yielding its value.



63
64
65
# File 'lib/soroban/sheet.rb', line 63

def walk(range)
  Walker.new(range, binding)
end