lib/gm/notepad/table_entry.rb in gm-notepad-0.0.6 vs lib/gm/notepad/table_entry.rb in gm-notepad-0.0.8
- old
+ new
@@ -2,41 +2,53 @@
module Gm
module Notepad
TABLE_ENTRY_RANGE_MARKER = "-".freeze
class TableEntry
Configuration.init!(target: self, from_config: [:column_delimiter], additional_params: [:line]) do
- self.lookup_column, self.entry_column = line.split(column_delimiter)
+ row = line.split(column_delimiter)
+ self.index = row.shift
+ self.cells = row
end
include Comparable
def <=>(other)
to_str <=> String(other)
end
+ def lookup(cell:)
+ # TODO: Need to deal with named columns
+ cells[cell.to_i]
+ end
+
NUMBER_RANGE_REGEXP = %r{(?<left>\d+) *- *(?<right>\d+)}
def lookup_range
- if match = NUMBER_RANGE_REGEXP.match(lookup_column)
+ if match = NUMBER_RANGE_REGEXP.match(index)
(match[:left].to_i..match[:right].to_i).map(&:to_s)
else
- [lookup_column]
+ [index]
end
end
- attr_reader :lookup_column, :entry_column
+ attr_reader :index, :cells
+ def entry
+ cells.join("\t")
+ end
+ alias entry_column entry
+
def to_s
- "[#{lookup_column}]\t#{entry_column}"
+ "[#{index}]\t#{entry}"
end
- alias to_str entry_column
+ alias to_str entry
private
- def lookup_column=(input)
- @lookup_column = input.strip.downcase.freeze
+ def index=(input)
+ @index = input.strip.downcase.freeze
end
- def entry_column=(input)
- @entry_column = input.strip.freeze
+ def cells=(input)
+ @cells = Array(input).map { |i| i.strip.freeze }.freeze
end
end
end
end