Class: ActiveReport::Array

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Array.



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

def initialize(datum, headers: nil, options: {})
  @datum   = datum
  @headers = headers
  @options = options
end

Instance Attribute Details

#datumObject

Returns the value of attribute datum.



3
4
5
# File 'lib/active_report/array.rb', line 3

def datum
  @datum
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/active_report/array.rb', line 3

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/active_report/array.rb', line 3

def options
  @options
end

Class Method Details

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



11
12
13
# File 'lib/active_report/array.rb', line 11

def self.export(datum, headers: nil, options: {})
  new(datum, headers: headers, options: options).export
end

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



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

def self.import(datum, headers: nil, options: {})
  new(datum, headers: headers, options: options).import
end

Instance Method Details

#exportObject



19
20
21
22
23
24
25
26
# File 'lib/active_report/array.rb', line 19

def export
  @datum = [].push(@datum).compact unless @datum.first.is_a?(Array)

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

#importObject



28
29
30
31
32
33
34
# File 'lib/active_report/array.rb', line 28

def import
  processed_datum = [].push(@headers).compact
  CSV.foreach(@datum, @options) do |data|
    processed_datum.push(data)
  end
  return(processed_datum.count < 2 ? processed_datum.flatten : processed_datum)
end