Module: Workbook::Writers::JsonTableWriter

Included in:
Table
Defined in:
lib/workbook/writers/json_table_writer.rb

Instance Method Summary collapse

Instance Method Details

#to_array_of_hashes_with_values(options = {}) ⇒ Array<Hash>

Output the current workbook to an array_of_hashes_with_values format

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (Array<Hash>)

    array with hashes (comma separated values in a string)



22
23
24
25
# File 'lib/workbook/writers/json_table_writer.rb', line 22

def to_array_of_hashes_with_values options={}
  array_of_hashes = self.collect{|a| a.to_hash_with_values unless a.header?}.compact
  return array_of_hashes
end

#to_json(options = {}) ⇒ String

Output the current workbook to JSON format

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (String)

    json string



14
15
16
# File 'lib/workbook/writers/json_table_writer.rb', line 14

def to_json options={}
  JSON.generate(to_array_of_hashes_with_values(options))
end

#write_to_json(filename = "#{title}.json", options = {}) ⇒ String

Write the current workbook to JSON format

Parameters:

  • filename (String) (defaults to: "#{title}.json")
  • options (Hash) (defaults to: {})

    see #to_json

Returns:

  • (String)

    filename



32
33
34
35
# File 'lib/workbook/writers/json_table_writer.rb', line 32

def write_to_json filename="#{title}.json", options={}
  File.open(filename, 'w') {|f| f.write(to_json(options)) }
  return filename
end