heart_seed 0.0.1.beta1
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.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +23 -0
- data/.rspec +3 -0
- data/.travis.yml +11 -0
- data/.yardopts +2 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +11 -0
- data/heart_seed.gemspec +45 -0
- data/lib/heart_seed.rb +14 -0
- data/lib/heart_seed/converter.rb +120 -0
- data/lib/heart_seed/db_seed.rb +54 -0
- data/lib/heart_seed/helper.rb +49 -0
- data/lib/heart_seed/railtie.rb +7 -0
- data/lib/heart_seed/tasks/heart_seed.rake +87 -0
- data/lib/heart_seed/version.rb +3 -0
- data/spec/data/articles.xls +0 -0
- data/spec/data/articles.xlsx +0 -0
- data/spec/db/migration.rb +31 -0
- data/spec/dummy/.gitignore +16 -0
- data/spec/dummy/Gemfile +25 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +13 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +30 -0
- data/spec/dummy/config/boot.rb +4 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +28 -0
- data/spec/dummy/config/environments/production.rb +68 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/heart_seed.yml +2 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/db/migrate/20140615170742_create_articles.rb +10 -0
- data/spec/dummy/db/migrate/20140615170751_create_comments.rb +10 -0
- data/spec/dummy/db/migrate/20140615170757_create_likes.rb +9 -0
- data/spec/dummy/db/schema.rb +40 -0
- data/spec/dummy/db/seeds.rb +9 -0
- data/spec/dummy/db/seeds/articles.yml +11 -0
- data/spec/dummy/db/seeds/comments.yml +9 -0
- data/spec/dummy/db/seeds/likes.yml +4 -0
- data/spec/dummy/db/xls/articles.xls +0 -0
- data/spec/dummy/db/xls/comments_and_likes.xls +0 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/lib/tasks/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/robots.txt +5 -0
- data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
- data/spec/fixtures/articles.yml +11 -0
- data/spec/fixtures/comments.yml +9 -0
- data/spec/fixtures/likes.yml +4 -0
- data/spec/heart_seed/converter_spec.rb +59 -0
- data/spec/heart_seed/db_seed_spec.rb +54 -0
- data/spec/heart_seed/tasks/heart_seed_rake_spec.rb +84 -0
- data/spec/heart_seed_spec.rb +2 -0
- data/spec/spec_helper.rb +128 -0
- data/spec/support/models/article.rb +2 -0
- data/spec/support/models/comment.rb +2 -0
- data/spec/support/models/like.rb +2 -0
- data/spec/support/shared_contexts/rake_in_app_dir.rb +15 -0
- metadata +468 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 00e700cd31219d035de3f79465a7100761273f02
|
|
4
|
+
data.tar.gz: ae3bb179f7bd42d1f0529dd495bdc8b96fbbca7a
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 43c0dbba243860dfc16ace5d25f6ea84ed4518bda85bd82365fdf16081f48297c4ecb411897e8414616368e323575ac33c626c6dbc886f6db2e980a943b966af
|
|
7
|
+
data.tar.gz: 8e1d46e47673368a8840d19aff78de1fd1410a251825c33bec1b64b0fb9ab1a24e3c8ab9d735c977e39539ccd56bbeb73e18a10cd20fef2745652c67d16fe877
|
data/.coveralls.yml
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
repo_token: XaSSeLw9mCccZV3WRD4O1TcUfZMwjAxGA
|
data/.gitignore
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
*.bundle
|
|
19
|
+
*.so
|
|
20
|
+
*.o
|
|
21
|
+
*.a
|
|
22
|
+
mkmf.log
|
|
23
|
+
vendor/bundle
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 sue445
|
|
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,44 @@
|
|
|
1
|
+
# [WIP] HeartSeed
|
|
2
|
+
|
|
3
|
+
seed util (excel -> yaml -> db)
|
|
4
|
+
|
|
5
|
+
[](https://travis-ci.org/sue445/heart_seed)
|
|
6
|
+
[](https://codeclimate.com/github/sue445/heart_seed)
|
|
7
|
+
[](https://coveralls.io/r/sue445/heart_seed?branch=master)
|
|
8
|
+
[](https://gemnasium.com/sue445/heart_seed)
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
Add this line to your application's Gemfile:
|
|
13
|
+
|
|
14
|
+
gem 'heart_seed'
|
|
15
|
+
|
|
16
|
+
And then execute:
|
|
17
|
+
|
|
18
|
+
$ bundle
|
|
19
|
+
|
|
20
|
+
Or install it yourself as:
|
|
21
|
+
|
|
22
|
+
$ gem install heart_seed
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
1. `bundle exec rake heart_seed:init`
|
|
27
|
+
* create `config/heart_seed.yml`, `db/xls`, `db/seeds`
|
|
28
|
+
* append to `db/seeds.rb`
|
|
29
|
+
2. Create xls
|
|
30
|
+
* example https://github.com/sue445/heart_seed/tree/master/spec/dummy/db/xls
|
|
31
|
+
3. `bundle exec rake heart_seed:xls`
|
|
32
|
+
* Generate yml to `db/seeds`
|
|
33
|
+
* If you want to specify files: `FILES=comments_and_likes.xls SHEETS=comments,likes bundle exec rake heart_seed:xls`
|
|
34
|
+
4. `bundle exec rake db:seed`
|
|
35
|
+
* Import yml to db
|
|
36
|
+
* If you want to specify tables: `TABLES=articles,comments bundle exec rake db:seed`
|
|
37
|
+
|
|
38
|
+
## Contributing
|
|
39
|
+
|
|
40
|
+
1. Fork it ( https://github.com/sue445/heart_seed/fork )
|
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
44
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/heart_seed.gemspec
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'heart_seed/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "heart_seed"
|
|
8
|
+
spec.version = HeartSeed::VERSION
|
|
9
|
+
spec.authors = ["sue445"]
|
|
10
|
+
spec.email = ["sue445@sue445.net"]
|
|
11
|
+
spec.summary = %q{[WIP] seed util (excel -> yaml -> db) }
|
|
12
|
+
spec.description = %q{[WIP] seed util (excel -> yaml -> db) }
|
|
13
|
+
spec.homepage = "https://github.com/sue445/heart_seed"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
spec.required_ruby_version = '>= 2.0.0'
|
|
17
|
+
|
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
21
|
+
spec.require_paths = ["lib"]
|
|
22
|
+
|
|
23
|
+
spec.add_dependency "activesupport", ">= 3.0.0"
|
|
24
|
+
spec.add_dependency "activerecord", ">= 3.0.0"
|
|
25
|
+
spec.add_dependency "activerecord-import"
|
|
26
|
+
spec.add_dependency "roo", "~> 1.13.2"
|
|
27
|
+
|
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
|
29
|
+
spec.add_development_dependency "rake"
|
|
30
|
+
spec.add_development_dependency "rspec", "3.0.0"
|
|
31
|
+
spec.add_development_dependency "rspec-parameterized"
|
|
32
|
+
spec.add_development_dependency "rspec-its"
|
|
33
|
+
spec.add_development_dependency "rspec-collection_matchers"
|
|
34
|
+
spec.add_development_dependency "rspec-temp_dir"
|
|
35
|
+
spec.add_development_dependency "yard"
|
|
36
|
+
# spec.add_development_dependency "database_rewinder"
|
|
37
|
+
spec.add_development_dependency "sqlite3"
|
|
38
|
+
spec.add_development_dependency "pry"
|
|
39
|
+
spec.add_development_dependency "pry-remote"
|
|
40
|
+
spec.add_development_dependency "pry-nav"
|
|
41
|
+
spec.add_development_dependency "rake_shared_context"
|
|
42
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
|
43
|
+
spec.add_development_dependency "coveralls"
|
|
44
|
+
end
|
|
45
|
+
|
data/lib/heart_seed.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require "roo"
|
|
2
|
+
require "active_support/all"
|
|
3
|
+
require "active_record"
|
|
4
|
+
require "yaml"
|
|
5
|
+
require "activerecord-import"
|
|
6
|
+
|
|
7
|
+
module HeartSeed
|
|
8
|
+
autoload :Converter , "heart_seed/converter"
|
|
9
|
+
autoload :DbSeed , "heart_seed/db_seed"
|
|
10
|
+
autoload :Helper , "heart_seed/helper"
|
|
11
|
+
autoload :Version , "heart_seed/version"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
require "heart_seed/railtie" if defined?(Rails)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
module HeartSeed
|
|
2
|
+
module Converter
|
|
3
|
+
HEADER_ROW = 1
|
|
4
|
+
|
|
5
|
+
# convert xls,xlsx to yaml and write to file.
|
|
6
|
+
#
|
|
7
|
+
# ## example
|
|
8
|
+
# ### source xls
|
|
9
|
+
# ```csv
|
|
10
|
+
# id, title, description, created_at
|
|
11
|
+
# 1, title1, description1, 2014-06-01 12:10:00 +0900
|
|
12
|
+
# 2, title2, description2, 2014-06-02 12:10:00 +0900
|
|
13
|
+
# ```
|
|
14
|
+
#
|
|
15
|
+
# ### output yaml
|
|
16
|
+
# ```yaml
|
|
17
|
+
# ---
|
|
18
|
+
# articles_1:
|
|
19
|
+
# id: 1
|
|
20
|
+
# title: title1
|
|
21
|
+
# description: description1
|
|
22
|
+
# created_at: '2014-06-01 12:10:00 +0900'
|
|
23
|
+
# articles_2:
|
|
24
|
+
# id: 2
|
|
25
|
+
# title: title2
|
|
26
|
+
# description: description2
|
|
27
|
+
# created_at: '2014-06-02 12:10:00 +0900'
|
|
28
|
+
# ```
|
|
29
|
+
#
|
|
30
|
+
# @param source_file [String] source file (xls, xlsx)
|
|
31
|
+
# @param source_sheet [String]
|
|
32
|
+
# @param dist_file [String] don't write to file if blank
|
|
33
|
+
#
|
|
34
|
+
# @return [ Hash{ String => Hash{ String => Object } } ]
|
|
35
|
+
def self.convert_to_yml(source_file: nil, source_sheet: nil, dist_file: nil)
|
|
36
|
+
sheet = open_file(source_file).sheet(source_sheet)
|
|
37
|
+
return nil if empty_sheet?(sheet)
|
|
38
|
+
|
|
39
|
+
fixtures = read_sheet(sheet, source_sheet)
|
|
40
|
+
|
|
41
|
+
unless dist_file.blank?
|
|
42
|
+
File.open(dist_file, "w") do |f|
|
|
43
|
+
f.write(YAML.dump(fixtures))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
fixtures
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @param source_file [String] source yml file
|
|
51
|
+
#
|
|
52
|
+
# @return [Array<Hash>] rows
|
|
53
|
+
def self.read_fixture_yml(source_file)
|
|
54
|
+
YAML.load_file(source_file).values
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# @param source_file
|
|
58
|
+
#
|
|
59
|
+
# @return [Array<String>] sheet names (rejected multi-byte sheet)
|
|
60
|
+
def self.table_sheets(source_file)
|
|
61
|
+
# reject multi-byte sheet
|
|
62
|
+
open_file(source_file).sheets.select{|sheet| sheet =~ /^[A-Za-z0-9_]+$/ }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
# @param source_file [String]
|
|
68
|
+
#
|
|
69
|
+
# @return [Roo::Base]
|
|
70
|
+
def self.open_file(source_file)
|
|
71
|
+
case File.extname(source_file)
|
|
72
|
+
when ".xls"
|
|
73
|
+
Roo::Excel.new(source_file)
|
|
74
|
+
when ".xlsx"
|
|
75
|
+
Roo::Excelx.new(source_file)
|
|
76
|
+
else
|
|
77
|
+
raise ArgumentError, "unknown format: #{source_file}"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.empty_sheet?(sheet)
|
|
82
|
+
sheet.first_row == sheet.last_row || sheet.first_column == sheet.last_column
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @param sheet [Roo::Base]
|
|
86
|
+
# @param row_prefix [String]
|
|
87
|
+
#
|
|
88
|
+
# @return [ Hash{ String => Hash{ String => Object } } ]
|
|
89
|
+
def self.read_sheet(sheet, row_prefix)
|
|
90
|
+
header_keys = sheet.row(HEADER_ROW)
|
|
91
|
+
fixtures = {}
|
|
92
|
+
|
|
93
|
+
(HEADER_ROW + 1 .. sheet.last_row).each do |row_num|
|
|
94
|
+
row_value = {}
|
|
95
|
+
header_keys.each_with_index do |key, col_index|
|
|
96
|
+
value = sheet.cell(row_num, col_index + 1)
|
|
97
|
+
|
|
98
|
+
case sheet.celltype(row_num, col_index + 1)
|
|
99
|
+
when :float
|
|
100
|
+
# ex) 1.0 -> 1
|
|
101
|
+
value = value.to_i if value == value.to_i
|
|
102
|
+
when :date, :time, :datetime
|
|
103
|
+
# value is DateTime and localtime, but not included TimeZone(UTC)
|
|
104
|
+
time = Time.zone.at(value.to_i - Time.zone.utc_offset)
|
|
105
|
+
value = time.to_s
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
row_value[key] = value
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
suffix = row_value.has_key?("id") ? row_value["id"] : row_num - 1
|
|
112
|
+
row_name = "#{row_prefix}_#{suffix}"
|
|
113
|
+
|
|
114
|
+
fixtures[row_name] = row_value
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
fixtures
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module HeartSeed
|
|
2
|
+
module DbSeed
|
|
3
|
+
# delete all records and insert from seed yaml
|
|
4
|
+
#
|
|
5
|
+
# @param source_file [String]
|
|
6
|
+
# @param model_class [Class] require. extends {ActiveRecord::Base}
|
|
7
|
+
def self.bulk_insert(source_file: nil, model_class: nil)
|
|
8
|
+
fixtures = HeartSeed::Converter.read_fixture_yml(source_file)
|
|
9
|
+
models = fixtures.each_with_object([]) do |fixture, response|
|
|
10
|
+
response << model_class.new(fixture)
|
|
11
|
+
response
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
model_class.transaction do
|
|
15
|
+
model_class.delete_all
|
|
16
|
+
model_class.import(models)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# import all seed yaml to table
|
|
21
|
+
#
|
|
22
|
+
# @param seed_dir [String]
|
|
23
|
+
# @param tables [Array<String>,String] table names array or comma separated table names. if empty, import all seed yaml. if not empty, import only these tables.
|
|
24
|
+
def self.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: [])
|
|
25
|
+
target_tables = parse_arg_tables(tables)
|
|
26
|
+
|
|
27
|
+
Dir.glob(File.join(seed_dir, "*.yml")) do |file|
|
|
28
|
+
table_name = File.basename(file, '.*')
|
|
29
|
+
next unless target_table?(table_name, target_tables)
|
|
30
|
+
|
|
31
|
+
begin
|
|
32
|
+
model_class = table_name.classify.constantize
|
|
33
|
+
bulk_insert(source_file: file, model_class: model_class)
|
|
34
|
+
puts "[INFO] #{file} -> #{table_name}"
|
|
35
|
+
rescue => e
|
|
36
|
+
puts "[ERROR] #{e.message}"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.parse_arg_tables(tables)
|
|
42
|
+
return [] unless tables
|
|
43
|
+
return tables if tables.class == Array
|
|
44
|
+
|
|
45
|
+
tables.class == String ? tables.split(",") : []
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
def self.target_table?(source_table, target_tables)
|
|
50
|
+
return true if target_tables.empty?
|
|
51
|
+
return target_tables.include?(source_table)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module HeartSeed
|
|
2
|
+
module Helper
|
|
3
|
+
CONFIG_FILE = "config/heart_seed.yml"
|
|
4
|
+
|
|
5
|
+
# read config/heart_seed.yml
|
|
6
|
+
# @return [Hash{String => String}]
|
|
7
|
+
def self.config
|
|
8
|
+
if File.exists?(CONFIG_FILE)
|
|
9
|
+
YAML.load_file(CONFIG_FILE)
|
|
10
|
+
else
|
|
11
|
+
{
|
|
12
|
+
"seed_dir" => "db/seeds",
|
|
13
|
+
"xls_dir" => "db/xls",
|
|
14
|
+
}
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# @return [Pathname]
|
|
19
|
+
def self.seed_dir
|
|
20
|
+
dir = config["seed_dir"] || "db/seeds"
|
|
21
|
+
root_dir.join(dir)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @return [Pathname]
|
|
25
|
+
def self.xls_dir
|
|
26
|
+
dir = config["xls_dir"] || "db/xls"
|
|
27
|
+
root_dir.join(dir)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# return {Rails.root} , {Padrino.root} or current dir
|
|
31
|
+
# @return [Pathname]
|
|
32
|
+
def self.root_dir
|
|
33
|
+
return @root_dir if @root_dir
|
|
34
|
+
|
|
35
|
+
if defined? Rails
|
|
36
|
+
Rails.root
|
|
37
|
+
elsif defined? Padrino
|
|
38
|
+
Pathname.new(Padrino.root)
|
|
39
|
+
else
|
|
40
|
+
Pathname.pwd
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# @param dir [String]
|
|
45
|
+
def self.root_dir=(dir)
|
|
46
|
+
@root_dir = Pathname.new(dir)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
namespace :heart_seed do
|
|
2
|
+
desc "create dir and file"
|
|
3
|
+
task :init => ["config/heart_seed.yml", "db/xls", "db/seeds"] do
|
|
4
|
+
template = <<RUBY
|
|
5
|
+
|
|
6
|
+
# Appended by `rake heart_seed:init`
|
|
7
|
+
HeartSeed::DbSeed.import_all(tables: ENV["TABLES"])
|
|
8
|
+
|
|
9
|
+
RUBY
|
|
10
|
+
|
|
11
|
+
append_file("db/seeds.rb", template)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
file "config/heart_seed.yml" => "config" do
|
|
15
|
+
template = <<YAML
|
|
16
|
+
seed_dir: db/seeds
|
|
17
|
+
xls_dir: db/xls
|
|
18
|
+
YAML
|
|
19
|
+
|
|
20
|
+
create_file("config/heart_seed.yml", template)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
directory "config"
|
|
24
|
+
directory "db/xls"
|
|
25
|
+
directory "db/seeds"
|
|
26
|
+
|
|
27
|
+
desc "create seed files by xls directory"
|
|
28
|
+
task :xls => :environment do
|
|
29
|
+
Dir.glob(File.join(HeartSeed::Helper.xls_dir, "*.{xls,xlsx}")) do |file|
|
|
30
|
+
next if File.basename(file) =~ /^~/
|
|
31
|
+
|
|
32
|
+
next unless target_file?(file)
|
|
33
|
+
|
|
34
|
+
puts "Source File: #{file}"
|
|
35
|
+
sheets = HeartSeed::Converter.table_sheets(file)
|
|
36
|
+
sheets.each do |sheet|
|
|
37
|
+
unless ActiveRecord::Base.connection.table_exists?(sheet)
|
|
38
|
+
puts " [#{sheet}] Table is not found"
|
|
39
|
+
next
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
next unless target_sheet?(sheet)
|
|
43
|
+
|
|
44
|
+
dist_file = File.join(HeartSeed::Helper.seed_dir, "#{sheet}.yml")
|
|
45
|
+
fixtures = HeartSeed::Converter.convert_to_yml(source_file: file, source_sheet: sheet, dist_file: dist_file)
|
|
46
|
+
if fixtures
|
|
47
|
+
puts " [#{sheet}] Create seed: #{dist_file}"
|
|
48
|
+
else
|
|
49
|
+
puts " [#{sheet}] Sheet is empty"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
def target_file?(file)
|
|
57
|
+
return true if ENV["FILES"].blank?
|
|
58
|
+
|
|
59
|
+
ENV["FILES"].split(",").include?(File.basename(file))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def target_sheet?(sheet)
|
|
63
|
+
return true if ENV["SHEETS"].blank?
|
|
64
|
+
|
|
65
|
+
ENV["SHEETS"].split(",").include?(sheet)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def create_file(file, str)
|
|
69
|
+
open(file, "w") do |out|
|
|
70
|
+
out.write(str)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
puts "create: #{file}"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def append_file(file, str)
|
|
77
|
+
if File.exists?(file)
|
|
78
|
+
return if File.open(file).read.include?(str)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
File.open(file, "a") do |out|
|
|
82
|
+
out.write(str)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
puts "append: #{file}"
|
|
86
|
+
end
|
|
87
|
+
end
|