Class: Charty::Table::HashGroupBy
- Inherits:
-
GroupByBase
- Object
- GroupByBase
- Charty::Table::HashGroupBy
- Defined in:
- lib/charty/table.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #apply(*args, &block) ⇒ Object
- #each_group ⇒ Object
- #each_group_key(&block) ⇒ Object
- #group_keys ⇒ Object
- #indices ⇒ Object
-
#initialize(table, grouper, sort, drop_na) ⇒ HashGroupBy
constructor
A new instance of HashGroupBy.
Constructor Details
#initialize(table, grouper, sort, drop_na) ⇒ 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_group ⇒ Object
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_keys ⇒ Object
192 193 194 |
# File 'lib/charty/table.rb', line 192 def group_keys @indices.keys end |
#indices ⇒ Object
188 189 190 |
# File 'lib/charty/table.rb', line 188 def indices @indices.dup end |