seed_gimmick 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -2
- data/lib/generators/seed_gimmick/install_generator.rb +8 -3
- data/lib/generators/seed_gimmick/templates/seed_gimmick.yml +16 -0
- data/lib/seed_gimmick/inflector.rb +29 -14
- data/lib/seed_gimmick/options.rb +60 -4
- data/lib/seed_gimmick/seed.rb +77 -0
- data/lib/seed_gimmick/seed_io/base.rb +10 -0
- data/lib/seed_gimmick/seed_io/yaml_file.rb +14 -0
- data/lib/seed_gimmick/seed_io.rb +11 -5
- data/lib/seed_gimmick/version.rb +1 -1
- data/lib/seed_gimmick.rb +10 -10
- data/lib/tasks/seed_gimmick.rake +20 -3
- metadata +4 -5
- data/lib/generators/seed_gimmick/templates/seeds.rb +0 -3
- data/lib/seed_gimmick/finder.rb +0 -18
- data/lib/seed_gimmick/seed_file.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aceba72060bab98a41a0d8427ed09c9a1bf83338
|
4
|
+
data.tar.gz: 85ff18ec13553c2c3fd5fc6c36c3b3cafe0a6f66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48768e2ace6be4b85f2558ef2726e7ef867b9f956f4bfd78cacc665be3376ef1ac517f08cf01ee19768daf71def30a73fc9c6205a79f7c5658893e33662f5a2f
|
7
|
+
data.tar.gz: 84d28b1cb0489fc9dd8aa2fea8ce52ba7141bca70be3cca103f38983263d1a9d454b6c46e5ad7a8c2f40b86d2163ac8d18b04781c0ac09cddab3b4d95b77c730
|
data/README.md
CHANGED
@@ -16,8 +16,35 @@ And then execute:
|
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
20
|
-
|
19
|
+
### Generate configuration file and default seed directory
|
20
|
+
|
21
|
+
Run generator of Rails.
|
22
|
+
Configuration file created to `config/seed_gimmick.yml`.
|
23
|
+
|
24
|
+
```
|
25
|
+
$ bundle exec rails generate seed_gimmick:install
|
26
|
+
create db/seed_gimmick/.keep
|
27
|
+
create config/seed_gimmick.yml
|
28
|
+
```
|
29
|
+
|
30
|
+
### Rake tasks
|
31
|
+
|
32
|
+
#### Apply fixtures
|
33
|
+
|
34
|
+
Read fixtures from seed directory and apply to database.
|
35
|
+
All fixtures applied if not specified tables.
|
36
|
+
|
37
|
+
```
|
38
|
+
$ [TABLES=table_name,table_name] bundle exec rake db:seed_gimmick
|
39
|
+
```
|
40
|
+
|
41
|
+
#### Dump fixtures
|
42
|
+
|
43
|
+
Dump to fixture file from database in seed directory.
|
44
|
+
|
45
|
+
```
|
46
|
+
$ MODELS=ModelName,ModelName bundle exec rake db:seed_gimmick:dump
|
47
|
+
```
|
21
48
|
|
22
49
|
## Contributing
|
23
50
|
|
@@ -26,3 +53,4 @@ And then execute:
|
|
26
53
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
27
54
|
4. Push to the branch (`git push origin my-new-feature`)
|
28
55
|
5. Create a new Pull Request
|
56
|
+
|
@@ -3,9 +3,14 @@ module SeedGimmick
|
|
3
3
|
class InstallGenerator < ::Rails::Generators::Base
|
4
4
|
source_root File.expand_path(File.join(File.dirname(__FILE__), "templates"))
|
5
5
|
|
6
|
-
desc "
|
7
|
-
def
|
8
|
-
|
6
|
+
desc "Create seed_dir of SeedGimmick."
|
7
|
+
def create_seed_dir
|
8
|
+
create_file File.join("db", "seed_gimmick", ".keep")
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Create config file of SeedGimmick."
|
12
|
+
def create_config_file
|
13
|
+
template "seed_gimmick.yml", "config/seed_gimmick.yml"
|
9
14
|
end
|
10
15
|
end
|
11
16
|
end
|
@@ -2,33 +2,48 @@ module SeedGimmick
|
|
2
2
|
class Inflector
|
3
3
|
attr_reader :seed_dir
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
class << self
|
6
|
+
def build(options = nil)
|
7
|
+
new(options || Options.new)
|
8
|
+
end
|
9
|
+
|
10
|
+
def model_class(name)
|
11
|
+
name.try(:safe_constantize)
|
12
|
+
end
|
13
|
+
|
14
|
+
def pathname(path)
|
15
|
+
case path
|
16
|
+
when Pathname then path
|
17
|
+
when String then Pathname.new(path)
|
18
|
+
else nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(options)
|
24
|
+
@seed_dir = options.seed_dir
|
7
25
|
end
|
8
26
|
|
9
27
|
def model_for(seed_file)
|
10
|
-
relative_path(without_ext(seed_file)).to_s.classify
|
28
|
+
class_name = relative_path(without_ext(seed_file)).to_s.classify
|
29
|
+
self.class.model_class(class_name)
|
11
30
|
end
|
12
31
|
|
13
32
|
def seed_for(model, format = :yml)
|
14
|
-
seed_dir +
|
33
|
+
seed_dir + pathname(model.model_name.collection).sub_ext(".#{format}")
|
15
34
|
end
|
16
35
|
|
17
36
|
private
|
18
|
-
def without_ext(
|
19
|
-
|
37
|
+
def without_ext(path)
|
38
|
+
path.dirname + path.basename(".*")
|
20
39
|
end
|
21
40
|
|
22
|
-
def relative_path(
|
23
|
-
|
41
|
+
def relative_path(path)
|
42
|
+
path.relative? ? path : path.relative_path_from(seed_dir)
|
24
43
|
end
|
25
44
|
|
26
|
-
def
|
27
|
-
|
28
|
-
when Pathname then path
|
29
|
-
when String then Pathname.new(path)
|
30
|
-
else nil
|
31
|
-
end
|
45
|
+
def pathname(path)
|
46
|
+
self.class.pathname(path)
|
32
47
|
end
|
33
48
|
end
|
34
49
|
end
|
data/lib/seed_gimmick/options.rb
CHANGED
@@ -1,22 +1,78 @@
|
|
1
1
|
module SeedGimmick
|
2
2
|
class Options
|
3
|
-
%i(
|
3
|
+
VALID_OPTIONS_KEYS = %i(
|
4
|
+
seed_dir
|
5
|
+
tables
|
6
|
+
models
|
7
|
+
default_ext
|
8
|
+
exclude_columns
|
9
|
+
).freeze
|
10
|
+
|
11
|
+
VALID_OPTIONS_KEYS.each do |key|
|
4
12
|
define_method "#{key}=" do |value|
|
5
13
|
@options[key] = value
|
6
14
|
end
|
7
15
|
end
|
8
16
|
|
9
17
|
def initialize(options = {})
|
10
|
-
@options = options.symbolize_keys
|
18
|
+
@options = load_config.merge(options).symbolize_keys
|
11
19
|
end
|
12
20
|
|
13
21
|
def seed_dir
|
14
|
-
@options[:seed_dir] || default_seed_dir
|
22
|
+
root_dir.join(@options[:seed_dir] || default_seed_dir)
|
23
|
+
end
|
24
|
+
|
25
|
+
def tables
|
26
|
+
@options[:tables] || ENV["TABLES"].try(:split, ",") || []
|
27
|
+
end
|
28
|
+
|
29
|
+
def tables!
|
30
|
+
tables.presence || (raise SeedGimmickError)
|
31
|
+
end
|
32
|
+
|
33
|
+
def models
|
34
|
+
@options[:models] || ENV["MODELS"].try(:split, ",") || []
|
35
|
+
end
|
36
|
+
|
37
|
+
def models!
|
38
|
+
models.presence || (raise SeedGimmickError)
|
39
|
+
end
|
40
|
+
|
41
|
+
def default_ext
|
42
|
+
@options[:default_ext] || ENV["FORMAT"] || "yml"
|
43
|
+
end
|
44
|
+
|
45
|
+
def exclude_columns
|
46
|
+
@options[:exclude_columns] || default_exclude_columns
|
47
|
+
end
|
48
|
+
|
49
|
+
def load_config
|
50
|
+
config.exist? ? YAML.load_file(config)[environment] : {}
|
51
|
+
end
|
52
|
+
|
53
|
+
def environment
|
54
|
+
ENV["RAILS_ENV"] || ENV["APP_ENV"] || default_env
|
15
55
|
end
|
16
56
|
|
17
57
|
private
|
18
58
|
def default_seed_dir
|
19
|
-
|
59
|
+
Pathname.new("db/seed_gimmick")
|
60
|
+
end
|
61
|
+
|
62
|
+
def default_exclude_columns
|
63
|
+
["created_at", "updated_at"]
|
64
|
+
end
|
65
|
+
|
66
|
+
def default_env
|
67
|
+
defined?(Rails) ? Rails.env : "development".inquiry
|
68
|
+
end
|
69
|
+
|
70
|
+
def config
|
71
|
+
root_dir.join("config", "seed_gimmick.yml")
|
72
|
+
end
|
73
|
+
|
74
|
+
def root_dir
|
75
|
+
defined?(Rails) ? Rails.root : Pathname.pwd
|
20
76
|
end
|
21
77
|
end
|
22
78
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module SeedGimmick
|
2
|
+
class Seed
|
3
|
+
class << self
|
4
|
+
def find(options = nil)
|
5
|
+
options ||= Options.new
|
6
|
+
seed_files(options).map {|file|
|
7
|
+
new(file, options)
|
8
|
+
}.select {|seed|
|
9
|
+
options.tables.empty? || options.tables.include?(seed.table_name)
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def seed_files(options)
|
15
|
+
Pathname.glob(options.seed_dir.join("**", "*")).select(&:file?)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(file_or_model, options = nil)
|
20
|
+
@options = options || Options.new
|
21
|
+
@inflector = Inflector.build(@options)
|
22
|
+
unless @model = Inflector.model_class(file_or_model)
|
23
|
+
@seed_file = Inflector.pathname(file_or_model)
|
24
|
+
end
|
25
|
+
self_validation!
|
26
|
+
end
|
27
|
+
|
28
|
+
def model
|
29
|
+
self_validation!
|
30
|
+
@model ||= @inflector.model_for(@seed_file)
|
31
|
+
end
|
32
|
+
|
33
|
+
def seed_file(ext = nil)
|
34
|
+
self_validation!
|
35
|
+
@seed_file ||= @inflector.seed_for(@model, (ext || @options.default_ext))
|
36
|
+
end
|
37
|
+
|
38
|
+
def table_name
|
39
|
+
model.model_name.plural
|
40
|
+
end
|
41
|
+
|
42
|
+
def dump_columns(exclude_columns = [], all = false)
|
43
|
+
return model.column_names if all
|
44
|
+
exclude_columns = exclude_columns.presence || @options.exclude_columns
|
45
|
+
model.column_names - exclude_columns
|
46
|
+
end
|
47
|
+
|
48
|
+
def load_file
|
49
|
+
SeedIO.get(seed_file).load_data
|
50
|
+
end
|
51
|
+
|
52
|
+
def write_file(array_of_hashes)
|
53
|
+
SeedIO.get(seed_file).dump_data(array_of_hashes)
|
54
|
+
end
|
55
|
+
|
56
|
+
def bootstrap
|
57
|
+
ActiveRecord::Migration.say_with_time(table_name) do
|
58
|
+
model.transaction do
|
59
|
+
model.delete_all
|
60
|
+
model.import(load_file.map {|hash| model.new(hash) })
|
61
|
+
end
|
62
|
+
end
|
63
|
+
rescue LoadFailed => e
|
64
|
+
$stdout.print e.message
|
65
|
+
end
|
66
|
+
|
67
|
+
def dump(exclude_columns = [])
|
68
|
+
write_file(model.select(*dump_columns(exclude_columns)).map(&:attributes))
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
def self_validation!
|
73
|
+
@model || @seed_file || (raise SeedGimmickError)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
@@ -10,6 +10,16 @@ module SeedGimmick
|
|
10
10
|
def load_data
|
11
11
|
raise NotImplementedError
|
12
12
|
end
|
13
|
+
|
14
|
+
def dump_data(array_of_hashes)
|
15
|
+
raise NotImplementedError
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def write_raw(data)
|
20
|
+
seed_file.dirname.mkpath
|
21
|
+
seed_file.open("w") {|f| f.write data }
|
22
|
+
end
|
13
23
|
end
|
14
24
|
end
|
15
25
|
end
|
@@ -5,6 +5,20 @@ module SeedGimmick
|
|
5
5
|
data = YAML.load_file(seed_file) || (raise LoadFailed.new(seed_file))
|
6
6
|
data.values
|
7
7
|
end
|
8
|
+
|
9
|
+
def dump_data(array_of_hashes)
|
10
|
+
data = {}
|
11
|
+
array_of_hashes.each.with_index(1) do |row, i|
|
12
|
+
data[data_key(i)] = row
|
13
|
+
end
|
14
|
+
|
15
|
+
write_raw(data.to_yaml)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def data_key(id)
|
20
|
+
"data%d" % id
|
21
|
+
end
|
8
22
|
end
|
9
23
|
end
|
10
24
|
end
|
data/lib/seed_gimmick/seed_io.rb
CHANGED
@@ -3,11 +3,17 @@ require "seed_gimmick/seed_io/yaml_file"
|
|
3
3
|
|
4
4
|
module SeedGimmick
|
5
5
|
module SeedIO
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
class << self
|
7
|
+
def get(seed_file)
|
8
|
+
const_get(io_class_name_for(seed_file), false).new(seed_file)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def io_class_name_for(seed_file)
|
13
|
+
ext = Inflector.pathname(seed_file).extname.sub(/\A\./, "")
|
14
|
+
ext = "yaml" if ext == "yml"
|
15
|
+
"%sFile" % ext.capitalize
|
16
|
+
end
|
11
17
|
end
|
12
18
|
end
|
13
19
|
end
|
data/lib/seed_gimmick/version.rb
CHANGED
data/lib/seed_gimmick.rb
CHANGED
@@ -10,22 +10,22 @@ require "seed_gimmick/version"
|
|
10
10
|
require "seed_gimmick/errors"
|
11
11
|
require "seed_gimmick/options"
|
12
12
|
require "seed_gimmick/inflector"
|
13
|
-
require "seed_gimmick/finder"
|
14
13
|
require "seed_gimmick/seed_io"
|
15
|
-
require "seed_gimmick/
|
14
|
+
require "seed_gimmick/seed"
|
16
15
|
require "seed_gimmick/railtie" if defined?(Rails)
|
17
16
|
|
18
17
|
module SeedGimmick
|
19
|
-
|
20
|
-
|
21
|
-
seed.bootstrap
|
18
|
+
class << self
|
19
|
+
def bootstrap(options = nil)
|
20
|
+
Seed.find(options).each {|seed| seed.bootstrap }
|
22
21
|
end
|
23
|
-
end
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
def dump(options = nil)
|
24
|
+
options ||= Options.new
|
25
|
+
options.models.each do |model_name|
|
26
|
+
Seed.new(model_name, options).dump
|
27
|
+
end
|
28
|
+
end
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
data/lib/tasks/seed_gimmick.rake
CHANGED
@@ -1,6 +1,23 @@
|
|
1
|
-
namespace :
|
2
|
-
task :
|
3
|
-
|
1
|
+
namespace :db do
|
2
|
+
task :seed_gimmick => :environment do
|
3
|
+
SeedGimmick.bootstrap
|
4
|
+
end
|
5
|
+
|
6
|
+
namespace :seed_gimmick do
|
7
|
+
task :dump => :environment do
|
8
|
+
SeedGimmick.dump
|
9
|
+
end
|
4
10
|
end
|
5
11
|
end
|
6
12
|
|
13
|
+
namespace :seed_gimmick do
|
14
|
+
require "pp"
|
15
|
+
|
16
|
+
task :config do
|
17
|
+
pp SeedGimmick::Options.new.load_config
|
18
|
+
end
|
19
|
+
|
20
|
+
task :seed_files do
|
21
|
+
pp SeedGimmick::Seed.find.map {|seed| seed.seed_file.to_s }
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seed_gimmick
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -93,14 +93,13 @@ files:
|
|
93
93
|
- README.md
|
94
94
|
- Rakefile
|
95
95
|
- lib/generators/seed_gimmick/install_generator.rb
|
96
|
-
- lib/generators/seed_gimmick/templates/
|
96
|
+
- lib/generators/seed_gimmick/templates/seed_gimmick.yml
|
97
97
|
- lib/seed_gimmick.rb
|
98
98
|
- lib/seed_gimmick/errors.rb
|
99
|
-
- lib/seed_gimmick/finder.rb
|
100
99
|
- lib/seed_gimmick/inflector.rb
|
101
100
|
- lib/seed_gimmick/options.rb
|
102
101
|
- lib/seed_gimmick/railtie.rb
|
103
|
-
- lib/seed_gimmick/
|
102
|
+
- lib/seed_gimmick/seed.rb
|
104
103
|
- lib/seed_gimmick/seed_io.rb
|
105
104
|
- lib/seed_gimmick/seed_io/base.rb
|
106
105
|
- lib/seed_gimmick/seed_io/yaml_file.rb
|
data/lib/seed_gimmick/finder.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module SeedGimmick
|
2
|
-
module Finder
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def find(options = nil)
|
7
|
-
options ||= Options.new
|
8
|
-
seed_files(options).map {|file| SeedFile.new(options.seed_dir, file) }
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
def seed_files(options)
|
13
|
-
Pathname.glob(options.seed_dir.join("**", "*")).select(&:file?)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module SeedGimmick
|
2
|
-
class SeedFile
|
3
|
-
include Finder
|
4
|
-
|
5
|
-
attr_reader :inflector, :seed_file
|
6
|
-
|
7
|
-
def initialize(seed_dir, seed_file)
|
8
|
-
@inflector = Inflector.new(seed_dir)
|
9
|
-
@seed_file = seed_file
|
10
|
-
end
|
11
|
-
|
12
|
-
def load_file
|
13
|
-
SeedIO.get(seed_file).load_data
|
14
|
-
end
|
15
|
-
|
16
|
-
def bootstrap!
|
17
|
-
custom_action do |model, data|
|
18
|
-
ActiveRecord::Migration.say_with_time(model.model_name.plural) do
|
19
|
-
model.transaction do
|
20
|
-
model.delete_all
|
21
|
-
model.import(data.map {|rec| model.new(rec) })
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
rescue LoadFailed => e
|
26
|
-
puts e.message
|
27
|
-
end
|
28
|
-
|
29
|
-
def custom_action
|
30
|
-
yield(_model, load_file) if block_given?
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
def _model
|
35
|
-
@_model ||= inflector.model_for(seed_file)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|