Class: TableTransform::Table

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

Defined Under Namespace

Classes: Cell, ColumnProperties, Row, TableProperties

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, table_properties = {}) ⇒ Table

Returns a new instance of Table.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/table_transform/table.rb', line 31

def initialize(rows, table_properties = {})
  raise 'Table required to have at least a header row' if (rows.nil? or rows.empty?)

  @data_rows = rows.clone
  header = @data_rows.shift
  @column_indexes = create_column_name_binding(header)
  @formulas = {}
  @table_properties = TableProperties.new(table_properties)
  @column_properties = Hash.new{|_hash, key| raise "No column with name '#{key}' exists"}
  create_column_properties(*header,{})

  validate_header_uniqueness(header)
  validate_column_size
end

Instance Attribute Details

#column_propertiesObject (readonly)

Returns the value of attribute column_properties.



14
15
16
# File 'lib/table_transform/table.rb', line 14

def column_properties
  @column_properties
end

#formulasObject

Returns the value of attribute formulas.



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

def formulas
  @formulas
end

#table_propertiesObject (readonly)

Returns the value of attribute table_properties.



13
14
15
# File 'lib/table_transform/table.rb', line 13

def table_properties
  @table_properties
end

Class Method Details

.create_empty(header, table_properties = {}) ⇒ Object



23
24
25
26
27
# File 'lib/table_transform/table.rb', line 23

def self.create_empty(header, table_properties = {})
  raise 'Table header need to be array' unless header.is_a? Array
  raise 'Table, No header defined' if header.empty?
  Table.new([header], table_properties)
end

.create_from_file(file_name, sep = ',') ⇒ Object



16
17
18
19
20
21
# File 'lib/table_transform/table.rb', line 16

def self.create_from_file(file_name, sep = ',')
  rows = CSV.read(file_name, { :col_sep => sep })
  raise "'#{file_name}' contains no data" if rows.empty?

  Table.new(rows)
end

Instance Method Details

#+(table) ⇒ Object

Add two tables



73
74
75
76
77
78
79
80
# File 'lib/table_transform/table.rb', line 73

def +(table)
  t2 = table.to_a
  t2_header = t2.shift
  raise 'Tables cannot be added due to header mismatch' unless @column_properties.keys == t2_header
  raise 'Tables cannot be added due to column properties mismatch' unless column_properties_eql? table.column_properties
  raise 'Tables cannot be added due to table properties mismatch' unless @table_properties.to_h == table.table_properties.to_h
  TableTransform::Table.new(self.to_a + t2)
end

#<<(hash_values) ⇒ Object



66
67
68
69
# File 'lib/table_transform/table.rb', line 66

def << (hash_values)
  @data_rows << create_row(hash_values)
  self
end

#add_column(name, column_properties = {}) ⇒ Object

adds a column with given name to the far right of the table



113
114
115
116
117
118
119
120
121
# File 'lib/table_transform/table.rb', line 113

def add_column(name, column_properties = {})
  validate_column_absence(name)
  create_column_properties(name, column_properties)
  @data_rows.each{|x|
    x << (yield Row.new(@column_indexes, x))
  }
  @column_indexes[name] = @column_indexes.size
  self # enable chaining
end

#add_column_formula(column, formula, column_properties = {}) ⇒ Object



60
61
62
63
64
# File 'lib/table_transform/table.rb', line 60

def add_column_formula(column, formula, column_properties = {})
  add_column(column, column_properties){nil}
  @formulas[column] = formula
  self # self chaining
end

#change_column(name) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/table_transform/table.rb', line 123

def change_column(name)
  raise "Column with formula('#{name}') cannot be changed" if @formulas[name]
  index = Util::get_col_index(name, @column_indexes)
  @data_rows.each{|r|
    r[index] = yield Row.new(@column_indexes, r)
  }

  self # enable chaining
end

#delete_column(*names) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/table_transform/table.rb', line 133

def delete_column(*names)
  validate_column_exist(*names)
  names.each{|n|
    @column_properties.delete(n)
    @formulas.delete(n)
  }

  selected_cols = @column_indexes.values_at(*@column_properties.keys)
  @data_rows.map!{|row| row.values_at(*selected_cols)}

  @column_indexes = create_column_name_binding(@column_properties.keys)
  self
end

#each_rowObject



82
83
84
85
86
# File 'lib/table_transform/table.rb', line 82

def each_row
  @data_rows.each{|x|
    yield Row.new(@column_indexes, x)
  }
end

#extract(header) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/table_transform/table.rb', line 95

def extract(header)
  validate_column_exist(*header)
  selected_cols = @column_indexes.values_at(*header)
  t = Table.new( @data_rows.inject([header]) {|res, row| (res << row.values_at(*selected_cols))}, @table_properties.to_h )
  header.each{|h| t.column_properties[h].reset(@column_properties[h].to_h)}
  t.formulas = header.zip(@formulas.values_at(*header)).to_h
  t
end

#filterObject



105
106
107
108
109
# File 'lib/table_transform/table.rb', line 105

def filter
  t = Table.new( (@data_rows.select {|row| yield Row.new(@column_indexes, row)}.unshift @column_properties.keys.clone), @table_properties.to_h )
  t.formulas = @formulas.clone
  t
end

#metadataObject

Returns meta data as Hash with header name as key



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

def 
  warn 'metadata is deprecated. Use column_properties[] instead'
  @column_properties.inject({}){|res, (k, v)| res.merge!({k => v.to_h})}
end

#rename_column(from, to) ⇒ Object



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

def rename_column(from, to)
  validate_column_exist(from)
  validate_column_absence(to)

  @column_properties = @column_properties.map{|k,v| [k == from ? to : k, v] }.to_h
  @formulas = @formulas.map{|k,v| [k == from ? to : k, v] }.to_h
  @column_indexes = create_column_name_binding(@column_properties.keys)
end

#set_metadata(*columns, metadata) ⇒ Object

Sets metadata for given columns Example:

('Col1', {format: '#,##0'})


49
50
51
52
# File 'lib/table_transform/table.rb', line 49

def (*columns, )
  warn 'set_metadata is deprecated. Use column_properties[] instead'
  columns.each{|c| @column_properties[c].reset()}
end

#to_aObject



89
90
91
92
# File 'lib/table_transform/table.rb', line 89

def to_a
  res = @data_rows.clone
  res.unshift @column_properties.keys.clone
end