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

#csv_force_encoding?, #csv_options, evaluate

Constructor Details

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

Returns a new instance of Record.



9
10
11
12
13
14
15
16
# File 'lib/active_report/record.rb', line 9

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

Instance Attribute Details

#datumObject

Returns the value of attribute datum.



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

def datum
  @datum
end

#exceptObject

Returns the value of attribute except.



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

def except
  @except
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#modelObject

Returns the value of attribute model.



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

def model
  @model
end

#onlyObject

Returns the value of attribute only.



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

def only
  @only
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

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



18
19
20
21
# File 'lib/active_report/record.rb', line 18

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

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



23
24
25
26
# File 'lib/active_report/record.rb', line 23

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

Instance Method Details

#exportObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_report/record.rb', line 28

def export
  @datum = if @datum.is_a?(ActiveRecord::Relation)
             JSON.parse(@datum.to_json).flatten
           else
             merge(@datum.attributes)
           end

  @only.map!(&:to_s)
  @except.map!(&:to_s)

  CSV.generate(@options) do |csv|
    csv << (@headers || filter_humanize_keys(@datum))
    @datum.lazy.each { |data| csv << filter_values(data) }
  end
end

#importObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/active_report/record.rb', line 44

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)

  @datum.lazy.each do |data|
    params = {}

    data.lazy.each do |key, value|
      key = key.to_s.downcase.gsub(/ |-/, '_').to_sym
      params[key] = value
    end

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