sourced-component 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db15a3e8df75b3fd1e30d607d77d4a5812463f673c4530472ca32ad1b7bc66fc
4
- data.tar.gz: bde0065b4858b3a91898c2016068c1a1bec1d0d7cebc420f851cd51acce5269d
3
+ metadata.gz: 1a892eb22b720a4e29fcf85ef1412a8c396912a0568288ae72509f2e0c3490ec
4
+ data.tar.gz: 4841c6f448f7fe1b23643cc838729ac590b347a401d7e47dbdbf546bc19b4120
5
5
  SHA512:
6
- metadata.gz: 2268205f4ba2c92d53554e2b5a2b0d526d6c81df75cd193121a72bcf5ad921a7f4033baba1e5e5ba3bce8c2db133614a4e79742df4d35394974f1ba29bc7c69b
7
- data.tar.gz: bcd9972a3addfde922ad082991771357f96c6ac044f280c79c31255cbda1b0cea38ac2392454f91f39311edb697b49d292761d34401b9802da9690d5323e8484
6
+ metadata.gz: '09758a5ee69e20c50803398b948df6907ced997dd1263e1a2d7aa435ce8c811fb75b6a65f7affc367ac39b62347f88b82a5265fc793f84d0b36b31a058fad3df'
7
+ data.tar.gz: cdcdaa73c163546c71474c83f1dbd9b3985adb19a2111904970fefba587b0388f2a1ff93845aef1afd409550ae44fa21110852750d9dc1be5098c928f9afe81b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,6 @@
1
1
  ## [Unreleased]
2
2
 
3
+ - `#install_dirs!`: load the files of every `#from_dir` and declare their components on an open tree, before `#prepare!`, so classes loaded earlier (ex. by Zeitwerk's `#eager_load`) can inject them
3
4
  - `#from_dir(glob)`: declare components from the files in a directory, with `.__install_component`, and reconfigure them when the tree's Zeitwerk loader reloads
4
5
  - `Sourced::Component::Installable`: the default `.__install_component`, `c.declare(self).factory(self)`, and `.__abstract_component!` for base classes
5
6
  - `#from_dir(glob, root: dir)`, rooted at the directory it scans: each top-level directory is a branch, reloaded together
data/README.md CHANGED
@@ -529,6 +529,14 @@ end
529
529
  - **Dependencies are relative to the component `#from_dir` was called on**, like the keys of `#inject`, so siblings are referenced by their full key (`c['logger'].key`).
530
530
  - **Constants that don't respond to `.__install_component` are skipped**, and so are files the loader ignores.
531
531
  - **Files are loaded on the root's `#prepare!`**, not by `#from_dir`, so the components they inject are declared by then. The loader must be set up before that. If a file raises, none of the directory's components are left declared, so booting again after fixing it works.
532
+ - **`#install_dirs!` loads them earlier**, on the open tree, for an app that loads classes injecting them before it prepares the tree, ex. with Zeitwerk's `#eager_load`. The tree stays open, and `#prepare!` skips the sources already installed. It's for the root, and raises `LockedComponentError` once the tree is prepared:
533
+
534
+ ```ruby
535
+ App.from_dir('system/**/*.rb', root: 'system')
536
+ loader.setup
537
+ App.install_dirs! # declares 'users.create', from system/users/create.rb
538
+ loader.eager_load # so a class elsewhere can inject 'users.create' as it loads
539
+ ```
532
540
  - **Files can inject each other's components**, whichever loads first. While `#from_dir` loads files, injecting a key that isn't declared yet is resolved once every file has declared its components, and one that's still missing raises `UndeclaredComponentError` (on a reload, rolling it back). Everywhere else, injecting an undeclared key raises straight away.
533
541
  - **Keys:** `App.from_dir('app/queries/**/*.rb', root: 'app/queries', namespace: 'queries')` keys `app/queries/users/search.rb` as `queries.users.search`. A glob without wildcards is a directory's own `.rb` files.
534
542
  - **A whole directory, without its name:** with `root:` set to the directory scanned and no `namespace:`, each top-level directory is a branch of its own:
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sourced
4
4
  class Component
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
@@ -397,7 +397,8 @@ module Sourced
397
397
  # app.loader = loader # once, on the root: see #loader=
398
398
  # app.from_dir('lib/**/*.rb') # lib/queries/users/search.rb => 'lib.queries.users.search'
399
399
  # app.from_dir('app/queries/**/*.rb', root: 'app/queries', namespace: 'queries')
400
- # Files are loaded on the root's #prepare!, so the components they inject are declared by then.
400
+ # Files are loaded on the root's #prepare!, so the components they inject are declared by then,
401
+ # or earlier with #install_dirs!.
401
402
  # When the loader reloads, the branch above the glob's first wildcard is reconfigured from the new
402
403
  # constants (see #reconfigure). A failed reload leaves the tree as it was and calls +on_error+.
403
404
  def from_dir(glob, root: Dir.pwd, namespace: nil, on_error: DirSource::DEFAULT_ON_ERROR)
@@ -422,6 +423,26 @@ module Sourced
422
423
  self
423
424
  end
424
425
 
426
+ # Load the files of every #from_dir in the tree now, on an open tree, rather than on #prepare!,
427
+ # and declare their components. The tree stays open, so more can still be declared and
428
+ # implemented, and #prepare! skips the sources already installed. For an app that loads classes
429
+ # injecting those components before it prepares the tree, ex. with Zeitwerk's #eager_load:
430
+ # app.from_dir('system/**/*.rb', root: 'system')
431
+ # loader.setup
432
+ # app.install_dirs! # declares 'users.create', from system/users/create.rb
433
+ # loader.eager_load # so web/ classes can inject it
434
+ # The loader must be set up. If a file raises, none of the directory's components are left
435
+ # declared, and calling it again (or #prepare!) retries.
436
+ def install_dirs!
437
+ raise_mounted!
438
+ synchronize do
439
+ raise LockedComponentError, "can't install directories into a locked component" if locked?
440
+
441
+ [self, *index.values].each { |n| n.install_dir_sources! }
442
+ end
443
+ self
444
+ end
445
+
425
446
  # A node under this component, by relative key
426
447
  def node(ckey)
427
448
  index.fetch(ckey.to_s) { raise UndeclaredComponentError, "#{ckey} is not declared in #{path || 'this component'}" }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sourced-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis