Class: HtmlFormat::Generator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Generator

Returns a new instance of Generator.



48
49
50
# File 'lib/html_format/generator.rb', line 48

def initialize(options)
  @options = HtmlFormat.default_options.merge(depth: 0).merge(options)
end

Instance Attribute Details

#output_bufferObject

Returns the value of attribute output_buffer.



23
24
25
# File 'lib/html_format/generator.rb', line 23

def output_buffer
  @output_buffer
end

Class Method Details

.generate(*args, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/html_format/generator.rb', line 25

def self.generate(*args, &block)
  if block_given?
    object = yield
    options = args.extract_options!
  else
    if args.size == 0
      return
    elsif args.size == 1
      object = args.first
      options = {}
    else
      options = args.extract_options!
      if args.size > 1
        object = args
      else
        object = args.first
      end
    end
  end

  new(options).generate(object)
end

Instance Method Details

#generate(obj) ⇒ Object



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

def generate(obj)
  return if obj.blank?

  info = function_table.find { |e| e[:_case].call(obj) }
  body = ''.html_safe

  if v = @options[:caption].presence
    body << v
  end
  if @options[:header_patch]
    if info[:header_process]
      if v = info[:header_process].call(obj)
        body << v
      end
    end
  end
  body << info[:process].call(obj)
  body = tag.table(body, :class => table_class(info))
  if @options[:depth].zero?
    if @options[:responsive]
      body = tag.div(body, :class => 'table-responsive')
    end
    if @options[:title].present?
      body = tag.send(@options[:title_tag], @options[:title], :class => 'title') + body
    end
  end
  tag.div(body, :class => ['html_format', "html_format_depth_#{@options[:depth]}"])
end