Class: ActiveReport::Array

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#csv_force_encoding?, #csv_options, evaluate

Constructor Details

#initialize(data, headers: nil, options: {}) ⇒ Array

Returns a new instance of Array.



7
8
9
10
11
# File 'lib/active_report/array.rb', line 7

def initialize(data, headers: nil, options: {})
  @data = data
  @headers = headers
  @options = csv_options.merge(options)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/active_report/array.rb', line 5

def data
  @data
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/active_report/array.rb', line 5

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/active_report/array.rb', line 5

def options
  @options
end

Class Method Details

.export(data, headers: nil, options: {}) ⇒ Object



13
14
15
16
# File 'lib/active_report/array.rb', line 13

def self.export(data, headers: nil, options: {})
  klass = new(data, headers: headers, options: options)
  klass.export
end

.import(data, headers: nil, options: {}) ⇒ Object



18
19
20
21
# File 'lib/active_report/array.rb', line 18

def self.import(data, headers: nil, options: {})
  klass = new(data, headers: headers, options: options)
  klass.import
end

Instance Method Details

#exportObject



23
24
25
26
27
28
29
30
# File 'lib/active_report/array.rb', line 23

def export
  @data = munge_first(@data)

  CSV.generate(@options) do |csv|
    csv << @headers unless @headers.nil?
    @data.lazy.each { |cell| csv << cell }
  end
end

#importObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_report/array.rb', line 32

def import
  array = merge(@headers)

  CSV.foreach(@data, @options) do |row|
    row = encode_to_utf8(row) if csv_force_encoding?
    array.push(row)
  end

  array = array.flatten if array.size < 2
  metatransform(array)
end