Module: DevSuite::Utils::Data::Serialization

Included in:
DevSuite::Utils::Data
Defined in:
lib/dev_suite/utils/data/serialization.rb

Instance Method Summary collapse

Instance Method Details

#to_csv(array_of_hashes) ⇒ Object

Convert an array of hashes to CSV format



18
19
20
21
22
23
24
25
# File 'lib/dev_suite/utils/data/serialization.rb', line 18

def to_csv(array_of_hashes)
  return "" if array_of_hashes.empty?

  ::CSV.generate do |csv|
    csv << array_of_hashes.first.keys # Header row
    array_of_hashes.each { |hash| csv << hash.values }
  end
end

#to_json(data) ⇒ Object

Convert a hash or array of hashes to compact JSON



11
12
13
14
15
# File 'lib/dev_suite/utils/data/serialization.rb', line 11

def to_json(data)
  return if data.nil?

  ::JSON.generate(data) # Use JSON.generate for compact output
end