Class: ActiveReport::Array
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #export ⇒ Object
- #import ⇒ Object
-
#initialize(data, headers: nil, options: {}) ⇒ Array
constructor
A new instance of Array.
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 = .merge() end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/active_report/array.rb', line 5 def data @data end |
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/active_report/array.rb', line 5 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_report/array.rb', line 5 def @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: ) 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: ) klass.import end |
Instance Method Details
#export ⇒ Object
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 |
#import ⇒ Object
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 (array) end |