Class: TableFu::Row

Inherits:
Array
  • Object
show all
Defined in:
lib/table_fu.rb

Overview

TableFu::Row adds functionality to an row array in a TableFu instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, row_num, spreadsheet) ⇒ Row

Returns a new instance of Row.



187
188
189
190
191
# File 'lib/table_fu.rb', line 187

def initialize(row, row_num, spreadsheet)
  self.replace row
  @row_num = row_num
  @spreadsheet = spreadsheet
end

Instance Attribute Details

#row_numObject (readonly)

Returns the value of attribute row_num.



185
186
187
# File 'lib/table_fu.rb', line 185

def row_num
  @row_num
end

Instance Method Details

#<=>(b) ⇒ Object

Comparator for sorting a spreadsheet row.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/table_fu.rb', line 230

def <=>(b)
  if @spreadsheet.sorted_by
    column = @spreadsheet.sorted_by.keys.first
    order = @spreadsheet.sorted_by[column]["order"]
    format = @spreadsheet.sorted_by[column]["format"]
    a = column_for(column).value || ''
    b = b.column_for(column).value || ''
    if format
      a = TableFu::Formatting.send(format, a) || ''
      b = TableFu::Formatting.send(format, b) || ''
    end
    result = a <=> b
    result = -1 if result.nil?
    result = result * -1 if order == 'descending'
    result
  else
    -1
  end
end

#[](col) ⇒ Object

sugar



220
221
222
223
224
225
226
# File 'lib/table_fu.rb', line 220

def [](col)
  if col.is_a?(String)
    column_for(col)
  else
    super(col)
  end
end

#columnsObject



193
194
195
196
197
198
199
# File 'lib/table_fu.rb', line 193

def columns
  all_cols = []
  @spreadsheet.columns.each do |column|
    all_cols << datum_for(column)
  end
  all_cols
end

#datum_for(col_name) ⇒ Object Also known as: column_for

This returns a Datum object for a header name. Will return a nil Datum object for nonexistant column names

Parameters: header name

Returns: Datum object



210
211
212
213
214
215
216
# File 'lib/table_fu.rb', line 210

def datum_for(col_name)
  if col_num = @spreadsheet.column_headers.index(col_name)
    TableFu::Datum.new(self[col_num], col_name, self, @spreadsheet)
  else # Return a nil Datum object for non existant column names
    TableFu::Datum.new(nil, col_name, self, @spreadsheet)
  end
end