Class: ActiveReport::Record

Inherits:
Base
  • Object
show all
Defined in:
lib/active_report/record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

evaluate

Constructor Details

#initialize(datum, model: nil, only: nil, except: nil, headers: nil, options: {}) ⇒ Record

Returns a new instance of Record.



6
7
8
9
# File 'lib/active_report/record.rb', line 6

def initialize(datum, model: nil, only: nil, except: nil, headers: nil, options: {})
  @datum, @except, @headers, @model, @only = datum, except, headers, model, only
  @options = duplicate_options.merge!(options)
end

Instance Attribute Details

#datumObject

Returns the value of attribute datum.



4
5
6
# File 'lib/active_report/record.rb', line 4

def datum
  @datum
end

#exceptObject

Returns the value of attribute except.



4
5
6
# File 'lib/active_report/record.rb', line 4

def except
  @except
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/active_report/record.rb', line 4

def headers
  @headers
end

#modelObject

Returns the value of attribute model.



4
5
6
# File 'lib/active_report/record.rb', line 4

def model
  @model
end

#onlyObject

Returns the value of attribute only.



4
5
6
# File 'lib/active_report/record.rb', line 4

def only
  @only
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/active_report/record.rb', line 4

def options
  @options
end

Class Method Details

.export(datum, only: nil, except: nil, headers: nil, options: {}) ⇒ Object



11
12
13
# File 'lib/active_report/record.rb', line 11

def self.export(datum, only: nil, except: nil, headers: nil, options: {})
  new(datum, only: only, except: except, headers: headers, options: options).export
end

.import(datum, only: nil, except: nil, headers: nil, options: {}, model: nil) ⇒ Object



15
16
17
# File 'lib/active_report/record.rb', line 15

def self.import(datum, only: nil, except: nil, headers: nil, options: {}, model: nil)
  new(datum, only: only, except: except, headers: headers, options: options, model: model).import
end

Instance Method Details

#exportObject



19
20
21
22
23
24
25
26
27
# File 'lib/active_report/record.rb', line 19

def export
  @datum = @datum.is_a?(ActiveRecord::Relation) ? JSON.parse(@datum.to_json).flatten : merge(@datum.attributes)
  @only, @except = munge(@only).map(&:to_s), munge(@except).map(&:to_s)

  CSV.generate(@options) do |csv|
    csv << (@headers || (filter_first(@datum) || @datum.first).keys.map { |header| humanize(header) })
    @datum.lazy.each { |data| csv << (filter(data) || data).values }
  end
end

#importObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_report/record.rb', line 29

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, @only, @except = munge(@datum), munge(@only), munge(@except)

  @datum.lazy.each do |data|
    params = {}
    data.each do |key, value|
      key = key.to_s.downcase.gsub(" ", "_").gsub("-", "_").to_sym
      params.store(key, value)
    end

    filter(params)
    params.delete(:id)
    @model.create(params)
  end
end