Class: UDRS::Components::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/udrs/components/table.rb

Constant Summary collapse

COLUMN_SIZES =
%i(fit expand hide)

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#columnsObject (readonly)

Returns the value of attribute columns.



4
5
6
# File 'lib/udrs/components/table.rb', line 4

def columns
  @columns
end

#rowsObject (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_widthsObject



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

#row(*args, &block) ⇒ Object



17
18
19
# File 'lib/udrs/components/table.rb', line 17

def row(*args, &block)
	@rows << Row.new(self, *args, &block)
end