csv_to_object 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # CsvToObject
2
2
 
3
- Given the path to a CSV file, will return a collection of objects. The csv file name must match the name of an object the gem can instantiate. The first line of the csv file must define attribute names. The following lines contain the data that will be assigned to those attributes
3
+ Given the path to a CSV file, will return a collection of objects. The csv file name must match the name of an object the gem can instantiate. The first line of the csv file must define attribute names. The following lines contain the data that will be assigned to those attributes.
4
4
 
5
5
  ## Installation
6
6
 
@@ -26,29 +26,26 @@ Given a file person.csv with the contents
26
26
 
27
27
  ```ruby
28
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">]
29
+ CsvToObject::CsvToObject.new(csv.path).to_objects =>[#<Person:0x000001021a8148 @id=0, @first_name="Lindsey", @last_name="Austino">, #<Person:0x000001021a8030 @id=1, @first_name="Dodie", @last_name="Egnor">]
32
30
  ```
33
31
 
34
32
  ### Attribute Names
35
33
 
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:
34
+ CSV_to_object does not use the default header converter options provided by Ruby's CSV module. When converting the first line of the csv file into attribute names, the following conversions are applied:
37
35
 
38
36
  * Encoded using CSV::CharacterEncoding
39
37
  * Downcased
40
38
  * Spaces are replaced with underscores
41
39
 
40
+ If this behavior is not desired, overwrite the `header_converter` method of `CsvToObject`.
41
+
42
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
43
 
44
44
  ```ruby
45
45
  Person.new({"id" => 0, "first_name" => "Lindsey", "last_name" => "Austino"})
46
46
  ```
47
-
48
47
  So if you are looking for specific hash keys in `initialize()`, use strings not symbols.
49
48
 
50
- If this behavior is not desired, you could overwrite the `header_converter` method of `CsvToObject`.
51
-
52
49
  ## Contributing
53
50
 
54
51
  1. Fork it
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Ian Whitney", "Davin Lagerroos"]
6
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
- gem.summary = %q{Takes a CSV file and returns a a collection of objects generated by that CSV file.}
8
+ gem.summary = %q{Given the path to a CSV file, will return a collection of objects. The csv file name must match the name of an object the gem can instantiate. The first line of the csv file must define attribute names. The following lines contain the data that will be assigned to those attributes.}
9
9
  gem.homepage = "https://github.com/SeniorServiceAmerica/csv_to_object"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
@@ -1,3 +1,3 @@
1
1
  module CsvToObject
2
- VERSION = "0.0.3"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/csv_to_object.rb CHANGED
@@ -14,7 +14,7 @@ module CsvToObject
14
14
  # The first line of the csv file defines the attribute names for the data lines.
15
15
  # person.csv => [person objects]
16
16
  # attribute names are downcased and have spaces replaced with _.
17
- # attribute names are strings.
17
+ # attribute names are strings.
18
18
  def to_objects
19
19
  objects = []
20
20
  file = File.open(@input_path)
@@ -1,8 +1,14 @@
1
1
  require 'spec_helper'
2
- require_relative '../test/lib/person.rb'
3
2
 
4
- describe CsvToObject do
3
+ class Person
4
+ attr_reader :id, :first_name, :last_name
5
+
6
+ def initialize(args={})
7
+ @id,@first_name, @last_name = args["id"], args["first_name"],args["last_name"]
8
+ end
9
+ end
5
10
 
11
+ describe CsvToObject do
6
12
  before(:each) do
7
13
  @test_csv_path = 'test/csv/person.csv'
8
14
  @source_file = File.open(@test_csv_path)
@@ -27,8 +33,8 @@ describe CsvToObject do
27
33
 
28
34
  it "instantiate objects defined by its source file name, using the data line as parameters" do
29
35
  expected_paramaters = CSV.table(File.open(@test_csv_path),{header_converters: lambda { |h| h.encode(CSV::ConverterEncoding).downcase.gsub(/\s+/, "_") }})
30
- expected_paramaters.each do |x|
31
- @c2o.should_receive(:new_object).with(x.to_hash)
36
+ expected_paramaters.each do |object_attributes|
37
+ @c2o.should_receive(:new_object).with(object_attributes.to_hash)
32
38
  end
33
39
  @c2o.to_objects
34
40
  end
@@ -50,5 +56,4 @@ describe CsvToObject do
50
56
  @c2o.should_receive(:new_object).with(include_key('score_[person_id|date]')).exactly(@number_of_data_lines).times
51
57
  @c2o.to_objects
52
58
  end
53
-
54
- end
59
+ end
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: 0.0.3
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-31 00:00:00.000000000Z
13
+ date: 2012-09-05 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
17
- requirement: &2164666320 !ruby/object:Gem::Requirement
17
+ requirement: &2156341180 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *2164666320
25
+ version_requirements: *2156341180
26
26
  description: Takes a CSV file and returns a a collection of objects generated by that
27
27
  CSV file.
28
28
  email:
@@ -44,7 +44,6 @@ files:
44
44
  - spec/csv_to_object_spec.rb
45
45
  - spec/spec_helper.rb
46
46
  - test/csv/person.csv
47
- - test/lib/person.rb
48
47
  homepage: https://github.com/SeniorServiceAmerica/csv_to_object
49
48
  licenses: []
50
49
  post_install_message:
@@ -68,10 +67,11 @@ rubyforge_project:
68
67
  rubygems_version: 1.8.16
69
68
  signing_key:
70
69
  specification_version: 3
71
- summary: Takes a CSV file and returns a a collection of objects generated by that
72
- CSV file.
70
+ summary: Given the path to a CSV file, will return a collection of objects. The csv
71
+ file name must match the name of an object the gem can instantiate. The first line
72
+ of the csv file must define attribute names. The following lines contain the data
73
+ that will be assigned to those attributes.
73
74
  test_files:
74
75
  - spec/csv_to_object_spec.rb
75
76
  - spec/spec_helper.rb
76
77
  - test/csv/person.csv
77
- - test/lib/person.rb
data/test/lib/person.rb DELETED
@@ -1,9 +0,0 @@
1
- class Person
2
- attr_reader :id, :first_name, :last_name
3
-
4
- def initialize(args={})
5
- @id = args["id"]
6
- @first_name = args["first_name"]
7
- @last_name = args["last_name"]
8
- end
9
- end