Class: TableCreator::Col
- Inherits:
-
Object
- Object
- TableCreator::Col
- Includes:
- ActionView::Helpers::TagHelper
- Defined in:
- lib/table_creator/col.rb
Instance Attribute Summary collapse
-
#anchor ⇒ Object
Returns the value of attribute anchor.
-
#colspan ⇒ Object
Returns the value of attribute colspan.
-
#data ⇒ Object
Returns the value of attribute data.
-
#data_type ⇒ Object
Returns the value of attribute data_type.
-
#link_to ⇒ Object
Returns the value of attribute link_to.
-
#options ⇒ Object
Returns the value of attribute options.
-
#row ⇒ Object
Returns the value of attribute row.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(data, row, type = nil) ⇒ Col
constructor
A new instance of Col.
- #quote(data) ⇒ Object
- #to_csv ⇒ Object
- #to_html ⇒ Object
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
#anchor ⇒ Object
Returns the value of attribute anchor.
5 6 7 |
# File 'lib/table_creator/col.rb', line 5 def anchor @anchor end |
#colspan ⇒ Object
Returns the value of attribute colspan.
5 6 7 |
# File 'lib/table_creator/col.rb', line 5 def colspan @colspan end |
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/table_creator/col.rb', line 5 def data @data end |
#data_type ⇒ Object
Returns the value of attribute data_type.
5 6 7 |
# File 'lib/table_creator/col.rb', line 5 def data_type @data_type end |
#link_to ⇒ Object
Returns the value of attribute link_to.
5 6 7 |
# File 'lib/table_creator/col.rb', line 5 def link_to @link_to end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/table_creator/col.rb', line 5 def @options end |
#row ⇒ Object
Returns the value of attribute row.
5 6 7 |
# File 'lib/table_creator/col.rb', line 5 def row @row end |
#type ⇒ Object
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_csv ⇒ Object
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_html ⇒ Object
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 = content_tag :a, content, :href => link_to if link_to content = content_tag :a, content, :name => anchor if anchor tag_class = [[:class].presence, data_type.presence].compact.join(' ') attributes = .except(:type).merge(:class => tag_class, :colspan => colspan) content_tag col_tag, content.to_s.gsub(/\n/, tag(:br)).html_safe, attributes end |