class Tablespoon::Table

Attributes

column_map[RW]
doc[RW]
field_map[RW]
id_field[RW]
include_blank_rows[RW]
ws[RW]

Public Class Methods

new( ws, opts = {} ) click to toggle source
# 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

Public Instance Methods

[]() click to toggle source
# File lib/tablespoon.rb, line 73
def []
  return @rows[i]
end
add_row() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/tablespoon.rb, line 122
def dump
  output = []

  @rows.each do |r|
    output << r.data
  end

  output 
end
each() { |i| ... } click to toggle source
# File lib/tablespoon.rb, line 93
def each
  @rows.each { |i| yield i }
end
find( field, value ) click to toggle source
# File lib/tablespoon.rb, line 97
def find( field, value )
  @rows.find { |r| r[field] == value }
end
find_all( field, value ) click to toggle source
# File lib/tablespoon.rb, line 105
def find_all( field, value )
  @rows.select { |r| r[field] == value }
end
find_by_id( value ) click to toggle source
# File lib/tablespoon.rb, line 101
def find_by_id( value )
  @rows.find { |r| r.id == value }
end
last() click to toggle source
# File lib/tablespoon.rb, line 89
def last
  return @rows.last
end
length() click to toggle source
# File lib/tablespoon.rb, line 85
def length
  return @rows.length
end
save() click to toggle source
# File lib/tablespoon.rb, line 109
def save
  @ws.save
end
to_json() click to toggle source
# File lib/tablespoon.rb, line 132
def to_json
  JSON.dump( dump )
end