Class: ActiveReport::Record
- Inherits:
-
Object
- Object
- ActiveReport::Record
- Defined in:
- lib/active_report/record.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.
-
#model ⇒ Object
Returns the value of attribute model.
-
#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, model: nil, only: nil, except: nil, headers: nil, options: {}) ⇒ Object
Instance Method Summary collapse
- #export ⇒ Object
- #import ⇒ Object
-
#initialize(datum, model: nil, only: nil, except: nil, headers: nil, options: {}) ⇒ Record
constructor
A new instance of Record.
Constructor Details
#initialize(datum, model: nil, only: nil, except: nil, headers: nil, options: {}) ⇒ Record
Returns a new instance of Record.
6 7 8 9 10 11 12 13 |
# File 'lib/active_report/record.rb', line 6 def initialize(datum, model: nil, only: nil, except: nil, headers: nil, options: {}) @datum = datum @model = model @only = only @except = except @headers = headers = end |
Instance Attribute Details
#datum ⇒ Object
Returns the value of attribute datum.
4 5 6 |
# File 'lib/active_report/record.rb', line 4 def datum @datum end |
#except ⇒ Object
Returns the value of attribute except.
4 5 6 |
# File 'lib/active_report/record.rb', line 4 def except @except end |
#headers ⇒ Object
Returns the value of attribute headers.
4 5 6 |
# File 'lib/active_report/record.rb', line 4 def headers @headers end |
#model ⇒ Object
Returns the value of attribute model.
4 5 6 |
# File 'lib/active_report/record.rb', line 4 def model @model end |
#only ⇒ Object
Returns the value of attribute only.
4 5 6 |
# File 'lib/active_report/record.rb', line 4 def only @only end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/active_report/record.rb', line 4 def end |
Class Method Details
.export(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Object
15 16 17 |
# File 'lib/active_report/record.rb', line 15 def self.export(datum, only: nil, except: nil, headers: nil, options: {}) new(datum, only: only, except: except, headers: headers, options: ).export end |
.import(datum, model: nil, only: nil, except: nil, headers: nil, options: {}) ⇒ Object
19 20 21 |
# File 'lib/active_report/record.rb', line 19 def self.import(datum, model: nil, only: nil, except: nil, headers: nil, options: {}) new(datum, model: model, only: only, except: except, headers: headers, options: ).import end |
Instance Method Details
#export ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/active_report/record.rb', line 23 def export @datum = @datum.is_a?(ActiveRecord::Relation) ? JSON.parse(@datum.to_json).flatten : [].push(@datum.attributes).compact @only = (@only.is_a?(Array) ? @only : [].push(@only).compact).map(&:to_s) @except = (@except.is_a?(Array) ? @except : [].push(@except).compact).map(&:to_s) CSV.generate() 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
41 42 43 44 45 46 47 48 49 |
# File 'lib/active_report/record.rb', line 41 def import if @model.nil? || (@model.superclass != ActiveRecord::Base) raise ArgumentError, "Model must be an ActiveRecord::Base object." end processed_datum = ActiveReport::Hash.import(@datum, headers: @headers, options: ) processed_datum.each { |data| @model.create(data) } end |