Class: Charty::Table

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

Defined Under Namespace

Classes: GroupByBase, HashGroupBy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, **kwargs) ⇒ Table

Returns a new instance of Table.



17
18
19
20
21
22
23
24
25
26
# File 'lib/charty/table.rb', line 17

def initialize(data, **kwargs)
  adapter_class = TableAdapters.find_adapter_class(data)
  if kwargs.empty?
    @adapter = adapter_class.new(data)
  else
    @adapter = adapter_class.new(data, **kwargs)
  end

  @column_cache = {}
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



28
29
30
# File 'lib/charty/table.rb', line 28

def adapter
  @adapter
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/charty/table.rb', line 52

def ==(other)
  return true if equal?(other)

  case other
  when Charty::Table
    adapter == other.adapter
  else
    super
  end
end

#[](key) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/charty/table.rb', line 67

def [](key)
  key = case key
        when Symbol
          key
        else
          String.try_convert(key).to_sym
        end
  if @column_cache.key?(key)
    @column_cache[key]
  else
    @column_cache[key] = @adapter[nil, key]
  end
end

#column?(name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/charty/table.rb', line 37

def column?(name)
  return true if column_names.include?(name)

  case name
  when String
    column_names.include?(name.to_sym)
  when Symbol
    column_names.include?(name.to_s)
  else
    false
  end
end

#drop_naObject



118
119
120
# File 'lib/charty/table.rb', line 118

def drop_na
  @adapter.drop_na || self
end

#eachObject



108
109
110
111
112
113
114
115
116
# File 'lib/charty/table.rb', line 108

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

#empty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/charty/table.rb', line 63

def empty?
  length == 0
end

#group_by(grouper, sort: true, drop_na: true) ⇒ Object



81
82
83
# File 'lib/charty/table.rb', line 81

def group_by(grouper, sort: true, drop_na: true)
  adapter.group_by(self, grouper, sort, drop_na)
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/charty/table.rb', line 85

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
  when table.kind_of?(Array)
    table
  else
    raise ArgumentError, "unsupported object: #{table.inspect}"
  end
end