csv_to_object 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +23 -2
- data/csv_to_object.gemspec +2 -2
- data/lib/csv_to_object/version.rb +1 -1
- data/lib/csv_to_object.rb +11 -5
- data/spec/csv_to_object_spec.rb +20 -3
- data/spec/spec_helper.rb +6 -0
- data/test/csv/person.csv +11 -11
- data/test/lib/person.rb +3 -3
- metadata +7 -5
data/README.md
CHANGED
@@ -24,9 +24,30 @@ Given a file person.csv with the contents
|
|
24
24
|
`0,Lindsey,Austino`
|
25
25
|
`1,Dodie,Egnor`
|
26
26
|
|
27
|
-
|
27
|
+
```ruby
|
28
|
+
csv_path = 'tmp/person.csv'
|
29
|
+
CsvToObject::CsvToObject.new(csv.path).to_objects
|
30
|
+
[#<Person:0x000001021a8148 @id=0, @first_name="Lindsey", @last_name="Austino">,
|
31
|
+
#<Person:0x000001021a8030 @id=1, @first_name="Dodie", @last_name="Egnor">]
|
32
|
+
```
|
28
33
|
|
29
|
-
|
34
|
+
### Attribute Names
|
35
|
+
|
36
|
+
CSV_to_object does not use the default header converter options. When converting the first line of the csv file into attribute names, the following conversions are applied:
|
37
|
+
|
38
|
+
* Encoded using CSV::CharacterEncoding
|
39
|
+
* Downcased
|
40
|
+
* Spaces are replaced with underscores
|
41
|
+
|
42
|
+
When passed to the target object.new() method, attribute names are strings, not symbols. For example, the instantiation of the Person object above would be:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Person.new({"id" => 0, "first_name" => "Lindsey", "last_name" => "Austino"})
|
46
|
+
```
|
47
|
+
|
48
|
+
So if you are looking for specific hash keys in `initialize()`, use strings not symbols.
|
49
|
+
|
50
|
+
If this behavior is not desired, you could overwrite the `header_converter` method of `CsvToObject`.
|
30
51
|
|
31
52
|
## Contributing
|
32
53
|
|
data/csv_to_object.gemspec
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
require File.expand_path('../lib/csv_to_object/version', __FILE__)
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = ["Ian Whitney"]
|
6
|
-
gem.email = ["
|
5
|
+
gem.authors = ["Ian Whitney", "Davin Lagerroos"]
|
6
|
+
gem.email = ["iwhitney@ssa-i.org", "dlagerroos@ssa-i.org"]
|
7
7
|
gem.description = %q{Takes a CSV file and returns a a collection of objects generated by that CSV file.}
|
8
8
|
gem.summary = %q{Takes a CSV file and returns a a collection of objects generated by that CSV file.}
|
9
9
|
gem.homepage = "https://github.com/SeniorServiceAmerica/csv_to_object"
|
data/lib/csv_to_object.rb
CHANGED
@@ -6,30 +6,36 @@ module CsvToObject
|
|
6
6
|
|
7
7
|
# Requires the path to the csv file.
|
8
8
|
def initialize(input_path)
|
9
|
-
@
|
9
|
+
@input_path = File.open(input_path)
|
10
10
|
end
|
11
11
|
|
12
12
|
# Converts the data lines of the csv file to objects.
|
13
13
|
# Objects have the class of the name of the csv file.
|
14
14
|
# The first line of the csv file defines the attribute names for the data lines.
|
15
|
-
#
|
16
15
|
# person.csv => [person objects]
|
16
|
+
# attribute names are downcased and have spaces replaced with _.
|
17
|
+
# attribute names are strings.
|
17
18
|
def to_objects
|
18
19
|
objects = []
|
19
|
-
|
20
|
+
file = File.open(@input_path)
|
21
|
+
CSV.table(file, {header_converters: header_converter}).each do |row|
|
20
22
|
objects << new_object(row.to_hash)
|
21
23
|
end
|
22
24
|
objects
|
23
25
|
end
|
24
26
|
|
25
27
|
private
|
26
|
-
|
28
|
+
|
29
|
+
def header_converter
|
30
|
+
lambda { |h| h.encode(CSV::ConverterEncoding).downcase.gsub(/\s+/, "_") }
|
31
|
+
end
|
32
|
+
|
27
33
|
def new_object(attrs)
|
28
34
|
@object ? @object.new(attrs) : object_to_create().new(attrs)
|
29
35
|
end
|
30
36
|
|
31
37
|
def object_to_create()
|
32
|
-
class_name = File.basename(@
|
38
|
+
class_name = File.basename(@input_path).gsub('.csv','').capitalize
|
33
39
|
@object = Object::const_get(class_name)
|
34
40
|
end
|
35
41
|
end
|
data/spec/csv_to_object_spec.rb
CHANGED
@@ -7,6 +7,10 @@ describe CsvToObject do
|
|
7
7
|
@test_csv_path = 'test/csv/person.csv'
|
8
8
|
@source_file = File.open(@test_csv_path)
|
9
9
|
@c2o = CsvToObject::CsvToObject.new(@test_csv_path)
|
10
|
+
@number_of_data_lines = (@source_file.count - 1)
|
11
|
+
@source_file.rewind
|
12
|
+
@attribute_names = @source_file.first
|
13
|
+
@source_file.rewind
|
10
14
|
end
|
11
15
|
|
12
16
|
it "returns an array" do
|
@@ -18,11 +22,11 @@ describe CsvToObject do
|
|
18
22
|
end
|
19
23
|
|
20
24
|
it "returns as many objects as there are data lines in the csv file" do
|
21
|
-
@c2o.to_objects.count.should ==
|
25
|
+
@c2o.to_objects.count.should == @number_of_data_lines
|
22
26
|
end
|
23
27
|
|
24
28
|
it "instantiate objects defined by its source file name, using the data line as parameters" do
|
25
|
-
expected_paramaters = CSV.table(File.open(@test_csv_path))
|
29
|
+
expected_paramaters = CSV.table(File.open(@test_csv_path),{header_converters: lambda { |h| h.encode(CSV::ConverterEncoding).downcase.gsub(/\s+/, "_") }})
|
26
30
|
expected_paramaters.each do |x|
|
27
31
|
@c2o.should_receive(:new_object).with(x.to_hash)
|
28
32
|
end
|
@@ -31,7 +35,20 @@ describe CsvToObject do
|
|
31
35
|
|
32
36
|
it "should use the data line to populate attributes" do
|
33
37
|
@c2o.to_objects.first.id.should == 0
|
34
|
-
@c2o.to_objects.first.first_name.should == "Lindsey"
|
38
|
+
@c2o.to_objects.first.first_name.should == "Billie Lindsey"
|
35
39
|
@c2o.to_objects.first.last_name.should == "Austino"
|
36
40
|
end
|
41
|
+
|
42
|
+
it "replaces spaces in attribute names with underscores" do
|
43
|
+
@attribute_names.include?('last name').should == true
|
44
|
+
@c2o.should_receive(:new_object).with(include_key('last_name')).exactly(@number_of_data_lines).times
|
45
|
+
@c2o.to_objects
|
46
|
+
end
|
47
|
+
|
48
|
+
it "downcases attribute names and does not remove non-word characters" do
|
49
|
+
@attribute_names.include?('SCORE_[person_id|date]').should == true
|
50
|
+
@c2o.should_receive(:new_object).with(include_key('score_[person_id|date]')).exactly(@number_of_data_lines).times
|
51
|
+
@c2o.to_objects
|
52
|
+
end
|
53
|
+
|
37
54
|
end
|
data/spec/spec_helper.rb
CHANGED
data/test/csv/person.csv
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
id,first_name,
|
2
|
-
0,Lindsey,Austino
|
3
|
-
1,Dodie,Egnor
|
4
|
-
2,Tommie,Mclauglin
|
5
|
-
3,Aletha,Vettel
|
6
|
-
4,Matilda,Osornio
|
7
|
-
5,Robby,Kloke
|
8
|
-
6,Forest,Neall
|
9
|
-
7,Sherrie,Licon
|
10
|
-
8,Elroy,Bergren
|
11
|
-
9,Darlene,Guialdo
|
1
|
+
id,first_name,last name,SCORE_[person_id|date]
|
2
|
+
0,Billie Lindsey,Austino,[0|2012-01-01]
|
3
|
+
1,Dodie,Egnor,[0|2012-01-01]
|
4
|
+
2,Tommie,Mclauglin,[0|2012-01-01]
|
5
|
+
3,Aletha,Vettel,[0|2012-01-01]
|
6
|
+
4,Matilda,Osornio,[0|2012-01-01]
|
7
|
+
5,Robby,Kloke,[0|2012-01-01]
|
8
|
+
6,Forest,Neall,[0|2012-01-01]
|
9
|
+
7,Sherrie,Licon,[0|2012-01-01]
|
10
|
+
8,Elroy,Bergren,[0|2012-01-01]
|
11
|
+
9,Darlene,Guialdo,[0|2012-01-01]
|
data/test/lib/person.rb
CHANGED
@@ -2,8 +2,8 @@ class Person
|
|
2
2
|
attr_reader :id, :first_name, :last_name
|
3
3
|
|
4
4
|
def initialize(args={})
|
5
|
-
@id = args[
|
6
|
-
@first_name = args[
|
7
|
-
@last_name = args[
|
5
|
+
@id = args["id"]
|
6
|
+
@first_name = args["first_name"]
|
7
|
+
@last_name = args["last_name"]
|
8
8
|
end
|
9
9
|
end
|
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv_to_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ian Whitney
|
9
|
+
- Davin Lagerroos
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
13
|
+
date: 2012-08-31 00:00:00.000000000Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rspec
|
16
|
-
requirement: &
|
17
|
+
requirement: &2164666320 !ruby/object:Gem::Requirement
|
17
18
|
none: false
|
18
19
|
requirements:
|
19
20
|
- - ! '>='
|
@@ -21,11 +22,12 @@ dependencies:
|
|
21
22
|
version: '0'
|
22
23
|
type: :development
|
23
24
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
+
version_requirements: *2164666320
|
25
26
|
description: Takes a CSV file and returns a a collection of objects generated by that
|
26
27
|
CSV file.
|
27
28
|
email:
|
28
|
-
-
|
29
|
+
- iwhitney@ssa-i.org
|
30
|
+
- dlagerroos@ssa-i.org
|
29
31
|
executables: []
|
30
32
|
extensions: []
|
31
33
|
extra_rdoc_files: []
|