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



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

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

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

#[](key) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/charty/table.rb', line 54

def [](key)
  case key
  when Array
    @adapter[nil, key]
  else
    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
end

#[]=(key, values) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/charty/table.rb', line 73

def []=(key, values)
  case key
  when Array
    raise ArgumentError,
          "Substituting multiple keys is not supported"
  when Symbol
    # do nothing
  else
    key = key.to_str.to_sym
  end
  @adapter[key] = values
end

#drop_naObject



123
124
125
# File 'lib/charty/table.rb', line 123

def drop_na
  @adapter.drop_na || self
end

#eachObject



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

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)


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

def empty?
  length == 0
end

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



86
87
88
# File 'lib/charty/table.rb', line 86

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/charty/table.rb', line 90

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