Class: Oaken::Loader
- Inherits:
-
Object
- Object
- Oaken::Loader
- Defined in:
- lib/oaken/loader.rb
Defined Under Namespace
Classes: Type
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#locator ⇒ Object
readonly
Returns the value of attribute locator.
-
#lookup_paths ⇒ Object
readonly
Returns the value of attribute lookup_paths.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
-
#defaults(**defaults) ⇒ Object
Allow assigning defaults across types.
- #defaults_for(*keys) ⇒ Object
- #definition_location ⇒ Object
- #glob(identifier) ⇒ Object
-
#initialize(lookup_paths: nil, locator: Type, provider: Oaken::Stored::ActiveRecord, context: Oaken::Seeds) ⇒ Loader
constructor
A new instance of Loader.
-
#load_seed ⇒ Object
Mirrors ‘bin/rails db:seed`.
-
#register(type, as: nil) ⇒ Object
Register a model class via ‘Oaken.loader.context`.
-
#replant_seed ⇒ Object
Mirrors ‘bin/rails db:seed:replant`.
-
#seed(*identifiers) ⇒ Object
Set up a general seed rule or perform a one-off seed for a test file.
- #test_setup ⇒ Object
-
#with(**overrides) ⇒ Object
Instantiate a new loader with all its attributes and any specified ‘overrides`.
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
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/oaken/loader.rb', line 6 def context @context end |
#locator ⇒ Object (readonly)
Returns the value of attribute locator.
6 7 8 |
# File 'lib/oaken/loader.rb', line 6 def locator @locator end |
#lookup_paths ⇒ Object (readonly)
Returns the value of attribute lookup_paths.
6 7 8 |
# File 'lib/oaken/loader.rb', line 6 def lookup_paths @lookup_paths end |
#provider ⇒ Object (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_location ⇒ Object
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_seed ⇒ Object
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 Account
register Account::Job
register Account::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_seed ⇒ Object
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_setup ⇒ Object
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 |