Class: ActiveReport::Hash
Instance Attribute Summary collapse
-
#datum ⇒ Object
Returns the value of attribute datum.
-
#except ⇒ Object
Returns the value of attribute except.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#only ⇒ Object
Returns the value of attribute only.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .export(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Object
- .import(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Object
Instance Method Summary collapse
- #export ⇒ Object
- #import ⇒ Object
-
#initialize(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Hash
constructor
A new instance of Hash.
Methods inherited from Base
Constructor Details
#initialize(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Hash
Returns a new instance of Hash.
5 6 7 8 9 10 11 |
# File 'lib/active_report/hash.rb', line 5 def initialize(datum, only: nil, except: nil, headers: nil, options: {}) @datum = datum @except = except @headers = headers @only = only @options = .merge!() end |
Instance Attribute Details
#datum ⇒ Object
Returns the value of attribute datum.
3 4 5 |
# File 'lib/active_report/hash.rb', line 3 def datum @datum end |
#except ⇒ Object
Returns the value of attribute except.
3 4 5 |
# File 'lib/active_report/hash.rb', line 3 def except @except end |
#headers ⇒ Object
Returns the value of attribute headers.
3 4 5 |
# File 'lib/active_report/hash.rb', line 3 def headers @headers end |
#only ⇒ Object
Returns the value of attribute only.
3 4 5 |
# File 'lib/active_report/hash.rb', line 3 def only @only end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/active_report/hash.rb', line 3 def @options end |
Class Method Details
.export(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Object
13 14 15 |
# File 'lib/active_report/hash.rb', line 13 def self.export(datum, only: nil, except: nil, headers: nil, options: {}) new(datum, only: only, except: except, headers: headers, options: ).export end |
.import(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Object
17 18 19 |
# File 'lib/active_report/hash.rb', line 17 def self.import(datum, only: nil, except: nil, headers: nil, options: {}) new(datum, only: only, except: except, headers: headers, options: ).import end |
Instance Method Details
#export ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_report/hash.rb', line 21 def export @datum = munge(@datum) @only = munge(@only) @except = munge(@except) CSV.generate(@options) do |csv| csv << (@headers || (filter_first(@datum) || @datum.first).keys.map { |hdr| humanize(hdr) }) @datum.lazy.each { |data| csv << (filter(data) || data).values } end end |
#import ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/active_report/hash.rb', line 32 def import @only = munge(@only) @except = munge(@except) datum = [] CSV.foreach(@datum, @options).with_index do |data, line| data = encode_to_utf8(data) if force_encoding? if @headers.nil? && line.zero? @headers = data else subdata = {} @headers.lazy.each_with_index do |header, idx| subdata.store(header.to_s, data[idx]) end filter(subdata) datum.push(subdata) end end datum = datum.first if datum.size == 1 datum = (datum) datum end |