Class: Charty::Table::HashGroupBy

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

Instance Method Summary collapse

Constructor Details

#initialize(table, grouper, sort, drop_na) ⇒ HashGroupBy

Returns a new instance of HashGroupBy.


137
138
139
140
141
# File 'lib/charty/table.rb', line 137

def initialize(table, grouper, sort, drop_na)
  @table = table
  @grouper = check_grouper(grouper)
  init_groups(sort, drop_na)
end

Instance Method Details

#[](key) ⇒ Object


224
225
226
227
228
229
230
231
232
233
234
# File 'lib/charty/table.rb', line 224

def [](key)
  return nil unless @indices.key?(key)

  index = @indices[key]
  Charty::Table.new(
    @table.column_names.map {|col|
      [col, @table[col].values_at(*index)]
    }.to_h,
    index: index
  )
end

#apply(*args, &block) ⇒ Object


207
208
209
210
211
212
213
214
# File 'lib/charty/table.rb', line 207

def apply(*args, &block)
  Charty::Table.new(
    each_group.map { |_key, table|
      block.call(table, *args)
    },
    index: Charty::Index.new(@indices.keys, name: @grouper)
  )
end

#each_groupObject


216
217
218
219
220
221
222
# File 'lib/charty/table.rb', line 216

def each_group
  return enum_for(__method__) unless block_given?

  @indices.each_key do |key|
    yield(key, self[key])
  end
end

#each_group_key(&block) ⇒ Object


203
204
205
# File 'lib/charty/table.rb', line 203

def each_group_key(&block)
  @indices.each_key(&block)
end

#group_keysObject


199
200
201
# File 'lib/charty/table.rb', line 199

def group_keys
  @indices.keys
end

#indicesObject


195
196
197
# File 'lib/charty/table.rb', line 195

def indices
  @indices.dup
end