Class: UDRS::Components::Table
- Inherits:
-
Object
- Object
- UDRS::Components::Table
- Defined in:
- lib/udrs/components/table.rb
Constant Summary collapse
- COLUMN_SIZES =
%i(fit expand hide)
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #get_column_widths ⇒ Object
-
#initialize(*columns, &block) ⇒ Table
constructor
A new instance of Table.
- #row(*args, &block) ⇒ Object
Constructor Details
#initialize(*columns, &block) ⇒ Table
Returns a new instance of Table.
8 9 10 11 12 13 14 15 |
# File 'lib/udrs/components/table.rb', line 8 def initialize(*columns, &block) @columns = columns invalid = (columns.reject { |c| c.is_a?(Fixnum) } - COLUMN_SIZES).uniq fail ArgumentError, "Invalid column size(s): #{invalid.join(', ')}" unless invalid.empty? @rows = [] block.call(self) end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'lib/udrs/components/table.rb', line 4 def columns @columns end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
4 5 6 |
# File 'lib/udrs/components/table.rb', line 4 def rows @rows end |
Instance Method Details
#get_column_widths ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/udrs/components/table.rb', line 21 def get_column_widths return columns.each_with_index.map do |column, i| case column # Fixed width when Fixnum next [column, column, column] # Fit when :fit size = rows.map { |r| r.cells[i] }.map(&:text).map(&:size).max next [size, size, size] # Expand when :expand size = rows.map { |r| r.cells[i] }.map(&:text).map(&:size).max next [0, size, 9999] # Hide when :hide next [0, 0, 0] else fail ArgumentError, "Cannot calculate column width for #{column}" end end end |