seedfile 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTg4ZTUwMWYwZmZmODgwOWU3ODc5ZTA5NzY4OTY1YzU3NDAzYTFkYQ==
5
+ data.tar.gz: !binary |-
6
+ YzAzNDYxNDM3MmUyYWNlNmJmMDRiZDUxYWJhOWM3NmY4ZGVkYzEwZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YmQ2NmEzNDMwMTBiZGMyMmRmMTBhZmE2NDg2OGVkOGUwYTE4YTlmMWJmOTUz
10
+ ZjRmMGM4NTNjMGIxODY5ZDhjMjFkZTA0NTNkYWI4OGIwZWMzNmJlYjRkYjZj
11
+ MGNiZDU0YzZhZDc2ZmVlMDI5YTYxOWNkM2FhNWNhMjM3NTdiMWI=
12
+ data.tar.gz: !binary |-
13
+ NWE4NTI3MzU1MjVkNmM3MWVhODVkZjU5NjFkODk5YTUwYzc4YzQwNzU4NWM4
14
+ NjkwNDI0ZmMwMzVjZWEwMTI2ZjI5ZGYzZWZhZjM3NWY5ZmJmNDBiMDExNzBi
15
+ MzE0OTRhNjkyZTUxMTY0YTM0YzhlNjU2OGY0ZDZiYzNlZmQwY2Y=
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 James Zhang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ Seedfile
2
+ =========
3
+
4
+ Create ActiveRecord objects using a CSV file.
5
+
6
+ ### Usage
7
+
8
+ As an example, let's use the following CSV file and `Car` class to seed the database. The first row of the CSV file must be the attribute names.
9
+
10
+ data.csv
11
+ ```
12
+ Brand,Model,Year,Price
13
+ BMW,3 Series,2014,32750
14
+ Mercedes-Benz,CLA-Class,2014,29990
15
+ Audi,A4,2014,33800
16
+ ```
17
+
18
+ car.rb
19
+ ```rb
20
+ class Car < ActiveRecord::Base
21
+ end
22
+ ```
23
+
24
+ Nothing in the database.
25
+ ```rb
26
+ >> Car.all
27
+ => #<ActiveRecord::Relation []>
28
+ ```
29
+
30
+ Create a new Seedfile instance. Pass in the path to the CSV and the model.
31
+ ```rb
32
+ >> data = Seedfile.new(path: 'data.csv', model: Car)
33
+ ```
34
+
35
+ Seed it.
36
+ ```
37
+ >> data.seed
38
+ ```
39
+
40
+ Database is populated with data from the file.
41
+ ```rb
42
+ >> Car.all
43
+ => #<ActiveRecord::Relation [#<Car id: 1, brand: "BMW", model: "3 Series", year: 2014, price: 32750>, #<Car id: 2, brand: "Mercedes-Benz", model: "CLA-Class", year: 2014, price: 29990>, #<Car id: 3, brand: "Audi", model: "A4", year: 2014, price: 33800>]>
44
+ ```
@@ -0,0 +1,30 @@
1
+ require 'active_record'
2
+ require 'csv'
3
+
4
+ class Seedfile
5
+
6
+ class FileNotFoundError < StandardError;end
7
+ class InvalidModelError < StandardError;end
8
+
9
+ attr_accessor :path, :model
10
+
11
+ def initialize(options)
12
+ @path = options[:path]
13
+ @model = options[:model]
14
+ check_for_errors
15
+ end
16
+
17
+ def seed
18
+ CSV.foreach(@path, headers: true, header_converters: :symbol) do |line|
19
+ @model.create(line.to_hash)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def check_for_errors
26
+ raise FileNotFoundError unless File.exist?(@path)
27
+ raise InvalidModelError unless @model.superclass == ActiveRecord::Base
28
+ end
29
+
30
+ end
@@ -0,0 +1,4 @@
1
+ Brand,Model,Year,Price
2
+ BMW,3 Series,2014,32750
3
+ Mercedes-Benz,CLA-Class,2014,29990
4
+ Audi,A4,2014,33800
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ class Car < ActiveRecord::Base
4
+ end
5
+
6
+ class InvalidCar;end
7
+
8
+ describe Seedfile do
9
+ before :each do
10
+ path = 'spec/data.csv'
11
+ model = Car
12
+ @seedfile = Seedfile.new(path: path, model: model)
13
+ end
14
+
15
+ it 'takes a path and model' do
16
+ expect(@seedfile.path).to eql 'spec/data.csv'
17
+ expect(@seedfile.model).to eql Car
18
+ end
19
+
20
+ it 'raises an error if file is not found' do
21
+ expect{ Seedfile.new(path: '', model: Car) }.to raise_error(Seedfile::FileNotFoundError)
22
+ end
23
+
24
+ it 'raises an error if model is not an ActiveRecord object' do
25
+ expect{ Seedfile.new(path: 'spec/data.csv', model: InvalidCar) }.to raise_error(Seedfile::InvalidModelError)
26
+ end
27
+
28
+ it 'creates ActiveRecord objects' do
29
+ expect(Car.all.length).to eql 0
30
+ @seedfile.seed
31
+ expect(Car.find(1).brand).to eql 'BMW'
32
+ expect(Car.find(1).model).to eql '3 Series'
33
+ expect(Car.find(1).year).to eql 2014
34
+ expect(Car.find(1).price.to_f).to eql 32750.0
35
+ expect(Car.find(2).brand).to eql 'Mercedes-Benz'
36
+ expect(Car.find(2).model).to eql 'CLA-Class'
37
+ expect(Car.find(2).year).to eql 2014
38
+ expect(Car.find(2).price.to_f).to eql 29990.0
39
+ expect(Car.find(3).brand).to eql 'Audi'
40
+ expect(Car.find(3).model).to eql 'A4'
41
+ expect(Car.find(3).year).to eql 2014
42
+ expect(Car.find(3).price.to_f).to eql 33800.0
43
+ end
44
+
45
+ end
@@ -0,0 +1,25 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+ require 'seedfile'
4
+
5
+ ActiveRecord::Base.establish_connection(
6
+ adapter: 'sqlite3',
7
+ database: ':memory:'
8
+ )
9
+
10
+ ActiveRecord::Migration.create_table :cars do |t|
11
+ t.string :brand
12
+ t.string :model
13
+ t.integer :year
14
+ t.decimal :price
15
+ t.timestamps
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ config.around do |example|
20
+ ActiveRecord::Base.transaction do
21
+ example.run
22
+ raise ActiveRecord::Rollback
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seedfile
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Zhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.14.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.8
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.8
55
+ description: Create ActiveRecord objects using a CSV file
56
+ email: jamesfzhang@icloud.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - LICENSE
63
+ - lib/seedfile.rb
64
+ - spec/data.csv
65
+ - spec/seedfile_spec.rb
66
+ - spec/spec_helper.rb
67
+ homepage: https://github.com/jamesfzhang/seedfile
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.1.10
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Seed ActiveRecord objects
91
+ test_files:
92
+ - spec/data.csv
93
+ - spec/seedfile_spec.rb
94
+ - spec/spec_helper.rb