Class: Plantae::Seeder
- Inherits:
-
Object
- Object
- Plantae::Seeder
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/plantae/seeder.rb
Overview
Extend this class to create your own seeders
Class Method Summary collapse
-
.all_public_methods_with_inline_jobs ⇒ Object
Run all public instance methos with the with_inline_jobs method.
-
.create(*args, &block) ⇒ Object
Define code to execute on create, this is used to create preconditions before running any scenarios.
-
.destroy(*args, &block) ⇒ Object
Define code to execute on destroy, this is used to clean up the items created through the seeder.
-
.scenario(name, &block) ⇒ Object
Create scenarios in your seeder using the following format:.
- .scenarios ⇒ Object
-
.with_inline_jobs(*names) ⇒ Object
Run the given methods with the ActiveJob inline adapter temporarily enabled.
Instance Method Summary collapse
-
#create ⇒ Object
Runs all create blocks.
-
#destroy ⇒ Object
Runs all destroy blocks.
-
#run(name = nil) ⇒ Object
Run scenario(s).
Class Method Details
.all_public_methods_with_inline_jobs ⇒ Object
Run all public instance methos with the with_inline_jobs method
60 61 62 |
# File 'lib/plantae/seeder.rb', line 60 def all_public_methods_with_inline_jobs with_inline_jobs(*public_instance_methods(false)) end |
.create(*args, &block) ⇒ Object
Define code to execute on create, this is used to create preconditions before running any scenarios.
80 81 82 |
# File 'lib/plantae/seeder.rb', line 80 def create(*args, &block) set_callback(:create, :before, *args, &block) end |
.destroy(*args, &block) ⇒ Object
Define code to execute on destroy, this is used to clean up the items created through the seeder.
86 87 88 |
# File 'lib/plantae/seeder.rb', line 86 def destroy(*args, &block) set_callback(:destroy, :before, *args, &block) end |
.scenario(name, &block) ⇒ Object
Create scenarios in your seeder using the following format:
scenario "this is my scenario" do
...
end
73 74 75 76 |
# File 'lib/plantae/seeder.rb', line 73 def scenario(name, &block) @scenarios ||= [] @scenarios << Scenario.new(name, &block) end |
.scenarios ⇒ Object
64 65 66 |
# File 'lib/plantae/seeder.rb', line 64 def scenarios @scenarios || [] end |
.with_inline_jobs(*names) ⇒ Object
Run the given methods with the ActiveJob inline adapter temporarily enabled
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/plantae/seeder.rb', line 17 def with_inline_jobs(*names) names.each do |name| m = instance_method(name) @@semaphore ||= Mutex.new @@in_mutex ||= false only_kwargs = m.parameters.count.positive? && m.parameters.all? { |i| i.first == :key } the_new_method = proc do |*args| the_code = proc do old_queue_adapter = ActiveJob::Base.queue_adapter ActiveJob::Base.queue_adapter = ActiveJobAdapter.new kwargs = args.last.is_a?(Hash) ? args.pop : {} if only_kwargs m.bind(self).call(**kwargs) else m.bind(self).call(*args, **kwargs) end ensure ActiveJob::Base.queue_adapter = old_queue_adapter end if @@in_mutex result = the_code.call else @@semaphore.synchronize do @@in_mutex = true result = the_code.call ensure @@in_mutex = false end end result end define_method(name, the_new_method) end end |
Instance Method Details
#create ⇒ Object
Runs all create blocks
92 93 94 |
# File 'lib/plantae/seeder.rb', line 92 def create run_callbacks :create end |
#destroy ⇒ Object
Runs all destroy blocks
97 98 99 |
# File 'lib/plantae/seeder.rb', line 97 def destroy run_callbacks :destroy end |
#run(name = nil) ⇒ Object
Run scenario(s)
104 105 106 107 108 109 110 111 |
# File 'lib/plantae/seeder.rb', line 104 def run(name = nil) create unless name.present? scenarios.select { |scenario| name.blank? || scenario.name == name } .each do |scenario| instance_exec(&scenario.block) end end |