Class: ActiveCsv

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
lib/PVLIB_Ruby/models/active_csv.rb

Overview

This is a model in model-view-controller design pattern. It is analogous to ActiveRecord. Hence, it’s a placeholder for data from CSV file. Borrowed the idea from “kyle.conarro.com/importing-from-csv-in-rails”.

Direct Known Subclasses

Inverter, Location, PvModule

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(csv_filepath) ⇒ Object



10
11
12
13
14
# File 'lib/PVLIB_Ruby/models/active_csv.rb', line 10

def self.create(csv_filepath)
  new_instance = self.new
  new_instance.load_data(csv_filepath)
  new_instance
end

Instance Method Details

#load_data(csv_filepath) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/PVLIB_Ruby/models/active_csv.rb', line 24

def load_data(csv_filepath)
  csv = CSV.new(File.new(csv_filepath), headers: true, header_converters: :symbol)

  data = csv.shift

  previous_header = nil;
  index = 0;

  data.headers.each do |header|

    if "#{header}=" == "="

      if !previous_header.blank? && !data[index].blank?

        assigned_data_for_previous_header = self.send(previous_header.to_sym)
        
        if assigned_data_for_previous_header.instance_of? Array
          assigned_data_for_previous_header << data[index]
          self.send("#{previous_header}=".to_sym, assigned_data_for_previous_header)
        else
          self.send("#{previous_header}=".to_sym, [assigned_data_for_previous_header,data[index]])
        end

      end

    else
      self.send("#{header}=".to_sym, data[header])
    end

    previous_header = header unless header.blank?
    index = index + 1;

  end

end

#persisted?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/PVLIB_Ruby/models/active_csv.rb', line 16

def persisted?
  false
end

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/PVLIB_Ruby/models/active_csv.rb', line 20

def valid?
  # TODO: implement this
end