Class: Ekidata::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/ekidata/import.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#env_nameObject

Returns the value of attribute env_name.



3
4
5
# File 'lib/ekidata/import.rb', line 3

def env_name
  @env_name
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/ekidata/import.rb', line 3

def headers
  @headers
end

#model_nameObject

Returns the value of attribute model_name.



3
4
5
# File 'lib/ekidata/import.rb', line 3

def model_name
  @model_name
end

Instance Method Details

#from_csvObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ekidata/import.rb', line 5

def from_csv
  if ENV[@env_name].present?
    model = @model_name.classify.constantize
    puts model
    CSV.open ENV[@env_name] do |csv|
      rows = csv.map do |row|
        model.new Hash[@headers.zip row.to_a]
      end

      puts rows.last.attributes

      ActiveRecord::Base.transaction do
        model.delete_all
        model.import rows
      end
    end
  else
    puts "Specify #{@env_name} arguments."
  end
end