Class: Gm::Notepad::TableEntry

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Includes:
Comparable
Defined in:
lib/gm/notepad/table_entry.rb

Constant Summary collapse

NUMBER_RANGE_REGEXP =
%r{(?<left>\d+) *- *(?<right>\d+)}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TableEntry

Returns a new instance of TableEntry.



13
14
15
16
17
18
# File 'lib/gm/notepad/table_entry.rb', line 13

def initialize(*args)
  super
  row = line.split(column_delimiter)
  self.index = row.shift
  self.cells = row
end

Instance Attribute Details

#cellsObject

Returns the value of attribute cells.



39
40
41
# File 'lib/gm/notepad/table_entry.rb', line 39

def cells
  @cells
end

#indexObject

Returns the value of attribute index.



39
40
41
# File 'lib/gm/notepad/table_entry.rb', line 39

def index
  @index
end

Instance Method Details

#<=>(other) ⇒ Object



21
22
23
# File 'lib/gm/notepad/table_entry.rb', line 21

def <=>(other)
  to_str <=> String(other)
end

#entryObject Also known as: entry_column, to_str



41
42
43
# File 'lib/gm/notepad/table_entry.rb', line 41

def entry
  cells.join("\t")
end

#lookup(cell:) ⇒ Object



25
26
27
28
# File 'lib/gm/notepad/table_entry.rb', line 25

def lookup(cell:)
  index = table.column_index_for(cell: cell)
  cells[index] || cells[0]
end

#lookup_rangeObject



31
32
33
34
35
36
37
# File 'lib/gm/notepad/table_entry.rb', line 31

def lookup_range
  if match = NUMBER_RANGE_REGEXP.match(index)
    (match[:left].to_i..match[:right].to_i).map(&:to_s)
  else
    [index]
  end
end

#to_sObject



46
47
48
# File 'lib/gm/notepad/table_entry.rb', line 46

def to_s
  "[#{index}]\t#{entry}"
end