Class: ActiveReport::Record

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  @options = 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



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: 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: options).import
end

Instance Method Details

#exportObject



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(@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

#importObject



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: @options)
  processed_datum.each { |data| @model.create(data) }
end