Class: ActiveReport::Hash

Inherits:
Base
  • Object
show all
Defined in:
lib/active_report/hash.rb

Instance Method Summary collapse

Methods inherited from Base

#csv_force_encoding?, #csv_options, evaluate, export, import, #import_adapter, #import_options, #initialize

Constructor Details

This class inherits a constructor from ActiveReport::Base

Instance Method Details

#exportObject



5
6
7
8
9
10
# File 'lib/active_report/hash.rb', line 5

def export
  @data = munge(@data)
  @opts[:headers] = (@opts[:headers] || filter_humanize_keys(@data))

  @opts[:stream] ? export_stream : export_csv
end

#importObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_report/hash.rb', line 12

def import
  array = []

  CSV.foreach(@data, @opts[:options]).with_index do |data, i|
    data = encode_to_utf8(data) if csv_force_encoding?

    if @opts[:headers].nil? && i.zero?
      @opts[:headers] = data
    else
      subdata = {}
      @opts[:headers].each_with_index { |header, idx| subdata[header.to_s] = data[idx] }
      filter(subdata)
      array.push(subdata)
    end
  end

  array = array.first if array.size == 1
  metatransform(array)
end