Class: Tablespoon::Table
- Inherits:
-
Object
- Object
- Tablespoon::Table
- Includes:
- Enumerable
- Defined in:
- lib/tablespoon.rb
Instance Attribute Summary collapse
-
#column_map ⇒ Object
Returns the value of attribute column_map.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#field_map ⇒ Object
Returns the value of attribute field_map.
-
#id_field ⇒ Object
Returns the value of attribute id_field.
-
#include_blank_rows ⇒ Object
Returns the value of attribute include_blank_rows.
-
#ws ⇒ Object
Returns the value of attribute ws.
Instance Method Summary collapse
- #[] ⇒ Object
- #add_row ⇒ Object
- #build_column_map ⇒ Object
- #dump ⇒ Object
- #each ⇒ Object
- #find(field, value) ⇒ Object
- #find_all(field, value) ⇒ Object
- #find_by_id(value) ⇒ Object
-
#initialize(ws, opts = {}) ⇒ Table
constructor
A new instance of Table.
- #last ⇒ Object
- #length ⇒ Object
- #save ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(ws, opts = {}) ⇒ Table
Returns a new instance of Table.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tablespoon.rb', line 38 def initialize( ws, opts = {} ) @ws = ws # handle some options @id_field = opts[:id_field] @include_blank_row = opts[:include_blank_rows] || true build_column_map # build data array @rows = [] for row in 2..@ws.num_rows r = Record.new self r.row_num = row data = {} for col in 1..@ws.num_cols data[ column_map[col] ] = @ws[ row, col ] if column_map[col] == @id_field r.id = @ws[ row, col ] end end r.data=data @rows << r end end |
Instance Attribute Details
#column_map ⇒ Object
Returns the value of attribute column_map.
36 37 38 |
# File 'lib/tablespoon.rb', line 36 def column_map @column_map end |
#doc ⇒ Object
Returns the value of attribute doc.
36 37 38 |
# File 'lib/tablespoon.rb', line 36 def doc @doc end |
#field_map ⇒ Object
Returns the value of attribute field_map.
36 37 38 |
# File 'lib/tablespoon.rb', line 36 def field_map @field_map end |
#id_field ⇒ Object
Returns the value of attribute id_field.
36 37 38 |
# File 'lib/tablespoon.rb', line 36 def id_field @id_field end |
#include_blank_rows ⇒ Object
Returns the value of attribute include_blank_rows.
36 37 38 |
# File 'lib/tablespoon.rb', line 36 def include_blank_rows @include_blank_rows end |
#ws ⇒ Object
Returns the value of attribute ws.
36 37 38 |
# File 'lib/tablespoon.rb', line 36 def ws @ws end |
Instance Method Details
#[] ⇒ Object
73 74 75 |
# File 'lib/tablespoon.rb', line 73 def [] return @rows[i] end |
#add_row ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/tablespoon.rb', line 77 def add_row r = Record.new self r.row_num = @ws.num_rows + 1 r.data = {} r end |
#build_column_map ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/tablespoon.rb', line 113 def build_column_map @column_map = {} for col in 1..@ws.num_cols @column_map[ col ] = @ws[ 1,col ] end @field_map = column_map.invert end |
#dump ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/tablespoon.rb', line 122 def dump output = [] @rows.each do |r| output << r.data end output end |
#each ⇒ Object
93 94 95 |
# File 'lib/tablespoon.rb', line 93 def each @rows.each { |i| yield i } end |
#find(field, value) ⇒ Object
97 98 99 |
# File 'lib/tablespoon.rb', line 97 def find( field, value ) @rows.find { |r| r[field] == value } end |
#find_all(field, value) ⇒ Object
105 106 107 |
# File 'lib/tablespoon.rb', line 105 def find_all( field, value ) @rows.select { |r| r[field] == value } end |
#find_by_id(value) ⇒ Object
101 102 103 |
# File 'lib/tablespoon.rb', line 101 def find_by_id( value ) @rows.find { |r| r.id == value } end |
#last ⇒ Object
89 90 91 |
# File 'lib/tablespoon.rb', line 89 def last return @rows.last end |
#length ⇒ Object
85 86 87 |
# File 'lib/tablespoon.rb', line 85 def length return @rows.length end |
#save ⇒ Object
109 110 111 |
# File 'lib/tablespoon.rb', line 109 def save @ws.save end |
#to_json ⇒ Object
132 133 134 |
# File 'lib/tablespoon.rb', line 132 def to_json JSON.dump( dump ) end |