Class: HashToCsv::HashToCsv

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_to_csv/hash_to_csv.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash_flattener) ⇒ HashToCsv

Returns a new instance of HashToCsv.



7
8
9
# File 'lib/hash_to_csv/hash_to_csv.rb', line 7

def initialize(hash_flattener)
  @hash_flattener = hash_flattener
end

Instance Method Details

#to_csv(hashes, options = {}, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hash_to_csv/hash_to_csv.rb', line 11

def to_csv(hashes, options={}, &block)
  @hashes = [hashes].flatten.map { |h| @hash_flattener.flatten h }
  @include = options[:include]
  @exclude = options[:exclude]
  @sort = options[:sort] == true

  CSV.generate do |csv|
    csv << column_names
    rows.each { |row| csv << values_for_row(row, &block) }
  end
end