Class: Tables::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table = nil, name = nil) ⇒ Table

Returns a new instance of Table.



14
15
16
17
18
19
# File 'lib/tables/table.rb', line 14

def initialize(table=nil,name=nil)
  @table=[]
  table.each {|row| @table<<row } unless table.nil?
  build_column_index unless table.nil?
  self.name=name unless name.nil?
end

Instance Attribute Details

#colindexObject (readonly)

Returns the value of attribute colindex.



11
12
13
# File 'lib/tables/table.rb', line 11

def colindex
  @colindex
end

#idcolumnObject

Returns the value of attribute idcolumn.



11
12
13
# File 'lib/tables/table.rb', line 11

def idcolumn
  @idcolumn
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/tables/table.rb', line 12

def name
  @name
end

#rowindexObject (readonly)

Returns the value of attribute rowindex.



11
12
13
# File 'lib/tables/table.rb', line 11

def rowindex
  @rowindex
end

#tableObject (readonly)

Returns the value of attribute table.



11
12
13
# File 'lib/tables/table.rb', line 11

def table
  @table
end

Instance Method Details

#<<(row) ⇒ Object

<< is a synonym of add_row



95
96
97
# File 'lib/tables/table.rb', line 95

def <<(row)
  self.add_row(row)
end

#==(other) ⇒ Object



60
61
62
# File 'lib/tables/table.rb', line 60

def ==(other)
  self.table==other.table
end

#[](idx) ⇒ Object



56
57
58
# File 'lib/tables/table.rb', line 56

def [](idx)
  @table[idx]
end

#add_row(row) ⇒ Object



50
51
52
53
54
# File 'lib/tables/table.rb', line 50

def add_row(row)
  add_row_array(row) if row.is_a?(Array)
  add_row_hash(row) if (row.is_a?(Hash) and self.row_count>0)
  add_first_row_hash(row) if (row.is_a?(Hash) and self.row_count==0)
end

#column_copy(other_table) ⇒ Object



171
172
173
174
175
# File 'lib/tables/table.rb', line 171

def column_copy(other_table)
  other_table.get_each_row do |other_row|
    self.add_row(other_row)
  end
end

#column_countObject



70
71
72
# File 'lib/tables/table.rb', line 70

def column_count
  self.header.count
end

#delete_column(name) ⇒ Object



156
157
158
159
160
161
# File 'lib/tables/table.rb', line 156

def delete_column(name)
  colnum=@colindex[name]
  raise "Unknown column '#{name}'" if colnum.nil?
  self.each_row {|row| row.delete_at(colnum)}
  @colindex.delete(name)
end

#demerge!(colnum = 0) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/tables/table.rb', line 139

def demerge!(colnum=0)
  new_table=[@table[0]]
  (1..@table.count-1).each do |idx|
    demerge_it(colnum,idx, new_table)
  end
  @table=new_table
  return self
end

#each_rowObject



99
100
101
# File 'lib/tables/table.rb', line 99

def each_row
  @table.each {|row| yield(row)}
end

#each_row_with_indexObject



103
104
105
# File 'lib/tables/table.rb', line 103

def each_row_with_index
  @table.each_with_index {|row,idx| yield(row,idx) }
end

#get_column(name) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/tables/table.rb', line 163

def get_column(name)
  colnum=@colindex[name]
  raise "Unknown column '#{name}'" if colnum.nil?
  result=[]
  self.each_row {|row| result<<row[colnum]}
  result
end

#get_each_row(skip_header = true) ⇒ Object



107
108
109
# File 'lib/tables/table.rb', line 107

def get_each_row(skip_header=true)
  @table.each_with_index {|row,idx| yield(self.get_row(idx)) unless (idx==0 and skip_header)}
end

#get_row(arg) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/tables/table.rb', line 41

def get_row(arg)
  result=nil
  if arg.is_a? Integer then
    result=get_row_by_num(arg)
  else
    result=get_row_by_num(@rowindex[arg])
  end
end

#get_value(column_name, row_num) ⇒ Object



35
36
37
38
39
# File 'lib/tables/table.rb', line 35

def get_value(column_name,row_num)
  col_num=@colindex[column_name]
  raise "Unknown column name '#{column_name}'" if col_num.nil?
  @table[row_num][col_num]
end

#headerObject



21
22
23
# File 'lib/tables/table.rb', line 21

def header
  @table[0]
end

#merge!(t2) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tables/table.rb', line 82

def merge!(t2)
  raise "tables are not similar" unless similar?(t2)
  (1..t2.row_count-1).each do |idx|
    begin
      self<<t2[idx]
    rescue Exception=>e
      puts "ERROR: Error adding row #{idx} from '#{t2.name}' to '#{self.name}'"
      puts "ERROR: #{e.message}"
    end
  end
end

#remove_blank_rows!(startcol = 0) ⇒ Object



120
121
122
# File 'lib/tables/table.rb', line 120

def remove_blank_rows!(startcol=0)
  remove_matched_rows! { |row| row[startcol..-1].join.strip=="" }
end

#remove_matched_rows!Object



129
130
131
132
133
134
135
136
137
# File 'lib/tables/table.rb', line 129

def remove_matched_rows!
  blanks=[]
  @table.each do |row|
    blanks << row if yield(row)
  end
  blanks.each {|br| @table.delete_if {|el| el.equal?(br) }} # delete if objects are the same
  build_column_index
  return self
end

#remove_repeat_headers!Object



124
125
126
127
# File 'lib/tables/table.rb', line 124

def remove_repeat_headers!
  header=self.header
  remove_matched_rows! { |row| row.eql?(header) and not(row.equal?(header)) }
end

#rename_column(old_name, new_name) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/tables/table.rb', line 148

def rename_column(old_name, new_name)
  colnum=@colindex[old_name]
  raise "Unknown column '#{name}'" if colnum.nil?
  @colindex[new_name]=colnum
  @colindex.delete(old_name)
  @table[0][colnum]=new_name
end

#row_countObject



74
75
76
# File 'lib/tables/table.rb', line 74

def row_count
  @table.count
end

#rowsObject

obsolescent



65
66
67
68
# File 'lib/tables/table.rb', line 65

def rows
  puts "WARNING: use 'row_count' instead of 'rows'"
  self.row_count
end

#set_row(row) ⇒ Object

TODO: How can we do this within the get_row closure?



112
113
114
115
116
117
118
# File 'lib/tables/table.rb', line 112

def set_row(row)
  idx=row[:rownum]
  row.each_pair do |colname,value|
    jdx=@colindex[colname]
    @table[idx][jdx]=value unless jdx.nil?
  end
end

#signatureObject



177
178
179
# File 'lib/tables/table.rb', line 177

def signature
  self.header.join(',')
end

#similar?(t2) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/tables/table.rb', line 78

def similar?(t2)
  self.header==t2.header
end