Class: Charty::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Table


4
5
6
# File 'lib/charty/table.rb', line 4

def initialize(table)
  @table = table
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table


8
9
10
# File 'lib/charty/table.rb', line 8

def table
  @table
end

Instance Method Details

#eachObject


31
32
33
34
35
36
37
38
39
# File 'lib/charty/table.rb', line 31

def each
  return to_enum(__method__) unless block_given?
  data = to_a
  i, n = 0, data.size
  while i < n
    yield data[i]
    i += 1
  end
end

#to_a(x = nil, y = nil, z = nil) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/charty/table.rb', line 10

def to_a(x=nil, y=nil, z=nil)
  case
  when defined?(Daru::DataFrame) && table.kind_of?(Daru::DataFrame)
    table.map(&:to_a)
  when defined?(Numo::NArray) && table.kind_of?(Numo::NArray)
    table.to_a
  when defined?(NMatrix) && table.kind_of?(NMatrix)
    table.to_a
  when defined?(ActiveRecord::Relation) && table.kind_of?(ActiveRecord::Relation)
    if z && x && y
      [table.pluck(x), table.pluck(y), table.pluck(z)]
    elsif x && y
      [table.pluck(x), table.pluck(y)]
    else
      raise ArgumentError, "column_names is required to convert to_a from ActiveRecord::Relation"
    end
  else
    raise ArgumentError, "unsupported object: #{table.inspect}"
  end
end