csv_to_object 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/csv_to_object/version.rb +1 -1
- data/lib/csv_to_object.rb +7 -6
- metadata +3 -3
data/lib/csv_to_object.rb
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
#coding: utf-8
|
|
1
2
|
require "csv_to_object/version"
|
|
2
3
|
require 'csv'
|
|
3
4
|
|
|
@@ -8,7 +9,7 @@ module CsvToObject
|
|
|
8
9
|
def initialize(input_path)
|
|
9
10
|
@input_path = File.open(input_path)
|
|
10
11
|
end
|
|
11
|
-
|
|
12
|
+
|
|
12
13
|
# Converts the data lines of the csv file to objects.
|
|
13
14
|
# Objects have the class of the name of the csv file.
|
|
14
15
|
# The first line of the csv file defines the attribute names for the data lines.
|
|
@@ -18,22 +19,22 @@ module CsvToObject
|
|
|
18
19
|
def to_objects
|
|
19
20
|
objects = []
|
|
20
21
|
file = File.open(@input_path)
|
|
21
|
-
CSV.table(file, {header_converters: header_converter}).each do |row|
|
|
22
|
+
CSV.table(file, {header_converters: header_converter, encoding: "ISO-8859-1"}).each do |row|
|
|
22
23
|
objects << new_object(row.to_hash)
|
|
23
24
|
end
|
|
24
25
|
objects
|
|
25
26
|
end
|
|
26
|
-
|
|
27
|
+
|
|
27
28
|
private
|
|
28
|
-
|
|
29
|
+
|
|
29
30
|
def header_converter
|
|
30
31
|
lambda { |h| h.encode(CSV::ConverterEncoding).downcase.gsub(/\s+/, "_") }
|
|
31
32
|
end
|
|
32
|
-
|
|
33
|
+
|
|
33
34
|
def new_object(attrs)
|
|
34
35
|
@object ? @object.new(attrs) : object_to_create().new(attrs)
|
|
35
36
|
end
|
|
36
|
-
|
|
37
|
+
|
|
37
38
|
def object_to_create()
|
|
38
39
|
class_name = File.basename(@input_path).gsub('.csv','').capitalize
|
|
39
40
|
@object = Object::const_get(class_name)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: csv_to_object
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -10,7 +10,7 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date:
|
|
13
|
+
date: 2013-04-24 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: rspec
|
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
69
69
|
version: '0'
|
|
70
70
|
requirements: []
|
|
71
71
|
rubyforge_project:
|
|
72
|
-
rubygems_version: 1.8.
|
|
72
|
+
rubygems_version: 1.8.24
|
|
73
73
|
signing_key:
|
|
74
74
|
specification_version: 3
|
|
75
75
|
summary: Given the path to a CSV file, will return a collection of objects. The csv
|