Class: TableCreator::Col

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/table_creator/col.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, row, type = nil) ⇒ Col

Returns a new instance of Col.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/table_creator/col.rb', line 7

def initialize(data, row, type = nil)
  @row = row
  @options = {}
  if data.is_a? Hash
    @colspan = data[:colspan]
    @data = data[:data]
    @link_to = data[:link_to]
    @anchor = data[:anchor]
    @options = data.except(:colspan, :data, :link_to, :anchor)
  else
    @data = data
  end

  @data_type = case @data.class.to_s.to_sym
  when :Fixnum
    :number
  when :String
    :text
  else
    nil
  end
  @type = type || :data
  self
end

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def anchor
  @anchor
end

#colspanObject

Returns the value of attribute colspan.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def colspan
  @colspan
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def data
  @data
end

#data_typeObject

Returns the value of attribute data_type.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def data_type
  @data_type
end

Returns the value of attribute link_to.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def link_to
  @link_to
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def options
  @options
end

#rowObject

Returns the value of attribute row.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def row
  @row
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/table_creator/col.rb', line 5

def type
  @type
end

Instance Method Details

#quote(data) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/table_creator/col.rb', line 44

def quote(data)
  formatted_data = format_csv(data)
  quoted = formatted_data.to_s.gsub('"', '\"')
  if formatted_data.to_s.include?(',')
    '"'+quoted+'"'
  else
    quoted
  end
end

#to_csvObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/table_creator/col.rb', line 32

def to_csv
  if @colspan && @colspan > 1
    cols = [quote(@data)]
    (@colspan-1).times do
      cols << ''
    end
    cols
  else
    quote(@data)
  end
end

#to_htmlObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/table_creator/col.rb', line 54

def to_html
  formatter = TableCreator.formatters(:html)[@data.class]
  content = if formatter
    result = formatter.is_a?(Symbol) ? @data.send(formatter) : formatter.call(@data)
    if result.is_a?(Hash)
      link_to = result[:link_to]
      anchor  = result[:anchor]
      @options[:class] ||= @data.class.name.underscore
      result.fetch(:data)
    else
      @options[:class] ||= @data.class.name.underscore
      result
    end
  else
    @data
  end
  col_tag = type == :header ? :th : :td
  content =  :a, content, :href => link_to if link_to
  content =  :a, content, :name => anchor if anchor
  tag_class = [options[:class].presence, data_type.presence].compact.join(' ')
  attributes = options.except(:type).merge(:class => tag_class, :colspan => colspan)

   col_tag, content.to_s.gsub(/\n/, tag(:br)).html_safe, attributes
end