Class: Xport::ExportBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/xport/export_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(export, &block) ⇒ ExportBuilder

Returns a new instance of ExportBuilder.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/xport/export_builder.rb', line 7

def initialize(export, &block)
  @export  = export
  @columns = []
  @headers = []
  @groups  = []
  @styles  = []
  @types   = []
  @widths  = []
  @blocks  = []
  instance_exec(export, &block)
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def blocks
  @blocks
end

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def columns
  @columns
end

#exportObject (readonly)

Returns the value of attribute export.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def export
  @export
end

#groupsObject (readonly)

Returns the value of attribute groups.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def groups
  @groups
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def headers
  @headers
end

#stylesObject (readonly)

Returns the value of attribute styles.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def styles
  @styles
end

#typesObject (readonly)

Returns the value of attribute types.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def types
  @types
end

#widthsObject (readonly)

Returns the value of attribute widths.



5
6
7
# File 'lib/xport/export_builder.rb', line 5

def widths
  @widths
end

Instance Method Details

#column(name, type: nil, style: nil, width: nil, header: nil, group: nil, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/xport/export_builder.rb', line 19

def column(name, type: nil, style: nil, width: nil, header: nil, group: nil, &block)
  columns << name
  headers << (header || name)
  groups  << group
  types   << type
  styles  << style
  widths  << width
  blocks  << block
end

#grouped?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/xport/export_builder.rb', line 29

def grouped?
  groups.any?
end

#groups_with_offset_and_colspanObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xport/export_builder.rb', line 33

def groups_with_offset_and_colspan
  offset  = 0
  colspan = 1
  [].tap do |result|
    groups.each do |group|
      last = result.last
      # check if current group is same as last group
      if last && last[0] == group
        # if group is the same, update colspan
        last[2] += 1
      else
        result << [group, offset, colspan]
      end
      offset  += 1
    end
  end
end