Class: Plantae::Seeder

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/plantae/seeder.rb

Overview

Extend this class to create your own seeders

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_public_methods_with_inline_jobsObject

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

.scenariosObject



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

#createObject

Runs all create blocks



92
93
94
# File 'lib/plantae/seeder.rb', line 92

def create
  run_callbacks :create
end

#destroyObject

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)

Parameters:

  • name (String, nil) (defaults to: nil)

    when given only run the scenario matching the given name and skip the create logic



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