Class: SimpleTables::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, col_names) ⇒ Table

Returns a new instance of Table.



5
6
7
# File 'lib/simple_tables/table.rb', line 5

def initialize(data,col_names)
  @data=data.map{|r| Record.new(r,col_names)}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/simple_tables/table.rb', line 3

def data
  @data
end

Instance Method Details

#pivot(rows_col, cols_col, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/simple_tables/table.rb', line 13

def pivot(rows_col,cols_col,&block)
  result=@data.group_by{|r| [r[cols_col],r[rows_col]]}

  if block_given?
    return PivotTable[result.map{|k,v| [k,block.call(v)] }]
  end

  PivotTable[result.map{|k,v| [k,v.count]}]
end

#where(conditions) ⇒ Object



9
10
11
# File 'lib/simple_tables/table.rb', line 9

def where(conditions)
  @data.select { |r| r.matches?(conditions) }
end