Class: Soroban::Helpers

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

Class Method Summary collapse

Class Method Details

.boolean?(data) ⇒ Boolean

Return true if the supplied data is a boolean.

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/soroban/helpers.rb', line 18

def self.boolean?(data)
  /^(true|false)$/i.match(data.to_s) && true ||
  false
end

.formula?(data) ⇒ Boolean

Return true if the supplied data represents a formula.

Returns:

  • (Boolean)


8
9
10
# File 'lib/soroban/helpers.rb', line 8

def self.formula?(data)
  data.to_s.slice(0..0) == '='
end

.getPos(data) ⇒ Object

Return the row and column index of the given label. This converts something like “B42” into [41, 1]. It is a known bug that it does not work for labels of the form “BC42”. Will raise a ParseError if the supplied argument is not a valid label.



64
65
66
67
68
# File 'lib/soroban/helpers.rb', line 64

def self.getPos(data)
  raise Soroban::ParseError, "invalid #getPos for '#{data}'" if !label?(data)
  match = /^([a-zA-Z]+)([\d]+)$/.match(data.to_s)
  return [match[2].to_i - 1, match[1].upcase[0].ord-"A"[0].ord]
end

.getRange(data) ⇒ Object

Return the components of a range. This converts something like “A12:C42” to a tuple of the form [“A”, “12”, “C”, “42”]. Will raise a ParseError if the supplied argument is not a valid range.



55
56
57
58
# File 'lib/soroban/helpers.rb', line 55

def self.getRange(data)
  raise Soroban::ParseError, "invalid #getRange for '#{data}'" if !range?(data)
  /^([a-zA-Z]+)([\d]+):([a-zA-Z]+)([\d]+)$/.match(data.to_s).to_a[1..-1]
end

.getValues(context, *args) ⇒ Object

Return an array of values for the supplied arguments (which may be numbers, labels and ranges).



71
72
73
# File 'lib/soroban/helpers.rb', line 71

def self.getValues(context, *args)
  args.map { |arg| range?(arg) ? Soroban::ValueWalker.new(arg, context).to_a : arg }.to_a.flatten
end

.label?(data) ⇒ Boolean

Return true if the supplied data is a label.

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/soroban/helpers.rb', line 31

def self.label?(data)
  /^([a-zA-Z]+)([\d]+)$/.match(data.to_s) && true ||
  false
end

.number?(data) ⇒ Boolean

Return true if the supplied data is a number.

Returns:

  • (Boolean)


13
14
15
# File 'lib/soroban/helpers.rb', line 13

def self.number?(data)
  Float(data.to_s) && true rescue false
end

.range?(data) ⇒ Boolean

Return true if the supplied data is a range.

Returns:

  • (Boolean)


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

def self.range?(data)
  /^([a-zA-Z]+)([\d]+):([a-zA-Z]+)([\d]+)$/.match(data.to_s) && true ||
  false
end

.string?(data) ⇒ Boolean

Return true if the supplied data is a string.

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/soroban/helpers.rb', line 24

def self.string?(data)
  /^["](\"|[^"])*["]$/.match(data.to_s) && true ||
  /^['][^']*[']$/.match(data.to_s) && true ||
  false
end

.unknown?(data) ⇒ Boolean

Return true if the supplied data is of no recognised format.

Returns:

  • (Boolean)


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

def self.unknown?(data)
  !formula?(data) &&
  !number?(data) &&
  !boolean?(data) &&
  !string?(data) &&
  !label?(data) &&
  !range?(data)
end