Class: TnPDF::Table::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/tn_pdf/table_column.rb

Overview

Represents a column of a table. Typically not used directly, but through Table#add_column and friends.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ Column

Creates a new Column

The parameter has to be an Array in the form:

[header, procedure, style, width]

where:

header (required)

Is a String that contains the column header

procedure (required)

Is an object that responds to to_proc, typically a Symbol or a Proc. It represents the procedure used to extract the information from the object.

style (optional)

Defines how the formatting of the result will occur. Typically used for formatting currencies, numbers etc, but can be anything defined on Configuration, the YAML Configuration file or a Hash that contains (at least) the :format key. It defaults to the :text style.

width (optional)

Defines the (visual) width of the column. May be defined in PDF points (1/72 inch), a String “in” the cm or mm units, or a String representing a percentage.

It is important to note that, in the case of a percentage-based column, it represents a percentage of the page on which the table will be rendered, not of the table.

Examples:

Column.new [ "Full name", :full_name ]
sum = Proc.new { |obj| obj.value_a + obj.value_b }
Column.new [ "Sum", sum, :number, "15%" ]

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
# File 'lib/tn_pdf/table_column.rb', line 76

def initialize(arguments)
  raise ArgumentError unless valid_column_args?(arguments)
  @header = arguments[0].to_s
  @proc   = arguments[1].to_proc
  @style  = Column.style_for(arguments[2])
  @width  = Configuration.perform_conversions arguments[3]
  if @width.nil?
    column_width_type = :generated
  end
end

Instance Attribute Details

#column_width_typeObject

Returns the value of attribute column_width_type.



57
58
59
# File 'lib/tn_pdf/table_column.rb', line 57

def column_width_type
  @column_width_type
end

#headerString (readonly)

Is a String that contains the column header

Returns:

  • (String)


8
9
10
# File 'lib/tn_pdf/table_column.rb', line 8

def header
  @header
end

#indexObject

Returns the value of attribute index.



121
122
123
# File 'lib/tn_pdf/table_column.rb', line 121

def index
  @index
end

#max_widthObject

Returns the value of attribute max_width.



121
122
123
# File 'lib/tn_pdf/table_column.rb', line 121

def max_width
  @max_width
end

#proc#to_proc (readonly) Also known as: to_proc

Is an object that responds to to_proc, typically a Symbol or a Proc. It represents the procedure used to extract the information from the object.

Examples:

column.proc = :full_name
myProc = Proc.new do |person|
  "#{person.id} - #{person.name}"
end
column.proc = myProc

Returns:



21
22
23
# File 'lib/tn_pdf/table_column.rb', line 21

def proc
  @proc
end

#styleSymbol, Hash (readonly)

Defines how the formatting of the result will occur. Typically used for formatting currencies, numbers etc, but can be anything defined on Configuration, the YAML Configuration file or a Hash that contains (at least) the :format key. It defaults to the :text style.

Examples:

column.style = :currency
column.style = { :format  => "%.2f",
                 :decimal => ",",
                 :align   => :right }

Returns:

  • (Symbol, Hash)


35
36
37
# File 'lib/tn_pdf/table_column.rb', line 35

def style
  @style
end

#widthDouble, String

Defines the (visual) width of the column. May be defined in PDF points (1/72 inch), a String “in” the cm or mm units, or a String representing a percentage.

It is important to note that, in the case of a percentage-based column, it represents a percentage of the page on which the table will be rendered, not of the table.

Examples:

width = 1234.45
width = "1.5cm"
width = "14mm"
width = "20%"

Returns:

  • (Double, String)


53
54
55
# File 'lib/tn_pdf/table_column.rb', line 53

def width
  @width
end

Instance Method Details

#prawn_styleObject



92
93
94
# File 'lib/tn_pdf/table_column.rb', line 92

def prawn_style
  style.reject { |k, v| [:format, :decimal].include? k }
end

#value_for(object) ⇒ Object



87
88
89
90
# File 'lib/tn_pdf/table_column.rb', line 87

def value_for(object)
  value = @proc.call(object)
  Column.format_value(value, style)
end