class Results::Row

Used by ResultsFile. Intermediate Hash-like model between Excel row and Result.

Attributes

column_indexes[R]
next[RW]
previous[RW]
result[RW]
spreadsheet_row[R]
usac_results_format[R]

Public Class Methods

new(spreadsheet_row, column_indexes, usac_results_format) click to toggle source
# File lib/results/row.rb, line 11
def initialize(spreadsheet_row, column_indexes, usac_results_format)
  @spreadsheet_row = spreadsheet_row
  @column_indexes = column_indexes
  @usac_results_format = usac_results_format
end

Public Instance Methods

[](column_symbol) click to toggle source
# File lib/results/row.rb, line 17
def [](column_symbol)
  index = column_indexes[column_symbol]
  if index
    case spreadsheet_row[index]
    when ::Spreadsheet::Formula
      value = spreadsheet_row[index].value
    when ::Spreadsheet::Excel::Error
      value = nil
    else
      value = spreadsheet_row[index]
    end
    value.strip! if value.respond_to?(:strip!)
    value
  end
end
blank?() click to toggle source
# File lib/results/row.rb, line 39
def blank?
  spreadsheet_row.all? { |cell| cell.to_s.blank? }
end
first() click to toggle source
# File lib/results/row.rb, line 43
def first
  spreadsheet_row[0]
end
first?() click to toggle source
# File lib/results/row.rb, line 47
def first?
  spreadsheet_row.idx == 0
end
last?() click to toggle source
# File lib/results/row.rb, line 51
def last?
  spreadsheet_row == spreadsheet_row.worksheet.last_row
end
notes() click to toggle source
# File lib/results/row.rb, line 80
def notes
  if usac_results_format
    # We want to pick up the info in the first 5 columns: org, year, event #, date, discipline
    return "" if blank? || size < 5
    spreadsheet_row[0, 5].select { |cell| cell.present? }.join(", ")
  else  
    return "" if blank? || size < 2
    spreadsheet_row[1, size - 1].select { |cell| cell.present? }.join(", ")
  end
end
place() click to toggle source
# File lib/results/row.rb, line 59
def place
  if column_indexes[:place]
    value = self[:place]
  else
    value = spreadsheet_row[0]
    value = spreadsheet_row[0].value if spreadsheet_row[0].is_a?(::Spreadsheet::Formula)
    value.strip! if value.respond_to?(:strip!)
  end

  # Mainly to handle Dates and DateTimes in the place column
  value = nil unless value.respond_to?(:to_i)
  value
end
same_time?() click to toggle source
# File lib/results/row.rb, line 73
def same_time?
  if previous && self[:time].present?
    row_time = self[:time].try(:to_s)
    row_time && (row_time[%rst/] || row_time[%rs\.t\./])
  end
end
size() click to toggle source
# File lib/results/row.rb, line 55
def size
  spreadsheet_row.size
end
to_hash() click to toggle source
# File lib/results/row.rb, line 33
def to_hash
  hash = Hash.new
  column_indexes.keys.each { |key| hash[key] = self[key] }
  hash
end
to_s() click to toggle source
# File lib/results/row.rb, line 91
def to_s
  "#<Results::ResultsFile::Row #{spreadsheet_row.to_a.join(', ')} >"
end