lib/seedbank/dsl.rb in seedbank-0.1.3 vs lib/seedbank/dsl.rb in seedbank-0.2.0.pre
- old
+ new
@@ -1,36 +1,45 @@
module Seedbank
module DSL
def self.included(base)
- Rake::Task.extend(Seedbank::RenameTask)
+ Rake::Task.send(:include, Seedbank::RenameTask)
Rake::Application.send(:include, Seedbank::TaskManager)
end
- def override_task(*args, &block)
- name, params, deps = Rake.application.resolve_args(args.dup)
- fq_name = Rake.application.instance_variable_get(:@scope).dup.push(name).join(':')
- new_name = "#{fq_name}:original"
- Rake::Task.rename_task(fq_name, new_name)
- Rake::Task.define_task(*args, &block)
+ def override_seed_task(*args, &block)
+ Rake.application.override_task(*args, &block)
end
- # Creates a task namespaced in @seeds_path
- def define_seed_task(seed_file)
- relative_root = seed_file.sub(seeds_root + '/', '')
- scopes = File.dirname(relative_root).gsub(/^\./, '').split('/').unshift('seed')
+ def seed_task_from_file(seed_file)
+ scopes = scope_from_seed_file(seed_file)
fq_name = scopes.push(File.basename(seed_file, '.seeds.rb')).join(':')
+ args = Rake::Task.task_defined?('db:abort_if_pending_migrations') ? { fq_name => 'db:abort_if_pending_migrations' } : fq_name
- args = Rake::Task.task_defined?('db:abort_if_pending_migrations') ? { fq_name => 'db:abort_if_pending_migrations' } : fq_name
- task = Rake::Task.define_task(args) do |seed_task|
+ define_seed_task(seed_file, args)
+ end
+
+ def glob_seed_files_matching(*args, &block)
+ Dir.glob(File.join(seeds_root, *args), &block)
+ end
+
+ def define_seed_task(seed_file, *args)
+ task = Rake::Task.define_task(*args) do |seed_task|
Seedbank::Runner.new(seed_task).module_eval(File.read(seed_file)) if File.exist?(seed_file)
end
task.add_description "Load the seed data from #{seed_file}"
- fq_name
+ task.name
end
+ def scope_from_seed_file(seed_file)
+ dirname = Pathname.new(seed_file).dirname
+ return [] if dirname == seeds_root
+ relative = dirname.relative_path_from(seeds_root)
+ relative.to_s.split(File::Separator)
+ end
+
def seeds_root
- Seedbank.seeds_root
+ Pathname.new Seedbank.seeds_root
end
end
-end
\ No newline at end of file
+end