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.



130
131
132
133
134
# File 'lib/charty/table.rb', line 130

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

Instance Method Details

#[](key) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/charty/table.rb', line 217

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



200
201
202
203
204
205
206
207
# File 'lib/charty/table.rb', line 200

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



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

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



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

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

#group_keysObject



192
193
194
# File 'lib/charty/table.rb', line 192

def group_keys
  @indices.keys
end

#indicesObject



188
189
190
# File 'lib/charty/table.rb', line 188

def indices
  @indices.dup
end