Class: AxlsxReport::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx_report/column.rb

Overview

Report’s column representation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, callable, options = {}) ⇒ Column

Creates new column

Parameters:

  • name (String)

    Column name

  • callable (Proc)

    Proc to calculate column value form given object

  • options (Hash) (defaults to: {})

    ({}) Column parameters

Options Hash (options):

  • width: (Integer) — default: 16

    column width

  • transform: (Symbol)

    report object method to be called after value calculation

  • with: (Symbol)

    method name of given object to be used to base for cell value calculation

  • units: (String)

    Units text to be added to the header in the column

  • type: (Symbol)

    axlsx type to be used in this column. One of Axlsx::Cell::CELL_TYPES



16
17
18
# File 'lib/axlsx_report/column.rb', line 16

def initialize(name, callable, options = {})
  @name, @callable, @options = name, callable, options
end

Instance Attribute Details

#callableObject (readonly)

Returns the value of attribute callable.



4
5
6
# File 'lib/axlsx_report/column.rb', line 4

def callable
  @callable
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/axlsx_report/column.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/axlsx_report/column.rb', line 4

def options
  @options
end

Instance Method Details

#value(report, obj) ⇒ Object

Calculates column value for given object

Parameters:

  • report (AxlsxReport::Base)

    Report the value is calculated for

  • obj (Any)

    Source object for column value

Returns:

  • Column value for given object.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/axlsx_report/column.rb', line 32

def value(report, obj)
  source = extract_source(obj)
  return nil if source.nil?
  value =
    if callable.arity.zero?
      source.instance_exec &callable
    else
      report.instance_exec source, &callable
    end
  transform = options[:transform]
  value = report.send transform, value if transform
  value
end