seedbank 0.4.0 → 0.5.0

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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.hound.yml +3 -0
  4. data/.rubocop.yml +18 -0
  5. data/.rubocop_todo.yml +29 -0
  6. data/.travis.yml +10 -9
  7. data/Gemfile +5 -4
  8. data/README.md +50 -25
  9. data/Rakefile +2 -1
  10. data/TODO.txt +3 -3
  11. data/gemfiles/rake10.gemfile +1 -4
  12. data/gemfiles/rake11.gemfile +12 -0
  13. data/gemfiles/{rake0.9.gemfile → rake12.gemfile} +3 -4
  14. data/lib/seedbank/dsl.rb +65 -43
  15. data/lib/seedbank/railtie.rb +2 -7
  16. data/lib/seedbank/runner.rb +19 -32
  17. data/lib/seedbank/version.rb +2 -1
  18. data/lib/seedbank.rb +15 -6
  19. data/lib/tasks/seed.rake +16 -20
  20. data/seedbank.gemspec +35 -58
  21. data/test/dummy/Rakefile +1 -0
  22. data/test/dummy/app/controllers/application_controller.rb +1 -0
  23. data/test/dummy/config/application.rb +2 -22
  24. data/test/dummy/config/boot.rb +2 -1
  25. data/test/dummy/config/environment.rb +1 -0
  26. data/test/dummy/config/environments/development.rb +3 -7
  27. data/test/dummy/config/environments/test.rb +4 -9
  28. data/test/dummy/config/initializers/secret_token.rb +1 -0
  29. data/test/dummy/config/initializers/session_store.rb +2 -1
  30. data/test/dummy/config/initializers/wrap_parameters.rb +2 -1
  31. data/test/dummy/config/routes.rb +1 -0
  32. data/test/dummy/config.ru +2 -1
  33. data/test/dummy/db/seeds/circular1.seeds.rb +2 -1
  34. data/test/dummy/db/seeds/circular2.seeds.rb +2 -1
  35. data/test/dummy/db/seeds/dependency.seeds.rb +1 -0
  36. data/test/dummy/db/seeds/dependency2.seeds.rb +1 -0
  37. data/test/dummy/db/seeds/dependent.seeds.rb +2 -1
  38. data/test/dummy/db/seeds/dependent_on_nested.seeds.rb +2 -1
  39. data/test/dummy/db/seeds/dependent_on_several.seeds.rb +2 -1
  40. data/test/dummy/db/seeds/development/users.seeds.rb +1 -0
  41. data/test/dummy/db/seeds/no_block.seeds.rb +2 -1
  42. data/test/dummy/db/seeds/reference_memos.seeds.rb +2 -1
  43. data/test/dummy/db/seeds/with_block_memo.seeds.rb +2 -1
  44. data/test/dummy/db/seeds/with_inline_memo.seeds.rb +1 -0
  45. data/test/dummy/db/seeds.rb +1 -0
  46. data/test/dummy/script/rails +3 -2
  47. data/test/lib/seedbank/dsl_test.rb +58 -75
  48. data/test/lib/seedbank/runner_test.rb +39 -46
  49. data/test/lib/tasks/seed_rake_test.rb +85 -48
  50. data/test/test_helper.rb +13 -11
  51. metadata +50 -24
  52. data/test/dummy/lib/tasks/dsl.rake +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c3ba57609267657c1c16f55b5e2de397c104487
4
- data.tar.gz: 0f2fde06d7f9288761c22607739677558065a1f4
3
+ metadata.gz: e92838099b21443451e9fd5a5bfecdf5f3874e65
4
+ data.tar.gz: 31061861c8211b79dd619fb2017527d748ea2291
5
5
  SHA512:
6
- metadata.gz: a8e3cf1e8ace47e20bb827fd6f67a6037ec3c2f2b32a85b64dd3909b53e4c6640f32f2d9bbcb3c2e2227666dd25451eb85915d55309334b2cb46e6062572a933
7
- data.tar.gz: 4d91e63092b1acceb133552a223f0669b94875710dec4639bca7ae67001de201491cd67521bb1941c42f29915cd8a074ee04433b79082209450e189137dd7eec
6
+ metadata.gz: 1f9258f988c7ac6f2438a6974057c8b09e5fd6692196af040f60dcbf36c3e24c0d6427a0677d1bd670e73f9c75bc449cfab51c8481f1321da2b2fa9fd95d1968
7
+ data.tar.gz: 5e65fa43aa95cca1612a970e1fd47f11a2f66eb3aafc4ee0ee5f60669aed750f3decf570c3dfc0fb1e0664f6b3122ebaae7cf0983c13c7b47386f94eaed0b637
data/.gitignore CHANGED
@@ -27,4 +27,5 @@ test/dummy/log
27
27
  .ruby-version
28
28
  .ruby-gemset
29
29
  Gemfile.lock
30
- .bundle
30
+ .bundle
31
+ .byebug_history
data/.hound.yml ADDED
@@ -0,0 +1,3 @@
1
+ ruby:
2
+ config_file: .rubocop.yml
3
+
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ inherit_from: .rubocop_todo.yml
2
+ Style/StringLiteral:
3
+ Enabled: true
4
+ EnforcedStyle: single_quotes
5
+ Style/MutableConstant:
6
+ Exclude:
7
+ - 'lib/seedbank/version.rb'
8
+ Lint/ScriptPermission:
9
+ Exclude:
10
+ - 'test/dummy/Rakefile'
11
+ Metrics/LineLength:
12
+ Exclude:
13
+ - 'test/**/*'
14
+ Max: 132
15
+ Style/ConditionalAssignment:
16
+ Enabled: false
17
+ Layout/EmptyLineAfterMagicComment:
18
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,29 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2017-02-12 18:38:59 +0700 using RuboCop version 0.47.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 6
10
+ # Configuration parameters: CountComments, ExcludedMethods.
11
+ Metrics/BlockLength:
12
+ Max: 124
13
+
14
+ # Offense count: 1
15
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
16
+ # SupportedStyles: nested, compact
17
+ Style/ClassAndModuleChildren:
18
+ Exclude:
19
+ - 'test/test_helper.rb'
20
+
21
+ # Offense count: 4
22
+ Style/Documentation:
23
+ Exclude:
24
+ - 'spec/**/*'
25
+ - 'test/**/*'
26
+ - 'lib/seedbank.rb'
27
+ - 'lib/seedbank/dsl.rb'
28
+ - 'lib/seedbank/railtie.rb'
29
+ - 'lib/seedbank/runner.rb'
data/.travis.yml CHANGED
@@ -1,15 +1,16 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - ree
5
- - 1.8.7
6
- - 1.9.3
7
- - 2.0.0
8
- - 2.1.0
9
- - jruby-18mode # JRuby in 1.8 mode
10
- - jruby-19mode # JRuby in 1.9 mode
11
- - rbx-2
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3
7
+ - 2.4
8
+ - 2.5
12
9
 
13
10
  gemfile:
14
- - gemfiles/rake0.9.gemfile
15
11
  - gemfiles/rake10.gemfile
12
+ - gemfiles/rake11.gemfile
13
+ - gemfiles/rake12.gemfile
14
+
15
+ before_install:
16
+ - gem install bundler -v '~> 1.11' --no-ri --no-rdoc
data/Gemfile CHANGED
@@ -1,12 +1,13 @@
1
- source "http://rubygems.org"
1
+ # frozen_string_literal: true
2
+ source 'http://rubygems.org'
2
3
 
3
4
  # Specify your gem's dependencies in seedbank.gemspec
4
5
  gemspec
5
6
 
6
- gem 'rake', :group => :test
7
+ gem 'rubocop', group: :development
7
8
 
8
9
  # for CRuby, Rubinius, including Windows and RubyInstaller
9
- gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
10
+ gem 'sqlite3', platform: %i[ruby mswin mingw]
10
11
 
11
12
  # for JRuby
12
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
13
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Seedbank
2
2
  ========
3
3
 
4
- Seedbank allows you to structure your Rails seed data instead of having it all dumped into one large file. I find my seed data tended to fall into two categories:
4
+ Seedbank allows you to structure your apps seed data instead of having it all dumped into one large file. I find my seed data tended to fall into two categories:
5
5
 
6
6
  1. Stuff that the entire application requires.
7
7
  2. Stuff to populate my development and staging environments.
@@ -14,6 +14,11 @@ The reason behind Seedbank is laziness. When I checkout or re-visit a project I
14
14
 
15
15
  To achieve this slothful aim, Seedbank renames the original db:seed rake task to db:seed:original, makes it a dependency for all the Seedbank seeds and adds a new db:seed task that loads all the common seeds in db/seeds plus all the seeds for the current Rails environment.
16
16
 
17
+ Although originally built for Rails, Seedbank can work stand alone thanks to Aleksey Ivanov.
18
+
19
+ [![Build Status](https://travis-ci.org/james2m/seedbank.svg?branch=master)](https://travis-ci.org/james2m/seedbank)
20
+ [![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com)
21
+
17
22
  Example
18
23
  =======
19
24
 
@@ -46,12 +51,16 @@ will load the seeds in `db/seeds.rb`, `db/seeds/bar.seeds.rb` and `db/seeds/foo.
46
51
 
47
52
  Installation
48
53
  ============
54
+
55
+ Seedbank > 0.5.0 uses refinements and no longer supports rubies below 2.x. If you are using an older Ruby you'll have to stick with 0.4.0 and below.
56
+
57
+ I have also dropped support for Rubinius and JRuby. I'm happy to accept pull requests for them, but don't have the time to hack together the test environment. If you want to contribute, please ensure that he travis.yml is in line as it's the only way I will test these two environments.
58
+
49
59
  ### Rails 5.x
50
60
 
51
- Seedbank has not been updated to work with Rails 5. I've not tested it with 5.x and am working on a new version specifically for 5.x. That said, I believe some
52
- people are using it with no problems.
61
+ Seedbank has not been updated to work with Rails 5. I've used it with 5.x apps and am working on a new version specifically for 5.x. Other people are also reporting using it with no problems.
53
62
 
54
- ### Rails 3.x and 4.x
63
+ ### Rails 4.x and above
55
64
 
56
65
  Add the seedbank gem to your Gemfile. In Gemfile:
57
66
 
@@ -61,21 +70,22 @@ gem "seedbank"
61
70
 
62
71
  That's it!
63
72
 
64
- ### Rails 2.x
73
+ ### Non Rails apps
65
74
 
66
- Add to your config/environment.rb
75
+ Although originally built for Rails, Seedbank should work fine in other environments such as Padrino, Grape or the new new hotness. please let us know how you get on.
67
76
 
68
- ```ruby
69
- config.gem 'seedbank'
70
- ```
77
+ ### Rails 3.x
71
78
 
72
- Install the gem:
79
+ Seedbank 0.5.0 onwards is no longer tested against Rails 3.x, that isn't to say it will not work. I
80
+ will not fix issues against Rails 3.x, but will accept tested pull requests.
73
81
 
74
- $ rake gems:install
82
+ ### Rails 2.x
75
83
 
76
- Or, if you're using Bundler:
84
+ Seedbank hasn't supported Rails 2.x for some time. You'll need to use the 0.2.1 version. In your Gemfile:
77
85
 
78
- $ bundle install
86
+ ```ruby
87
+ gem "seedbank", '~> 0.2.1'
88
+ ```
79
89
 
80
90
  Then in the bottom of your application's Rakefile:
81
91
 
@@ -89,7 +99,7 @@ If you vendor the gem you'll need to change the require to the specific path.
89
99
  Usage
90
100
  =====
91
101
 
92
- Seeds files are just plain old Ruby executed in your rails application environment so anything you could type into the rails console will work in your seeds. Seeds files have to be named with the '.seeds.rb' extension.
102
+ Seeds files are just plain old Ruby executed in your application environment so anything you could type into the console will work in your seeds. Seeds files have to be named with the '.seeds.rb' extension.
93
103
 
94
104
  db/seeds/companies.seeds.rb
95
105
  ```ruby
@@ -134,24 +144,39 @@ after "development:companies" do
134
144
  end
135
145
  ```
136
146
 
147
+ *Note* - If you experience any errors like `Don't know how to build task 'db:seed:users'`. Ensure you are specifying `after 'development:companies'` like the above example. This is the usual culprit (YMMV).
148
+
137
149
  ### Defining and using methods
138
150
 
139
- As seed files are evaluated within blocks, methods need to be defined and used as per below:
151
+ As seed files are evaluated within a single runner in dependency order, any methods defined earlier in the run will be available across dependent tasks. I
152
+ recommend keeping method definitions in the seed file that uses them. Alternatively if you have many common methods, put them into a module and extend the
153
+ runner with the module.
140
154
 
155
+ db/seeds/support.rb
141
156
  ```ruby
142
- class << self
143
- def create_user name
144
- user = User.where(name: name).first_or_create
145
- # ...
157
+ module Support
158
+ def notify(filename)
159
+ puts "Seeding: #{filename}"
146
160
  end
147
161
  end
162
+ ```
148
163
 
149
- ['Greg', 'Daniel'].each do |name|
150
- create_user name
151
- end
164
+ db/seeds/common.seeds.rb
165
+ ```ruby
166
+ require_relative 'support'
167
+ extend Support
168
+
169
+ notify(__FILE__)
152
170
  ```
153
171
 
154
- *Note* - If you experience any errors like `Don't know how to build task 'db:seed:users'`. Ensure your specifying `after 'development:companies'` like the above example. This is the usual culprit (YMMV).
172
+ To keep this dry you could make the seeds dependent on a support seed that extends the runner.
173
+
174
+ db/seeds/users.seeds.rb
175
+ ```ruby
176
+ after :common do
177
+ notify(__FILE__)
178
+ end
179
+ ```
155
180
 
156
181
  Contributors
157
182
  ============
@@ -171,7 +196,7 @@ git log | grep Author | sort | uniq
171
196
  * lulalala
172
197
  * pivotal-cloudplanner
173
198
  * vkill
174
-
199
+ * Aleksey Ivanov
175
200
 
176
201
  Note on Patches/Pull Request
177
202
  ============================
@@ -185,4 +210,4 @@ Note on Patches/Pull Request
185
210
 
186
211
  Copyright
187
212
  =========
188
- Copyright (c) 2011-2015 James McCarthy, released under the MIT license
213
+ Copyright (c) 2011-2017 James McCarthy, released under the MIT license
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'bundler/gem_tasks'
2
3
  require 'rake'
3
4
  require 'rake/testtask'
@@ -19,4 +20,4 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
19
20
  rdoc.rdoc_files.include('lib/**/*.rb')
20
21
  end
21
22
 
22
- task :default => ["test"]
23
+ task default: ['test']
data/TODO.txt CHANGED
@@ -1,4 +1,4 @@
1
1
  * Add generator for seeds directories
2
- * Add generator for seeds data
3
- * Add ability to add or update records in place
4
- * Make framework agnostic
2
+ * Add platform independent test environment
3
+ * Add generator for seeds data?
4
+ * Add ability to add or update records in place?
@@ -9,7 +9,4 @@ gem 'rake', '~>10.0', :group => :test
9
9
  gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
10
10
 
11
11
  # for JRuby
12
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
13
-
14
- gem 'seedbank', :path => '../'
15
-
12
+ gem 'activerecord-jdbcsqlite3-adapter', '~>1.3.25', :platform => :jruby
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in seedbank.gemspec
4
+ gemspec :path=>"../"
5
+
6
+ gem 'rake', '~>11.0', :group => :test
7
+
8
+ # for CRuby, Rubinius, including Windows and RubyInstaller
9
+ gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
10
+
11
+ # for JRuby
12
+ gem 'activerecord-jdbcsqlite3-adapter', '~>1.3.25', :platform => :jruby
@@ -1,14 +1,13 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in seedbank.gemspec
4
- gemspec :path => "../"
4
+ gemspec :path=>"../"
5
5
 
6
- gem 'rake', '~>0.9.0', :group => :test
6
+ gem 'rake', '~>12.0', :group => :test
7
7
 
8
8
  # for CRuby, Rubinius, including Windows and RubyInstaller
9
9
  gem "sqlite3", :platform => [:ruby, :mswin, :mingw]
10
10
 
11
11
  # for JRuby
12
- gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
12
+ gem 'activerecord-jdbcsqlite3-adapter', '~>1.3.25', :platform => :jruby
13
13
 
14
- gem 'seedbank', :path => '../'
data/lib/seedbank/dsl.rb CHANGED
@@ -1,64 +1,86 @@
1
+ # frozen_string_literal: true
1
2
  module Seedbank
2
3
  module DSL
4
+ refine Object do
5
+ def override_seed_task(*args)
6
+ task_name, _, deps = Rake.application.resolve_args(args)
7
+ seed_task = Rake::Task.task_defined?(task_name) ? Rake::Task[task_name].clear : Rake::Task.define_task(task_name)
8
+ add_comment_to(seed_task, Rake.application.last_description)
9
+ add_environment_dependency(seed_task)
10
+ seed_task.enhance deps
11
+ end
3
12
 
4
- def override_seed_task(*args)
5
- task_name, arg_names, deps = Rake.application.resolve_args(args)
6
- seed_task = Rake::Task.task_defined?(task_name) ? Rake::Task[task_name].clear : Rake::Task.define_task(task_name)
7
- add_comment_to(seed_task, Rake.application.last_description)
8
- seed_task.enhance deps
9
- end
13
+ def seed_tasks_matching(*pattern)
14
+ glob_seed_files_matching(*pattern)
15
+ .sort
16
+ .map { |seed_file| seed_task_from_file(seed_file) }
17
+ end
10
18
 
11
- def seed_task_from_file(seed_file)
12
- scopes = scope_from_seed_file(seed_file)
13
- fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':')
19
+ def seed_task_from_file(seed_file)
20
+ scopes = scope_from_seed_file(seed_file)
21
+ fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':')
14
22
 
15
- define_seed_task(seed_file, fq_name)
16
- end
23
+ define_seed_task(seed_file, fq_name)
24
+ end
17
25
 
18
- def glob_seed_files_matching(*args, &block)
19
- Dir.glob(File.join(seeds_root, *args), &block)
20
- end
26
+ def glob_seed_files_matching(*args, &block)
27
+ Dir.glob(File.join(seeds_root, *args), &block)
28
+ end
21
29
 
22
- def runner
23
- @_seedbank_runner ||= Seedbank::Runner.new
24
- end
30
+ def define_seed_task(seed_file, *args)
31
+ task = Rake::Task.define_task(*args) do |seed_task|
32
+ runner.evaluate(seed_task, seed_file) if File.exist?(seed_file)
33
+ end
25
34
 
26
- def define_seed_task(seed_file, *args)
27
- task = Rake::Task.define_task(*args) do |seed_task|
28
- runner.evaluate(seed_task, seed_file) if File.exist?(seed_file)
35
+ relative_file = Pathname.new(seed_file).relative_path_from(Seedbank.application_root)
36
+
37
+ task.add_description "Load the seed data from #{relative_file}"
38
+ add_environment_dependency(task)
39
+ task.name
29
40
  end
30
41
 
31
- task.add_description "Load the seed data from #{seed_file}"
42
+ def original_seeds_file
43
+ @_seedbank_original ||= existent(Pathname.new('../seeds.rb').expand_path(seeds_root))
44
+ end
32
45
 
33
- if Rake::Task.task_defined?('db:abort_if_pending_migrations')
34
- task.enhance(['db:abort_if_pending_migrations'])
35
- elsif Rake::Task.task_defined?(':environment')
36
- task.enhance([':environment'])
46
+ def seeds_root
47
+ Pathname.new Seedbank.seeds_root
37
48
  end
38
49
 
39
- task.name
40
- end
50
+ private
41
51
 
42
- def scope_from_seed_file(seed_file)
43
- dirname = Pathname.new(seed_file).dirname
44
- return [] if dirname == seeds_root
45
- relative = dirname.relative_path_from(seeds_root)
46
- relative.to_s.split(File::Separator)
47
- end
52
+ def existent(path)
53
+ String(path) if path.exist?
54
+ end
48
55
 
49
- def seeds_root
50
- Pathname.new Seedbank.seeds_root
51
- end
56
+ def scope_from_seed_file(seed_file)
57
+ dirname = Pathname.new(seed_file).dirname
58
+ return [] if dirname == seeds_root
59
+ dirname
60
+ .relative_path_from(seeds_root)
61
+ .to_s
62
+ .split(File::Separator)
63
+ end
52
64
 
53
- private
65
+ def add_environment_dependency(task)
66
+ if Rake::Task.task_defined?('db:abort_if_pending_migrations')
67
+ task.enhance(['db:abort_if_pending_migrations'])
68
+ elsif defined?(Rails)
69
+ task.enhance([:environment])
70
+ end
71
+ end
54
72
 
55
- def add_comment_to(seed_task, comment)
56
- if seed_task.respond_to?(:clear_comments)
57
- seed_task.comment = comment
58
- else
59
- seed_task.send :instance_variable_set, '@full_comment', comment
73
+ def runner
74
+ @_seedbank_runner ||= Seedbank::Runner.new
60
75
  end
61
- end
62
76
 
77
+ def add_comment_to(seed_task, comment)
78
+ if seed_task.respond_to?(:clear_comments)
79
+ seed_task.comment = comment
80
+ else
81
+ seed_task.send :instance_variable_set, '@full_comment', comment
82
+ end
83
+ end
84
+ end
63
85
  end
64
86
  end
@@ -1,13 +1,8 @@
1
- require 'seedbank'
2
- require 'rails'
3
-
1
+ # frozen_string_literal: true
4
2
  module Seedbank
5
3
  class Railtie < Rails::Railtie
6
-
7
4
  rake_tasks do
8
- Seedbank.seeds_root = File.expand_path('db/seeds', Rails.root)
9
5
  Seedbank.load_tasks
10
6
  end
11
-
12
7
  end
13
- end
8
+ end
@@ -1,33 +1,10 @@
1
+ # frozen_string_literal: true
1
2
  module Seedbank
2
3
  class Runner
3
-
4
4
  def initialize
5
5
  @_memoized = {}
6
6
  end
7
7
 
8
- def let(name, &block)
9
- name = String(name)
10
-
11
- raise ArgumentError.new("#{name} is already defined") if respond_to?(name, true)
12
-
13
- __eigenclass.instance_exec(name) do |name|
14
- define_method(name) do
15
- @_memoized.fetch(name) { |key| @_memoized[key] = instance_eval(&block) }
16
- end
17
- end
18
-
19
- end
20
-
21
- def let!(name, &block)
22
- let(name, &block)
23
- send name
24
- end
25
-
26
- def evaluate(seed_task, seed_file)
27
- @_seed_task = seed_task
28
- instance_eval(File.read(seed_file), seed_file)
29
- end
30
-
31
8
  # Run this seed after the specified dependencies have run
32
9
  # @param dependencies [Array] seeds to run before the block is executed
33
10
  #
@@ -42,26 +19,36 @@ module Seedbank
42
19
  #
43
20
  # Would look for a db/seeds/shared/users.seeds.rb seed and execute it.
44
21
  def after(*dependencies, &block)
45
- dependencies.flatten!
46
- dependencies.map! { |dep| "db:seed:#{dep}"}
47
- dependent_task_name = @_seed_task.name + ':body'
22
+ depends_on = dependencies.flat_map { |dep| "db:seed:#{dep}" }
23
+ dependent_task_name = @_seed_task.name + ':body'
48
24
 
49
25
  if Rake::Task.task_defined?(dependent_task_name)
50
26
  dependent_task = Rake::Task[dependent_task_name]
51
27
  else
52
- dependent_task = Rake::Task.define_task(dependent_task_name => dependencies, &block)
28
+ dependent_task = Rake::Task.define_task(dependent_task_name => depends_on, &block)
53
29
  end
54
30
 
55
31
  dependent_task.invoke
56
32
  end
57
33
 
58
- private
34
+ def let(name, &block)
35
+ name = String(name)
36
+
37
+ raise ArgumentError, "#{name} is already defined" if respond_to?(name, true)
59
38
 
60
- def __eigenclass
61
- class << self
62
- self
39
+ define_singleton_method(name) do
40
+ @_memoized.fetch(name) { |key| @_memoized[key] = instance_exec(&block) }
63
41
  end
64
42
  end
65
43
 
44
+ def let!(name, &block)
45
+ let(name, &block)
46
+ public_send name
47
+ end
48
+
49
+ def evaluate(seed_task, seed_file)
50
+ @_seed_task = seed_task
51
+ instance_eval(File.read(seed_file), seed_file)
52
+ end
66
53
  end
67
54
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Seedbank
2
- VERSION = "0.4.0"
3
+ VERSION = '0.5.0'
3
4
  end
data/lib/seedbank.rb CHANGED
@@ -1,22 +1,31 @@
1
+ # frozen_string_literal: true
1
2
  require 'seedbank/dsl'
2
3
  require 'seedbank/runner'
3
4
 
4
5
  module Seedbank
5
-
6
6
  class << self
7
+ attr_writer :application_root, :seeds_root, :nesting, :matcher
7
8
 
8
- attr_writer :seeds_root
9
+ def application_root
10
+ @application_root ||= Pathname.new(Rake.application.original_dir)
11
+ end
9
12
 
10
13
  def seeds_root
11
- @seeds_root ||= 'db/seeds'
14
+ @seeds_root ||= File.join(application_root, 'db', 'seeds')
15
+ end
16
+
17
+ def nesting
18
+ @nesting ||= 2
12
19
  end
13
20
 
21
+ def matcher
22
+ @matcher ||= '*.seeds.rb'
23
+ end
14
24
  end
15
25
 
16
26
  def self.load_tasks
17
- Dir[File.expand_path("tasks/*.rake", File.dirname(__FILE__))].each { |ext| load ext }
27
+ Dir[File.expand_path('../tasks/*.rake', __FILE__)].each { |ext| load ext }
18
28
  end
19
29
 
20
30
  require 'seedbank/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
21
-
22
- end
31
+ end
data/lib/tasks/seed.rake CHANGED
@@ -1,42 +1,38 @@
1
+ # frozen_string_literal: true
1
2
  namespace :db do
2
-
3
- include Seedbank::DSL
4
-
5
- base_dependencies = ['db:seed:original']
3
+ using Seedbank::DSL
6
4
  override_dependency = ['db:seed:common']
7
5
 
8
6
  namespace :seed do
9
7
  # Create seed tasks for all the seeds in seeds_path and add them to the dependency
10
8
  # list along with the original db/seeds.rb.
11
- common_dependencies = glob_seed_files_matching('*.seeds.rb').sort.map { |seed_file| seed_task_from_file(seed_file) }
9
+ common_dependencies = seed_tasks_matching(Seedbank.matcher)
10
+
11
+ # Only add the original seeds if db/seeds.rb exists.
12
+ if original_seeds_file
13
+ define_seed_task original_seeds_file, :original
14
+ common_dependencies.unshift('db:seed:original')
15
+ end
12
16
 
13
- desc "Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb."
14
- task 'common' => base_dependencies + common_dependencies
17
+ desc "Load the seed data from db/seeds.rb and db/seeds/#{Seedbank.matcher}."
18
+ task 'common' => common_dependencies
15
19
 
16
20
  # Glob through the directories under seeds_path and create a task for each adding it to the dependency list.
17
21
  # Then create a task for the environment
18
22
  glob_seed_files_matching('/*/').each do |directory|
19
23
  environment = File.basename(directory)
20
24
 
21
- environment_dependencies = glob_seed_files_matching(environment, '*.seeds.rb').sort.map { |seed_file| seed_task_from_file(seed_file) }
25
+ environment_dependencies = seed_tasks_matching(environment, Seedbank.matcher)
22
26
 
23
- desc "Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/*.seeds.rb."
27
+ desc "Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/#{environment}/#{Seedbank.matcher}."
24
28
  task environment => ['db:seed:common'] + environment_dependencies
25
29
 
26
30
  override_dependency << "db:seed:#{environment}" if defined?(Rails) && Rails.env == environment
27
31
  end
28
-
29
- if Rails.version > '4'
30
- original_seeds_file = Rails.application.paths["db/seeds.rb"].existent.first
31
- elsif Rails.version > '3.1'
32
- original_seeds_file = Rails.application.paths["db/seeds"].existent.first
33
- else
34
- original_seeds_file = Rails.root.join("db","seeds").children.first.to_s
35
- end
36
- define_seed_task original_seeds_file, :original if original_seeds_file
37
32
  end
38
33
 
39
34
  # Override db:seed to run all the common and environments seeds plus the original db:seed.
40
- desc 'Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env.'
41
- override_seed_task :seed => override_dependency
35
+ desc %(Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/ENVIRONMENT/#{Seedbank.matcher}.
36
+ ENVIRONMENT is the current Rails.env.)
37
+ override_seed_task seed: override_dependency
42
38
  end