Class: ObjectTable::Grouping::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/object_table/grouping/grid.rb

Defined Under Namespace

Modules: RowFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys, defaults) ⇒ Grid

Returns a new instance of Grid.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/object_table/grouping/grid.rb', line 8

def initialize(keys, defaults)
  unless defaults.is_a?(Hash)
    raise "Expected defaults to be a hash, got: #{defaults.inspect}"
  end
  defaults.default = 0
  @defaults = defaults

  @values = {}
  @index = {}
  @ids = keys.map{|k| @index[k] ||= @index.length}
  @keys = keys
  @length = @index.length
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



6
7
8
# File 'lib/object_table/grouping/grid.rb', line 6

def index
  @index
end

#valuesObject (readonly)

Returns the value of attribute values.



6
7
8
# File 'lib/object_table/grouping/grid.rb', line 6

def values
  @values
end

Instance Method Details

#[](k) ⇒ Object



22
23
24
# File 'lib/object_table/grouping/grid.rb', line 22

def [](k)
  (@values[k] ||= Array.new(@length, @defaults[k]))[@id]
end

#[]=(k, v) ⇒ Object



26
27
28
# File 'lib/object_table/grouping/grid.rb', line 26

def []=(k, v)
  @values[k][@id] = v
end

#apply_to_rows(rows, key_struct, block) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/object_table/grouping/grid.rb', line 36

def apply_to_rows(rows, key_struct, block)
  @ids.zip(@keys, rows) do |id, key, row|
    @id = id
    row.K = key_struct.new(*key)
    row.R = self
    ObjectTable::Util.apply_block(row, block)
  end
end