Class: ActiveReport::Hash
- Inherits:
-
Object
- Object
- ActiveReport::Hash
- Defined in:
- lib/active_report/hash.rb
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.
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 @only = only @except = except @headers = headers @options = 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 31 32 33 34 35 36 37 |
# File 'lib/active_report/hash.rb', line 21 def export @datum = [].push(@datum).compact unless @datum.is_a?(Array) @only = [].push(@only).compact unless @only.is_a?(Array) @except = [].push(@except).compact unless @except.is_a?(Array) CSV.generate(@options) do |csv| header = @datum.first.dup.keep_if { |k,v| @only.include?(k) } unless @only.empty? header = @datum.first.dup.delete_if { |k,v| @except.include?(k) } unless @except.empty? csv << (@headers || (header || @datum.first).keys.map { |k| k.to_s.gsub("_", " ").capitalize}) @datum.each do |data| cell = data.dup.keep_if { |k,v| @only.include?(k) } unless @only.empty? cell = data.dup.delete_if { |k,v| @except.include?(k) } unless @except.empty? csv << (cell || data).values end end end |
#import ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_report/hash.rb', line 39 def import @only = [].push(@only).compact unless @only.is_a?(Array) @except = [].push(@except).compact unless @except.is_a?(Array) processed_datum = [] CSV.foreach(@datum, @options).each_with_index do |data, line| if @headers.nil? && line.zero? @headers = data else processed_data = {} @headers.each_with_index do |v,i| processed_data.store(v.to_s, data.fetch(i, nil) ) end processed_data.keep_if { |k,v| @only.include?(k) } unless @only.empty? processed_data.delete_if { |k,v| @except.include?(k) } unless @except.empty? processed_datum.push(processed_data) end end return(processed_datum) end |