plus2_seeder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/plus2_seeder.rb +4 -0
- data/lib/plus2_seeder/conductor.rb +63 -0
- data/lib/plus2_seeder/seeder/base.rb +62 -0
- data/lib/plus2_seeder/seeder/spreadsheet.rb +103 -0
- data/lib/plus2_seeder/version.rb +3 -0
- data/plus2_seeder.gemspec +17 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 Ben Askins
|
|
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,29 @@
|
|
|
1
|
+
# Plus2Seeder
|
|
2
|
+
|
|
3
|
+
Seed database with data
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'plus2_seeder'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install plus2_seeder
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
rake seed
|
|
22
|
+
|
|
23
|
+
## Contributing
|
|
24
|
+
|
|
25
|
+
1. Fork it
|
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/plus2_seeder.rb
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module Plus2Seeder
|
|
2
|
+
class Conductor
|
|
3
|
+
# Require all of the seeders
|
|
4
|
+
Dir[File.expand_path('lib/seeder/seeders/*.rb')].each { |file| require file }
|
|
5
|
+
|
|
6
|
+
@recorded = []
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def self.seeders=(val)
|
|
10
|
+
@seeders = val
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.seeders
|
|
14
|
+
@seeders
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.all_seeders
|
|
18
|
+
@seeders.values.flatten.uniq
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def self.record(seeder_name)
|
|
23
|
+
@recorded << seeder_name
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Runs a named seeder
|
|
28
|
+
def self.run(seeder_name)
|
|
29
|
+
return if @recorded.include? seeder_name
|
|
30
|
+
|
|
31
|
+
klass = "#{seeder_name}_seeder".classify.constantize
|
|
32
|
+
seeder = klass.new
|
|
33
|
+
|
|
34
|
+
seeder.run_dependencies
|
|
35
|
+
|
|
36
|
+
seeder.reset if reset? # Call the seeder's reset method if called with RESET=true
|
|
37
|
+
|
|
38
|
+
seeder.before
|
|
39
|
+
|
|
40
|
+
puts "Seeding #{seeder_name.pluralize}"
|
|
41
|
+
seeder.seed
|
|
42
|
+
|
|
43
|
+
record seeder_name
|
|
44
|
+
|
|
45
|
+
seeder.after
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# Will run all seeders
|
|
50
|
+
def seed
|
|
51
|
+
self.class.seeders[Rails.env].each do |seeder|
|
|
52
|
+
self.class.run(seeder)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def self.reset?
|
|
58
|
+
@reset ||= (ENV['RESET'] == 'true')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module Plus2Seeder
|
|
2
|
+
|
|
3
|
+
module Seeder
|
|
4
|
+
|
|
5
|
+
class Base
|
|
6
|
+
|
|
7
|
+
# These seeders will be run before the subclass's seeder
|
|
8
|
+
def self.dependencies(dependency_list=nil)
|
|
9
|
+
@dependencies ||= dependency_list
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def run(seeder_name)
|
|
14
|
+
Plus2Seeder.run(seeder_name)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def run_dependencies
|
|
19
|
+
if self.class.dependencies.try(:any?)
|
|
20
|
+
self.class.dependencies.each do |seeder_name|
|
|
21
|
+
run(seeder_name)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Determines the model class to use based on the name of the seeder
|
|
28
|
+
def creator_class
|
|
29
|
+
@creator_class ||= self.class.name.gsub('Seeder', '').constantize
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def reset
|
|
34
|
+
puts "Clearing #{creator_class.table_name}"
|
|
35
|
+
creator_class.delete_all
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def before
|
|
40
|
+
#override if you want to do something before seeding
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def after
|
|
45
|
+
#override if you want to do something after seeding
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def debug?
|
|
50
|
+
@debug ||= (ENV['DEBUG'] == 'true')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def debug(val)
|
|
55
|
+
ap val if debug?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Superclass to be used for seeders that import data from a spreadsheet.
|
|
2
|
+
# The subclass needs to specify the source file, sheet, and column names.
|
|
3
|
+
# The subclass can optionally implement a pre_process and can_import? method.
|
|
4
|
+
#
|
|
5
|
+
# e.g.
|
|
6
|
+
#
|
|
7
|
+
# class PersonSeeder < SpreadsheetSeeder
|
|
8
|
+
# source 'people.xls'
|
|
9
|
+
# sheet 'People'
|
|
10
|
+
# columns %w(name email phone)
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# PersonSeeder.new.seed
|
|
14
|
+
#
|
|
15
|
+
# Will create instances of Person from the data in the People sheet of db/seeds/people.xls.
|
|
16
|
+
module Plus2Seeder
|
|
17
|
+
module Seeder
|
|
18
|
+
|
|
19
|
+
class Spreadsheet < Base
|
|
20
|
+
|
|
21
|
+
# Used to specify the spreadsheet to import. Assumed to be in db/seeds
|
|
22
|
+
def self.source(*filenames)
|
|
23
|
+
@filenames ||= filenames
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# The sheet name to import from
|
|
28
|
+
def self.sheet(sheetname=nil)
|
|
29
|
+
@sheetname ||= sheetname
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# The names of the columns - use the active record attribute names.
|
|
34
|
+
# These will be zipped up with the row values by row_to_hash to create an attributes hash that
|
|
35
|
+
# can be used to create a new ActiveRecord instance.
|
|
36
|
+
#
|
|
37
|
+
# To skip a column use '_', it will be deleted from the resulting hash
|
|
38
|
+
def self.columns(columns=nil)
|
|
39
|
+
@columns ||= columns
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# Skip (n) rows before starting to import
|
|
44
|
+
def self.skip_rows(rows=nil)
|
|
45
|
+
@skip_rows ||= rows || 0
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
# Opens the spreadsheet and calls import for each row
|
|
50
|
+
def seed
|
|
51
|
+
self.class.source.each do |source|
|
|
52
|
+
book = ::Spreadsheet.open "./db/seeds/#{source}"
|
|
53
|
+
|
|
54
|
+
sheet = book.worksheet self.class.sheet
|
|
55
|
+
|
|
56
|
+
sheet.each self.class.skip_rows do |row|
|
|
57
|
+
import(row_to_hash(row))
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Creates a hash from the row values and column names
|
|
64
|
+
def row_to_hash(row)
|
|
65
|
+
Hash[self.class.columns.zip(row)].tap do |h|
|
|
66
|
+
pre_process(h, row) if respond_to?(:pre_process)
|
|
67
|
+
|
|
68
|
+
# Delete skipped columns
|
|
69
|
+
h.delete('_')
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# Imports the row
|
|
75
|
+
def import(row)
|
|
76
|
+
if can_import?(row)
|
|
77
|
+
if object = object_available_to_update(row)
|
|
78
|
+
debug("Updating #{object}")
|
|
79
|
+
object.update_attributes(row)
|
|
80
|
+
else
|
|
81
|
+
debug("Creating new #{creator_class.name}")
|
|
82
|
+
creator_class.create!(row)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# Determines whether or not the row can be imported. Override this in your subclass if desired
|
|
89
|
+
def can_import?(row)
|
|
90
|
+
true
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# Override this in your subclass if you wish to update existing data from your seeder. It will need to return
|
|
95
|
+
# an instance of the AR object to be updated
|
|
96
|
+
def object_available_to_update(row)
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/plus2_seeder/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["Ben Askins"]
|
|
6
|
+
gem.email = ["ben.askins@gmail.com"]
|
|
7
|
+
gem.description = %q{database seeder}
|
|
8
|
+
gem.summary = %q{database seeder}
|
|
9
|
+
gem.homepage = ""
|
|
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 = "plus2_seeder"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = Plus2Seeder::VERSION
|
|
17
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: plus2_seeder
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 29
|
|
5
|
+
prerelease:
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 0
|
|
9
|
+
- 1
|
|
10
|
+
version: 0.0.1
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Ben Askins
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2012-04-24 00:00:00 Z
|
|
19
|
+
dependencies: []
|
|
20
|
+
|
|
21
|
+
description: database seeder
|
|
22
|
+
email:
|
|
23
|
+
- ben.askins@gmail.com
|
|
24
|
+
executables: []
|
|
25
|
+
|
|
26
|
+
extensions: []
|
|
27
|
+
|
|
28
|
+
extra_rdoc_files: []
|
|
29
|
+
|
|
30
|
+
files:
|
|
31
|
+
- .gitignore
|
|
32
|
+
- Gemfile
|
|
33
|
+
- LICENSE
|
|
34
|
+
- README.md
|
|
35
|
+
- Rakefile
|
|
36
|
+
- lib/plus2_seeder.rb
|
|
37
|
+
- lib/plus2_seeder/conductor.rb
|
|
38
|
+
- lib/plus2_seeder/seeder/base.rb
|
|
39
|
+
- lib/plus2_seeder/seeder/spreadsheet.rb
|
|
40
|
+
- lib/plus2_seeder/version.rb
|
|
41
|
+
- plus2_seeder.gemspec
|
|
42
|
+
homepage: ""
|
|
43
|
+
licenses: []
|
|
44
|
+
|
|
45
|
+
post_install_message:
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
none: false
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
hash: 3
|
|
56
|
+
segments:
|
|
57
|
+
- 0
|
|
58
|
+
version: "0"
|
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
none: false
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
hash: 3
|
|
65
|
+
segments:
|
|
66
|
+
- 0
|
|
67
|
+
version: "0"
|
|
68
|
+
requirements: []
|
|
69
|
+
|
|
70
|
+
rubyforge_project:
|
|
71
|
+
rubygems_version: 1.8.10
|
|
72
|
+
signing_key:
|
|
73
|
+
specification_version: 3
|
|
74
|
+
summary: database seeder
|
|
75
|
+
test_files: []
|
|
76
|
+
|