Class: Seedbank::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/seedbank/runner.rb

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



4
5
6
# File 'lib/seedbank/runner.rb', line 4

def initialize
  @_memoized = {}
end

Instance Method Details

#after(*dependencies, &block) ⇒ Object

Run this seed after the specified dependencies have run If a block is specified the contents of the block are executed after all the dependencies have been executed.

If no block is specified just the dependencies are run. This makes it possible to create shared dependencies. For example

Would look for a db/seeds/shared/users.seeds.rb seed and execute it.

Examples:

db/seeds/production/users.seeds.rb

after 'shared:users'

Parameters:

  • dependencies (Array)

    seeds to run before the block is executed



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/seedbank/runner.rb', line 21

def after(*dependencies, &block)
  depends_on = dependencies.flat_map { |dep| "db:seed:#{dep}" }
  dependent_task_name = @_seed_task.name + ':body'

  if Rake::Task.task_defined?(dependent_task_name)
    dependent_task = Rake::Task[dependent_task_name]
  else
    dependent_task = Rake::Task.define_task(dependent_task_name => depends_on, &block)
  end

  dependent_task.invoke
end

#evaluate(seed_task, seed_file) ⇒ Object



49
50
51
52
# File 'lib/seedbank/runner.rb', line 49

def evaluate(seed_task, seed_file)
  @_seed_task = seed_task
  instance_eval(File.read(seed_file), seed_file)
end

#let(name, &block) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
# File 'lib/seedbank/runner.rb', line 34

def let(name, &block)
  name = String(name)

  raise ArgumentError, "#{name} is already defined" if respond_to?(name, true)

  define_singleton_method(name) do
    @_memoized.fetch(name) { |key| @_memoized[key] = instance_exec(&block) }
  end
end

#let!(name, &block) ⇒ Object



44
45
46
47
# File 'lib/seedbank/runner.rb', line 44

def let!(name, &block)
  let(name, &block)
  public_send name
end