csv_to_object 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csv_to_object.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ian Whitney
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # CsvToObject
2
+
3
+ Given 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
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'csv_to_object'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install csv_to_object
18
+
19
+ ## Usage
20
+
21
+ Given a file person.csv with the contents
22
+
23
+ `id,first_name,last_name`
24
+ `0,Lindsey,Austino`
25
+ `1,Dodie,Egnor`
26
+
27
+ `CsvToObject::CsvToObject.new(File.open('person.csv')).to_objects` will return
28
+
29
+ `[#<Person:0x000001021a8148 @id=0, @first_name="Lindsey", @last_name="Austino">, #<Person:0x000001021a8030 @id=1, @first_name="Dodie", @last_name="Egnor">`]
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+
7
+ # If you want to make this the default task
8
+ task :default => :spec
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/csv_to_object/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ian Whitney"]
6
+ gem.email = ["ian@ianwhitney.com"]
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.}
9
+ gem.homepage = "https://github.com/SeniorServiceAmerica/csv_to_object"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "csv_to_object"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = CsvToObject::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ end
@@ -0,0 +1,3 @@
1
+ module CsvToObject
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ require "csv_to_object/version"
2
+ require 'CSV'
3
+
4
+ module CsvToObject
5
+ class CsvToObject
6
+
7
+ def initialize(input_path)
8
+ @input = File.open(input_path)
9
+ end
10
+
11
+ def to_objects
12
+ objects = []
13
+ CSV.table(@input).each do |row|
14
+ objects << new_object(row.to_hash)
15
+ end
16
+ objects
17
+ end
18
+
19
+ private
20
+
21
+ def new_object(attrs)
22
+ @object ? @object.new(attrs) : object_to_create().new(attrs)
23
+ end
24
+
25
+ def object_to_create()
26
+ class_name = File.basename(@input.path).gsub('.csv','').capitalize
27
+ @object = Object::const_get(class_name)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+ require_relative '../test/lib/person.rb'
3
+
4
+ describe CsvToObject do
5
+
6
+ before(:each) do
7
+ @test_csv_path = 'test/csv/person.csv'
8
+ @source_file = File.open(@test_csv_path)
9
+ @c2o = CsvToObject::CsvToObject.new(@test_csv_path)
10
+ end
11
+
12
+ it "returns an array" do
13
+ @c2o.to_objects.class.should == [].class
14
+ end
15
+
16
+ it "populates the objects array with objects defined by the source file name" do
17
+ @c2o.to_objects.first.class.to_s.should == File.basename(@source_file.path).gsub('.csv','').capitalize
18
+ end
19
+
20
+ it "returns as many objects as there are data lines in the csv file" do
21
+ @c2o.to_objects.count.should == (@source_file.count - 1)
22
+ end
23
+
24
+ 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))
26
+ expected_paramaters.each do |x|
27
+ @c2o.should_receive(:new_object).with(x.to_hash)
28
+ end
29
+ @c2o.to_objects
30
+ end
31
+
32
+ it "should use the data line to populate attributes" do
33
+ @c2o.to_objects.first.id.should == 0
34
+ @c2o.to_objects.first.first_name.should == "Lindsey"
35
+ @c2o.to_objects.first.last_name.should == "Austino"
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ require 'csv_to_object'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
@@ -0,0 +1,11 @@
1
+ id,first_name,last_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
@@ -0,0 +1,9 @@
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
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_to_object
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ian Whitney
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2168463560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2168463560
25
+ description: Takes a CSV file and returns a a collection of objects generated by that
26
+ CSV file.
27
+ email:
28
+ - ian@ianwhitney.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .rspec
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - csv_to_object.gemspec
40
+ - lib/csv_to_object.rb
41
+ - lib/csv_to_object/version.rb
42
+ - spec/csv_to_object_spec.rb
43
+ - spec/spec_helper.rb
44
+ - test/csv/person.csv
45
+ - test/lib/person.rb
46
+ homepage: https://github.com/SeniorServiceAmerica/csv_to_object
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.16
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Takes a CSV file and returns a a collection of objects generated by that
70
+ CSV file.
71
+ test_files:
72
+ - spec/csv_to_object_spec.rb
73
+ - spec/spec_helper.rb
74
+ - test/csv/person.csv
75
+ - test/lib/person.rb