Class: Spodunk::Table
- Inherits:
-
Object
- Object
- Spodunk::Table
- Defined in:
- lib/spodunk/table.rb
Instance Attribute Summary collapse
-
#col_offset ⇒ Object
readonly
Returns the value of attribute col_offset.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#row_offset ⇒ Object
readonly
Returns the value of attribute row_offset.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#slug_headers ⇒ Object
readonly
Returns the value of attribute slug_headers.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#changes(opts = {}) ⇒ Object
returns with 0-based index and column names as Strings.
- #clean? ⇒ Boolean
- #clean_rows ⇒ Object
- #dirty? ⇒ Boolean
- #dirty_rows ⇒ Object
-
#initialize(arr, opts = {}) ⇒ Table
constructor
A new instance of Table.
-
#itemized_changes ⇒ Object
similar to offset_changes, except hash contains keys of [row, val] Arrays.
- #num_cols ⇒ Object
- #num_rows ⇒ Object
-
#offset_changes ⇒ Object
returns format more specific for Google Worksheets with 1-based index and Hash with column indices rather than Strings.
- #real_row_index(row) ⇒ Object
-
#save ⇒ Object
this could be dangerous.
- #valid? ⇒ Boolean
Constructor Details
#initialize(arr, opts = {}) ⇒ Table
Returns a new instance of Table.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 |
Instance Attribute Details
#col_offset ⇒ Object (readonly)
Returns the value of attribute col_offset.
6 7 8 |
# File 'lib/spodunk/table.rb', line 6 def col_offset @col_offset end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/spodunk/table.rb', line 9 def connection @connection end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/spodunk/table.rb', line 6 def headers @headers end |
#row_offset ⇒ Object (readonly)
Returns the value of attribute row_offset.
6 7 8 |
# File 'lib/spodunk/table.rb', line 6 def row_offset @row_offset end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
6 7 8 |
# File 'lib/spodunk/table.rb', line 6 def rows @rows end |
#slug_headers ⇒ Object (readonly)
Returns the value of attribute slug_headers.
6 7 8 |
# File 'lib/spodunk/table.rb', line 6 def slug_headers @slug_headers end |
#title ⇒ Object (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
74 75 76 77 78 79 80 81 82 83 |
# 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? ⇒ Boolean
65 66 67 |
# File 'lib/spodunk/table.rb', line 65 def clean? dirty_rows.empty? end |
#clean_rows ⇒ Object
57 58 59 |
# File 'lib/spodunk/table.rb', line 57 def clean_rows @rows.reject{|r| r.dirty?} end |
#dirty? ⇒ Boolean
69 70 71 |
# File 'lib/spodunk/table.rb', line 69 def dirty? !clean? end |
#dirty_rows ⇒ Object
61 62 63 |
# File 'lib/spodunk/table.rb', line 61 def dirty_rows @rows.select{|r| r.dirty?} end |
#itemized_changes ⇒ Object
similar to offset_changes, except hash contains keys of [row, val] Arrays
94 95 96 97 98 99 100 101 102 |
# 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 ⇒ Object
48 49 50 |
# File 'lib/spodunk/table.rb', line 48 def num_cols @headers.count end |
#num_rows ⇒ Object
44 45 46 |
# File 'lib/spodunk/table.rb', line 44 def num_rows @rows.count end |
#offset_changes ⇒ Object
returns format more specific for Google Worksheets with 1-based index and Hash with column indices rather than
Strings
88 89 90 |
# File 'lib/spodunk/table.rb', line 88 def offset_changes changes(col_offset: @col_offset, row_offset: @row_offset) end |
#real_row_index(row) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# 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 ⇒ Object
this could be dangerous
107 108 109 |
# File 'lib/spodunk/table.rb', line 107 def save connection.save_table(self) end |
#valid? ⇒ Boolean
53 54 55 |
# File 'lib/spodunk/table.rb', line 53 def valid? @slug_headers.uniq.count == num_cols && @slug_headers.all?{|h| !h.empty? } end |