Class: RubyFromExcel::ExcelMatrixCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/formulae/run/excel_matrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ExcelMatrixCollection

Returns a new instance of ExcelMatrixCollection.



7
8
9
10
11
12
# File 'lib/formulae/run/excel_matrix.rb', line 7

def initialize(*args)
  self.matrices = args.map do |a|
    a.respond_to?(:to_excel_matrix) ? a.to_excel_matrix : ExcelMatrix.new(a)
  end
  coerce_to_equal_sizes
end

Instance Attribute Details

#matricesObject

Returns the value of attribute matrices.



5
6
7
# File 'lib/formulae/run/excel_matrix.rb', line 5

def matrices
  @matrices
end

#max_columnsObject

Returns the value of attribute max_columns.



5
6
7
# File 'lib/formulae/run/excel_matrix.rb', line 5

def max_columns
  @max_columns
end

#max_rowsObject

Returns the value of attribute max_rows.



5
6
7
# File 'lib/formulae/run/excel_matrix.rb', line 5

def max_rows
  @max_rows
end

Instance Method Details

#coerce_to_equal_sizesObject



14
15
16
17
18
19
20
21
# File 'lib/formulae/run/excel_matrix.rb', line 14

def coerce_to_equal_sizes
  self.max_rows = matrices.max_by(&:rows).rows
  self.max_columns = matrices.max_by(&:columns).columns
  matrices.each do |m|
    m.add_columns!(max_columns - m.columns)
    m.add_rows!(max_rows - m.rows)
  end
end

#each(&block) ⇒ Object



23
24
25
# File 'lib/formulae/run/excel_matrix.rb', line 23

def each &block
  block ? to_enum.each { |e| yield e } : to_enum
end

#matrix_mapObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/formulae/run/excel_matrix.rb', line 37

def matrix_map
  results = Array.new(max_rows) { Array.new(max_columns) }
  0.upto(max_rows-1) do |j|
    0.upto(max_columns-1) do |i|
      results[j][i] =  yield *matrices.map { |m| m.values[j][i] }
    end
  end
  em = ExcelMatrix.new(results)
  if em.rows == 1 && em.columns == 1
    return em.values.first.first
  else
    return em
  end
end

#to_enumObject



27
28
29
30
31
32
33
34
35
# File 'lib/formulae/run/excel_matrix.rb', line 27

def to_enum
  Enumerator.new do |yielder|
    0.upto(max_rows-1) do |j|
      0.upto(max_columns-1) do |i|
        yielder << matrices.map { |m| m.values[j][i] }
      end
    end
  end
end