class Spodunk::Table

Attributes

col_offset[R]
connection[R]
headers[R]
row_offset[R]
rows[R]
slug_headers[R]
title[R]

Public Class Methods

new(arr, opts={}) click to toggle source
# File lib/spodunk/table.rb, line 11
def initialize(arr, opts={})
  @connection = opts[:connection]

  rs = arr.dup
  @headers = rs.shift
  @slug_headers = @headers.map{|h| h.slugify}
  # rows get attributes as slugs
  @rows = RowCollection.new(rs, @slug_headers, self)

  @title = opts[:title]

  # most spreadsheets start counting from 1
  # and the first row is technically at index-2
  #  since headers is index-1
  @row_offset = opts[:row_offset] || 2

  # most spreadsheets begin column counting at 1
  @col_offset = opts[:col_offset] || 1
end

Public Instance Methods

changes(opts={}) click to toggle source

returns with 0-based index and column names as Strings

# File lib/spodunk/table.rb, line 74
def changes(opts={})
  row_offset = opts[:row_offset].to_i

  dirty_rows.inject({}) do |h, row|
    idx = @rows.index(row) + row_offset
    h[idx] = row.changes(opts)
 
    h
  end     
end
clean?() click to toggle source
# File lib/spodunk/table.rb, line 65
def clean?
  dirty_rows.empty?
end
clean_rows() click to toggle source
# File lib/spodunk/table.rb, line 57
def clean_rows
  @rows.reject{|r| r.dirty?}
end
dirty?() click to toggle source
# File lib/spodunk/table.rb, line 69
def dirty?
  !clean?
end
dirty_rows() click to toggle source
# File lib/spodunk/table.rb, line 61
def dirty_rows
  @rows.select{|r| r.dirty?}
end
itemized_changes() click to toggle source

similar to offset_changes, except hash contains keys of [row, val] Arrays

# File lib/spodunk/table.rb, line 94
def itemized_changes
  offset_changes.inject({}) do |h, (row_num, col_hsh) |
    col_hsh.each_pair do |col_num, val|
      h[[row_num, col_num]] = val
    end
    
    h
  end
end
num_cols() click to toggle source
# File lib/spodunk/table.rb, line 48
def num_cols
  @headers.count
end
num_rows() click to toggle source
# File lib/spodunk/table.rb, line 44
def num_rows
  @rows.count
end
offset_changes() click to toggle source

returns format more specific for Google Worksheets with 1-based index and Hash with column indices rather than

Strings
# File lib/spodunk/table.rb, line 88
def offset_changes
  changes(col_offset: @col_offset, row_offset: @row_offset)
end
real_row_index(row) click to toggle source
# File lib/spodunk/table.rb, line 33
def real_row_index(row)
  if row.is_a?(Fixnum)
    idx = row
  else
    idx = @rows.index(row)
  end

  return idx + @row_offset
end
save() click to toggle source

this could be dangerous

# File lib/spodunk/table.rb, line 107
def save
  connection.save_table(self)
end
valid?() click to toggle source
# File lib/spodunk/table.rb, line 53
def valid?
  @slug_headers.uniq.count == num_cols && @slug_headers.all?{|h| !h.empty? }
end