Class: Spodunk::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/spodunk/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr, opts = {}) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spodunk/table.rb', line 9

def initialize(arr, opts={})
  rs = arr.dup
  @headers = rs.shift
  @slug_headers = @headers.map{|h| h.slugify}
  # rows get attributes as slugs
  @rows = rs.map{ |r| Row.new(r, @slug_headers)}

  @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

Instance Attribute Details

#col_offsetObject (readonly)

Returns the value of attribute col_offset.



6
7
8
# File 'lib/spodunk/table.rb', line 6

def col_offset
  @col_offset
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/spodunk/table.rb', line 6

def headers
  @headers
end

#row_offsetObject (readonly)

Returns the value of attribute row_offset.



6
7
8
# File 'lib/spodunk/table.rb', line 6

def row_offset
  @row_offset
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/spodunk/table.rb', line 6

def rows
  @rows
end

#slug_headersObject (readonly)

Returns the value of attribute slug_headers.



6
7
8
# File 'lib/spodunk/table.rb', line 6

def slug_headers
  @slug_headers
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'lib/spodunk/table.rb', line 6

def title
  @title
end

Instance Method Details

#changes(opts = {}) ⇒ Object

returns with 0-based index and column names as Strings



68
69
70
71
72
73
74
75
76
77
# File 'lib/spodunk/table.rb', line 68

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?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/spodunk/table.rb', line 59

def clean?
  dirty_rows.empty?
end

#clean_rowsObject



51
52
53
# File 'lib/spodunk/table.rb', line 51

def clean_rows
  @rows.reject{|r| r.dirty?}
end

#dirty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/spodunk/table.rb', line 63

def dirty?
  !clean?
end

#dirty_rowsObject



55
56
57
# File 'lib/spodunk/table.rb', line 55

def dirty_rows
  @rows.select{|r| r.dirty?}
end

#itemized_changesObject

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



88
89
90
91
92
93
94
95
96
# File 'lib/spodunk/table.rb', line 88

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_colsObject



42
43
44
# File 'lib/spodunk/table.rb', line 42

def num_cols
  @headers.count
end

#num_rowsObject



38
39
40
# File 'lib/spodunk/table.rb', line 38

def num_rows
  @rows.count
end

#offset_changesObject

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

Strings


82
83
84
# File 'lib/spodunk/table.rb', line 82

def offset_changes
  changes(col_offset: @col_offset, row_offset: @row_offset)
end

#real_row_index(row) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/spodunk/table.rb', line 27

def real_row_index(row)
  if row.is_a?(Fixnum)
    idx = row
  else
    idx = @rows.index(row)
  end

  return idx + @row_offset
end

#valid?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/spodunk/table.rb', line 47

def valid?
  @slug_headers.uniq.count == num_cols
end