Method: Json2table.create_table

Defined in:
lib/json2table.rb

.create_table(hash, options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/json2table.rb', line 30

def self.create_table(hash, options)
  html = start_table_tag(options)
  if hash.is_a?(Array)
    html += "<tr><td>\n"
    #puts ">>>> #{process_array(hash, options)}"
    html += process_array(hash, options)
  elsif hash.is_a?(Hash)
    hash.each do |key, value|
      # key goes in a column and value in second column of the same row
      html += "<tr><th>#{to_human(key)}</th>\n"
      html += "<td>"
      if value.is_a?(Hash)
        # create a row with key as heading and body
        # as another table
        html += create_table(value, options)
      elsif value.is_a?(Array)
        html += process_array(value, options)
      else      # simple primitive data type of value (non hash, non array)
        html += "#{value}</td></tr>\n"
      end
    end
  else      # simple primitive data type of value (non hash, non array)
    html += "<tr><td>#{hash}</td></tr>\n"
  end
  html += close_table_tag
  return html
end