Class: Tablespoon::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tablespoon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_mapObject

Returns the value of attribute column_map.



36
37
38
# File 'lib/tablespoon.rb', line 36

def column_map
  @column_map
end

#docObject

Returns the value of attribute doc.



36
37
38
# File 'lib/tablespoon.rb', line 36

def doc
  @doc
end

#field_mapObject

Returns the value of attribute field_map.



36
37
38
# File 'lib/tablespoon.rb', line 36

def field_map
  @field_map
end

#id_fieldObject

Returns the value of attribute id_field.



36
37
38
# File 'lib/tablespoon.rb', line 36

def id_field
  @id_field
end

#include_blank_rowsObject

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

#wsObject

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_rowObject



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_mapObject



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

#dumpObject



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

#eachObject



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

#lastObject



89
90
91
# File 'lib/tablespoon.rb', line 89

def last
  return @rows.last
end

#lengthObject



85
86
87
# File 'lib/tablespoon.rb', line 85

def length
  return @rows.length
end

#saveObject



109
110
111
# File 'lib/tablespoon.rb', line 109

def save
  @ws.save
end

#to_jsonObject



132
133
134
# File 'lib/tablespoon.rb', line 132

def to_json
  JSON.dump( dump )
end