Class: Soroban::Cell

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

Overview

Represents a single cell in a sheet. This class is used internally, and isn't exposed to the caller. The cell stores the original string representation of its contents, and the executable Ruby version of same, as generated via a rewrite grammar. Cells also store their dependencies.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Cell) initialize(context)

Cells are initialised with a binding to allow formulas to be executed within the context of the sheet which owns the cell.



14
15
16
17
18
19
# File 'lib/soroban/cell.rb', line 14

def initialize(context)
  @dependencies = []
  @binding = context
  @touched = false
  @value = nil
end

Instance Attribute Details

- (Object) dependencies (readonly)

Returns the value of attribute dependencies



10
11
12
# File 'lib/soroban/cell.rb', line 10

def dependencies
  @dependencies
end

- (Object) excel (readonly)

Returns the value of attribute excel



10
11
12
# File 'lib/soroban/cell.rb', line 10

def excel
  @excel
end

- (Object) javascript (readonly)

Returns the value of attribute javascript



10
11
12
# File 'lib/soroban/cell.rb', line 10

def javascript
  @javascript
end

Instance Method Details

- (Object) clear

Clear the cached value of a cell to force it to be recalculated



37
38
39
# File 'lib/soroban/cell.rb', line 37

def clear
  @value = nil
end

- (Object) get

Eval the Ruby version of the string contents within the context of the owning sheet. Will throw Soroban::RecursionError if recursion is detected.



43
44
45
46
47
48
49
50
51
# File 'lib/soroban/cell.rb', line 43

def get
  raise Soroban::RecursionError, "Loop detected when evaluating '#{@excel}'" if @touched
  @touched = true
  @value ||= eval(@ruby, @binding)
rescue TypeError, RangeError, ZeroDivisionError
  nil
ensure
  @touched = false
end

- (Object) set(contents)

Set the contents of a cell, and store the executable Ruby version.



26
27
28
29
30
31
32
33
34
# File 'lib/soroban/cell.rb', line 26

def set(contents)
  contents = contents.to_s
  contents = "'#{contents}'" if Soroban::unknown?(contents)
  clear
  @excel = contents
  @tree = Soroban::parser.parse(@excel)
  raise Soroban::ParseError, Soroban::parser.failure_reason if @tree.nil?
  @ruby = _to_ruby
end

- (Object) to_compiled_ruby



21
22
23
# File 'lib/soroban/cell.rb', line 21

def to_compiled_ruby
  @tree.to_compiled_ruby
end