seedsv 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +43 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/seedsv/csv_seed.rb +49 -0
- data/lib/seedsv/engine.rb +10 -0
- data/lib/seedsv.rb +33 -0
- data/lib/tasks/seedsv.rake +28 -0
- data/seedsv.gemspec +60 -0
- data/test/helper.rb +10 -0
- data/test/test_seedssv.rb +7 -0
- metadata +110 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Innku
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= Seedsv
|
2
|
+
|
3
|
+
Seed your Rails 3 models using CSV files.
|
4
|
+
|
5
|
+
== Install it
|
6
|
+
|
7
|
+
In your gem file:
|
8
|
+
|
9
|
+
gem 'seedsv'
|
10
|
+
|
11
|
+
== Using it
|
12
|
+
|
13
|
+
Seedsv requires the developer to create the folder:
|
14
|
+
|
15
|
+
Rails.root/db/csv
|
16
|
+
|
17
|
+
In this folder you will place all your CSV files with the same name as your database tables. The format of the first line of each file is the following:
|
18
|
+
|
19
|
+
column_name_1, column_name_2, column_name_3...
|
20
|
+
|
21
|
+
So for example, to seed the model City, you would normally have the file: /db/csv/cities.csv
|
22
|
+
|
23
|
+
This is the example content of the file
|
24
|
+
|
25
|
+
name, state_id
|
26
|
+
Monterrey, 1
|
27
|
+
Guadalajara, 2
|
28
|
+
|
29
|
+
To seed the database with this file
|
30
|
+
|
31
|
+
rake db:seed:csv
|
32
|
+
|
33
|
+
If something goes wrong and you want to delete everything seeded from CSV files:
|
34
|
+
|
35
|
+
rake db:seed:cleanup
|
36
|
+
|
37
|
+
== RDoc
|
38
|
+
|
39
|
+
http://rdoc.info/github/adriancuadros/seedsv/master/frames
|
40
|
+
|
41
|
+
== Copyright
|
42
|
+
|
43
|
+
Copyright (c) 2010 Innku. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "seedsv"
|
8
|
+
gem.summary = %Q{Seed your database with data from csv files with ease}
|
9
|
+
gem.description = %Q{Seed your database with data from csv files with ease}
|
10
|
+
gem.email = "adrian@innku.com"
|
11
|
+
gem.homepage = "http://github.com/adriancuadros/seedsv"
|
12
|
+
gem.authors = ["Adrian Cuadros"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
gem.add_dependency("fastercsv", "~> 1.5.3")
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "seedssv #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.1
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Seedsv
|
2
|
+
# The CSV seed module contains all the logic inside the rake tasks
|
3
|
+
module CsvSeed
|
4
|
+
|
5
|
+
mattr_accessor :csv_class
|
6
|
+
|
7
|
+
#Use appropriate ruby library
|
8
|
+
if VERSION.include?('1.9')
|
9
|
+
require 'csv'
|
10
|
+
@@csv_class = CSV
|
11
|
+
else
|
12
|
+
require 'fastercsv'
|
13
|
+
@@csv_class = FasterCSV
|
14
|
+
end
|
15
|
+
|
16
|
+
# Receives the reference to the model class to seed.
|
17
|
+
# Seeds only when there are no records in the table
|
18
|
+
# or if the +force+ option is set to true.
|
19
|
+
# Also optionally it can receive the +file_name+ to use.
|
20
|
+
def seed_model(model_class, options={})
|
21
|
+
if model_class.count == 0 or options[:force]
|
22
|
+
table_name = options[:file_name] || model_class.table_name
|
23
|
+
puts "Seeding #{model_class.to_s.pluralize}..."
|
24
|
+
csv_file = @@csv_class.open(Rails.root + "db/csv/#{table_name}.csv", :headers => true)
|
25
|
+
seed_from_csv(model_class, csv_file)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Receives the class reference and csv file and creates all thre records
|
32
|
+
# inside one transaction
|
33
|
+
def seed_from_csv(migration_class, csv_file)
|
34
|
+
migration_class.transaction do
|
35
|
+
csv_file.each do |line_values|
|
36
|
+
record = migration_class.new
|
37
|
+
line_values.to_hash.keys.map{|attribute| record.send("#{attribute.strip}=",line_values[attribute].strip) rescue nil}
|
38
|
+
record.save(:validate => false)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Enforces utf8 as the encoding
|
44
|
+
def utf8(string)
|
45
|
+
string.force_encoding('utf-8') unless string.nil?
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
data/lib/seedsv.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# SeedSV provides the functionality to seed your database CSV Files
|
2
|
+
#
|
3
|
+
# Author:: Adrian Cuadros
|
4
|
+
# Copyright:: Copyright (c) 2010 Innku
|
5
|
+
# License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
|
6
|
+
#
|
7
|
+
# Seedsv requires the developer to create the folder csv inside Rails.root/db/
|
8
|
+
#
|
9
|
+
# In this folder you will place all your csv files with the same name as your
|
10
|
+
# database tables. The format of the first line of each file is the following:
|
11
|
+
#
|
12
|
+
# column_name_1, column_name_2, column_name_3...
|
13
|
+
#
|
14
|
+
# So for example, to seed the model City, you would normally have the file:
|
15
|
+
# /db/csv/cities.csv
|
16
|
+
#
|
17
|
+
# This is the example content of the file
|
18
|
+
#
|
19
|
+
# name, state_id
|
20
|
+
# Monterrey, 1
|
21
|
+
# Guadalajara, 2
|
22
|
+
#
|
23
|
+
# To seed the databse with this file
|
24
|
+
# rake db:seed:csv
|
25
|
+
#
|
26
|
+
# If something goes wrong and you want to delete everything seeded from csv files:
|
27
|
+
# rake db:seed:cleanup
|
28
|
+
module Seedsv
|
29
|
+
if defined?(Rails) && Rails::VERSION::MAJOR == 3
|
30
|
+
require 'seedsv/csv_seed'
|
31
|
+
require 'seedsv/engine'
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
namespace :db do
|
2
|
+
namespace :seed do
|
3
|
+
include Seedsv::CsvSeed
|
4
|
+
desc 'Seeds your database with csv files under db/csv/*'
|
5
|
+
task :csv => :environment do
|
6
|
+
Dir.foreach(Rails.root + 'db/csv') do |file|
|
7
|
+
unless File.directory?(file) or File.extname(file) != '.csv'
|
8
|
+
begin
|
9
|
+
model_class = eval(file.split('.').first.classify)
|
10
|
+
seed_model(model_class) if model_class
|
11
|
+
rescue
|
12
|
+
puts "#{file.split('.').first.classify} class not found, skiping..."
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Cleans up the database if you messed up your seed"
|
19
|
+
task :cleanup => :environment do
|
20
|
+
Dir.foreach(Rails.root + 'db/csv') do |file|
|
21
|
+
unless File.directory?(file)
|
22
|
+
eval(file.split('.').first.classify).destroy_all
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/seedsv.gemspec
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{seedsv}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Adrian Cuadros"]
|
12
|
+
s.date = %q{2010-09-23}
|
13
|
+
s.description = %q{Seed your database with data from csv files with ease}
|
14
|
+
s.email = %q{adrian@innku.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/seedsv.rb",
|
27
|
+
"lib/seedsv/csv_seed.rb",
|
28
|
+
"lib/seedsv/engine.rb",
|
29
|
+
"lib/tasks/seedsv.rake",
|
30
|
+
"seedsv.gemspec",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_seedssv.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/adriancuadros/seedsv}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{Seed your database with data from csv files with ease}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_seedssv.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
50
|
+
s.add_runtime_dependency(%q<fastercsv>, ["~> 1.5.3"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
s.add_dependency(%q<fastercsv>, ["~> 1.5.3"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
57
|
+
s.add_dependency(%q<fastercsv>, ["~> 1.5.3"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seedsv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Adrian Cuadros
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-23 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: thoughtbot-shoulda
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: fastercsv
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 5
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 5
|
47
|
+
- 3
|
48
|
+
version: 1.5.3
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
description: Seed your database with data from csv files with ease
|
52
|
+
email: adrian@innku.com
|
53
|
+
executables: []
|
54
|
+
|
55
|
+
extensions: []
|
56
|
+
|
57
|
+
extra_rdoc_files:
|
58
|
+
- LICENSE
|
59
|
+
- README.rdoc
|
60
|
+
files:
|
61
|
+
- .document
|
62
|
+
- .gitignore
|
63
|
+
- LICENSE
|
64
|
+
- README.rdoc
|
65
|
+
- Rakefile
|
66
|
+
- VERSION
|
67
|
+
- lib/seedsv.rb
|
68
|
+
- lib/seedsv/csv_seed.rb
|
69
|
+
- lib/seedsv/engine.rb
|
70
|
+
- lib/tasks/seedsv.rake
|
71
|
+
- seedsv.gemspec
|
72
|
+
- test/helper.rb
|
73
|
+
- test/test_seedssv.rb
|
74
|
+
has_rdoc: true
|
75
|
+
homepage: http://github.com/adriancuadros/seedsv
|
76
|
+
licenses: []
|
77
|
+
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options:
|
80
|
+
- --charset=UTF-8
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.7
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Seed your database with data from csv files with ease
|
108
|
+
test_files:
|
109
|
+
- test/helper.rb
|
110
|
+
- test/test_seedssv.rb
|