Class: ActiveReport::Record
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, only: nil, except: nil, headers: nil, options: {}, model: nil) ⇒ 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.
Methods inherited from Base
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 @except = except @headers = headers @model = model @only = only @options = .merge!() 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 @options 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, only: nil, except: nil, headers: nil, options: {}, model: nil) ⇒ Object
19 20 21 |
# File 'lib/active_report/record.rb', line 19 def self.import(datum, only: nil, except: nil, headers: nil, options: {}, model: nil) new(datum, only: only, except: except, headers: headers, options: , model: model).import end |
Instance Method Details
#export ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/active_report/record.rb', line 23 def export @datum = if @datum.is_a?(ActiveRecord::Relation) JSON.parse(@datum.to_json).flatten else merge(@datum.attributes) end @only = munge(@only).map(&:to_s) @except = munge(@except).map(&:to_s) 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
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/record.rb', line 39 def import if @model.nil? || (@model.superclass != ActiveRecord::Base) raise ArgumentError, 'Model must be an ActiveRecord::Base object.' end @datum = ActiveReport::Hash.import(@datum, headers: @headers, options: @options) @datum = munge(@datum) @only = munge(@only) @except = munge(@except) @datum.lazy.each do |data| params = {} data.each do |key, value| key = key.to_s.downcase.tr(' ', '_').tr('-', '_').to_sym params.store(key, value) end filter(params) params.delete(:id) @model.create(params) end end |