Class: Canvas::Utils::Csv
- Inherits:
-
Object
- Object
- Canvas::Utils::Csv
- Defined in:
- app/models/canvas/utils/csv.rb
Class Method Summary collapse
- .create_csv(models, headers) ⇒ Object
- .hash_csv(csv_string) ⇒ Object
- .hash_csv_file(file_path) ⇒ Object
- .unzip_csv(zip) ⇒ Object
Class Method Details
.create_csv(models, headers) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/models/canvas/utils/csv.rb', line 14 def self.create_csv(models, headers) CSV.generate do |csv| csv << headers.map do |header| header.instance_of?(Hash) ? header.values.first.to_s : header.to_s end models.each do |model| csv << headers.map do |header| const = header.instance_of?(Hash) ? header.keys.first : header model.respond_to?(const) ? model.send(const) : nil end end end end |
.hash_csv(csv_string) ⇒ Object
6 7 8 |
# File 'app/models/canvas/utils/csv.rb', line 6 def self.hash_csv(csv_string) CSV.parse(csv_string, {:headers => true, :return_headers => false, :header_converters => :symbol} ) end |
.hash_csv_file(file_path) ⇒ Object
10 11 12 |
# File 'app/models/canvas/utils/csv.rb', line 10 def self.hash_csv_file(file_path) CSV.read(file_path, {:headers => true, :return_headers => false, :header_converters => :symbol} ) end |
.unzip_csv(zip) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'app/models/canvas/utils/csv.rb', line 28 def self.unzip_csv(zip) {}.tap do |h| Zip::InputStream.open(StringIO.new(zip)) do |archive| while entry = archive.get_next_entry h[entry.name] = self.hash_csv(archive.read) end end end end |