Class: Oaken::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/oaken/loader.rb

Defined Under Namespace

Classes: Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup_paths: nil, locator: Type, provider: Oaken::Stored::ActiveRecord, context: Oaken::Seeds) ⇒ Loader

Returns a new instance of Loader.



9
10
11
12
13
# File 'lib/oaken/loader.rb', line 9

def initialize(lookup_paths: nil, locator: Type, provider: Oaken::Stored::ActiveRecord, context: Oaken::Seeds)
  @setup = nil
  @lookup_paths, @locator, @provider, @context = Array(lookup_paths).dup, locator, provider, context
  @defaults = {}.with_indifferent_access
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/oaken/loader.rb', line 6

def context
  @context
end

#locatorObject (readonly)

Returns the value of attribute locator.



6
7
8
# File 'lib/oaken/loader.rb', line 6

def locator
  @locator
end

#lookup_pathsObject (readonly)

Returns the value of attribute lookup_paths.



6
7
8
# File 'lib/oaken/loader.rb', line 6

def lookup_paths
  @lookup_paths
end

#providerObject (readonly)

Returns the value of attribute provider.



6
7
8
# File 'lib/oaken/loader.rb', line 6

def provider
  @provider
end

Instance Method Details

#defaults(**defaults) ⇒ Object

Allow assigning defaults across types.



23
# File 'lib/oaken/loader.rb', line 23

def defaults(**defaults) = @defaults.merge!(**defaults)

#defaults_for(*keys) ⇒ Object



24
# File 'lib/oaken/loader.rb', line 24

def defaults_for(*keys)  = @defaults.slice(*keys)

#definition_locationObject



84
85
86
87
# File 'lib/oaken/loader.rb', line 84

def definition_location
  # The first line referencing LABEL happens to be the line in the seed file.
  caller_locations(3, 6).find { _1.base_label == LABEL }
end

#glob(identifier) ⇒ Object



80
81
82
# File 'lib/oaken/loader.rb', line 80

def glob(identifier)
  Pathname.glob lookup_paths.map { File.join _1, "#{identifier}{,/**/*}.rb" }
end

#load_seedObject

Mirrors ‘bin/rails db:seed`.



55
56
57
# File 'lib/oaken/loader.rb', line 55

def load_seed
  Rails.application.load_seed
end

#register(type, as: nil) ⇒ Object

Register a model class via ‘Oaken.loader.context`. Note: Oaken’s auto-register means you don’t need to call ‘register` often yourself.

register 
register ::Job
register ::Job::Task

Oaken uses ‘name.tableize.tr(“/”, “_”)` for the method names, so they’re ‘accounts`, `account_jobs`, and `account_job_tasks`, respectively.

You can also pass an explicit ‘as:` option:

register User, as: :something_else


43
44
45
46
# File 'lib/oaken/loader.rb', line 43

def register(type, as: nil)
  stored = provider.new(self, type)
  context.define_method(as || type.name.tableize.tr("/", "_")) { stored }
end

#replant_seedObject

Mirrors ‘bin/rails db:seed:replant`.



49
50
51
52
# File 'lib/oaken/loader.rb', line 49

def replant_seed
  ActiveRecord::Tasks::DatabaseTasks.truncate_all
  load_seed
end

#seed(*identifiers) ⇒ Object

Set up a general seed rule or perform a one-off seed for a test file.

You can set up a general seed rule in ‘db/seeds.rb` like this:

Oaken.seed :accounts # Seeds from `db/seeds/accounts{,/**/*}.rb` and `db/seeds/<Rails.env>/accounts{,/**/*}.rb`

Then if you need a test specific scenario, we recommend putting them in ‘db/seeds/test/cases`.

Say you have ‘db/seeds/test/cases/pagination.rb`, you can load it like this:

# test/integration/pagination_test.rb
class PaginationTest < ActionDispatch::IntegrationTest
  setup { seed "cases/pagination" }
end


73
74
75
76
77
78
# File 'lib/oaken/loader.rb', line 73

def seed(*identifiers)
  setup

  identifiers.flat_map { glob _1 }.each { load_one _1 }
  self
end

#test_setupObject



26
27
28
# File 'lib/oaken/loader.rb', line 26

def test_setup
  Oaken::TestSetup.new self
end

#with(**overrides) ⇒ Object

Instantiate a new loader with all its attributes and any specified ‘overrides`. See #new for defaults.

Oaken.loader.with(root: "test/fixtures") # `root` returns "test/fixtures" here


18
19
20
# File 'lib/oaken/loader.rb', line 18

def with(**overrides)
  self.class.new(lookup_paths:, locator:, provider:, context:, **overrides)
end