sourced-component 0.1.4 → 0.1.5
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 +4 -4
- data/CHANGELOG.md +1 -0
- data/README.md +18 -1
- data/lib/sourced/component/dir_source.rb +1 -1
- data/lib/sourced/component/graph.rb +1 -0
- data/lib/sourced/component/reconfiguration.rb +1 -1
- data/lib/sourced/component/tree.rb +2 -1
- data/lib/sourced/component/version.rb +1 -1
- data/lib/sourced/component.rb +22 -8
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2e8d05bfe496301a5a84059231eb13a8cfaf70346c8d3a1a8da02de089eab3bd
|
|
4
|
+
data.tar.gz: 31b20a580a27b4d50d63a913b638f0ea18c95cae9e87e7296ad5533e1b1741c4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 30f99a8a11fabae8504d11f8e21505a67f08fe874c012e4c7878da7bd50f7dadf46caf417a126d477fa26d7ffdcf5f95df14166d8ead0b4def279692bd842a5b
|
|
7
|
+
data.tar.gz: 6215cdb4cef9d5f15193d2e57c07dee72787314bf83ae2b8a0cd2cf9e24c52e71c9f4fb4203e39c1d046dcdbf50a7bcc1c6e6271e4af0b5ea85732a2201e88be
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
- `#declare(key, type, description: '...')`: describe what a component is for. Read back with `#description`, and in `#tree` and `#graph`, to generate documentation or CLIs from a component tree
|
|
3
4
|
- `#on_dir_load { |source, constants| }`: hear of every constant the files of a `#from_dir` define, installable or not, each time they install or reload
|
|
4
5
|
- `#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
|
|
5
6
|
- `#from_dir(glob)`: declare components from the files in a directory, with `.__install_component`, and reconfigure them when the tree's Zeitwerk loader reloads
|
data/README.md
CHANGED
|
@@ -47,7 +47,7 @@ Requires Ruby 3.2+. Types are [Plumb](https://github.com/ismasan/plumb) types.
|
|
|
47
47
|
|
|
48
48
|
## Declaring components
|
|
49
49
|
|
|
50
|
-
`#declare(key, type = Any, &default)` adds a typed node to the tree. Values are parsed through the declared type when they're built, so a component that builds the wrong thing fails the boot.
|
|
50
|
+
`#declare(key, type = Any, description: nil, &default)` adds a typed node to the tree. Values are parsed through the declared type when they're built, so a component that builds the wrong thing fails the boot.
|
|
51
51
|
|
|
52
52
|
```ruby
|
|
53
53
|
T = Sourced::Component::T # Plumb::Types
|
|
@@ -64,6 +64,20 @@ A block is a default implementation: a singleton built once, with no dependencie
|
|
|
64
64
|
App.declare('logger', T::Interface[:info]) { Logger.new($stdout) }
|
|
65
65
|
```
|
|
66
66
|
|
|
67
|
+
### Descriptions
|
|
68
|
+
|
|
69
|
+
A description says what a component is for. It's read back from the node, `#tree` and `#graph`, so an app can generate documentation, or a CLI, from its own configuration.
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
App.declare('vault', Vault, description: "A secure vault for this app's secrets")
|
|
73
|
+
|
|
74
|
+
App.node('vault').description # => "A secure vault for this app's secrets"
|
|
75
|
+
|
|
76
|
+
App.graph.components.each { |c| puts "#{c[:key]}\t#{c[:type_name]}\t#{c[:description]}" }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
It's `nil` for namespaces and components declared without one. Re-declaring a component in [`#reconfigure`](#re-configuring-a-booted-tree) replaces its description, or clears it if none is given, without recycling the component.
|
|
80
|
+
|
|
67
81
|
### Nested keys
|
|
68
82
|
|
|
69
83
|
Dot-separated keys build the tree. Missing intermediate segments are created as *namespaces*: nodes without a type or an implementation, which are skipped by the lifecycle.
|
|
@@ -819,6 +833,7 @@ node.parent # => the my_lib component
|
|
|
819
833
|
node.root # => App
|
|
820
834
|
node.owner # => the component that declared it
|
|
821
835
|
node.type # => the declared type
|
|
836
|
+
node.description # => what it's for, as given to #declare. nil if not described
|
|
822
837
|
node.implementation # => deps, mode (:singleton, :dynamic or :alias) and the implementing component
|
|
823
838
|
node.dep_nodes # => the components it depends on, wildcards included. See #graph for keys
|
|
824
839
|
node.children # => { segment => Component }
|
|
@@ -861,6 +876,7 @@ store.key # => "store"
|
|
|
861
876
|
store.path # => "my_lib.store"
|
|
862
877
|
store.type # => the declared type
|
|
863
878
|
store.type_name # => "Store"
|
|
879
|
+
store.description # => as given to #declare. nil if not described
|
|
864
880
|
store.namespace # => false
|
|
865
881
|
store.mounted # => false, true for mounted components
|
|
866
882
|
store.implemented # => true
|
|
@@ -922,6 +938,7 @@ graph.to_h
|
|
|
922
938
|
# key: 'my_lib.store', # full path from the root
|
|
923
939
|
# type: <Plumb type>, # the declared type
|
|
924
940
|
# type_name: 'Interface[append]', # readable version of it
|
|
941
|
+
# description: 'Where events go', # as given to #declare. nil if not described
|
|
925
942
|
# implemented: true,
|
|
926
943
|
# mode: :singleton, # :singleton, :dynamic or :alias. nil if not implemented
|
|
927
944
|
# status: :built,
|
|
@@ -181,7 +181,7 @@ module Sourced
|
|
|
181
181
|
# A declarer for a key under this one
|
|
182
182
|
def [](ckey) = Declarer.new(@owner, "#{key}.#{ckey}")
|
|
183
183
|
|
|
184
|
-
def declare(type = T::Any, &default) = tap { @owner.declare(key, type, &default) }
|
|
184
|
+
def declare(type = T::Any, description: nil, &default) = tap { @owner.declare(key, type, description:, &default) }
|
|
185
185
|
def component!(...) = tap { @owner.component!(key, ...) }
|
|
186
186
|
def component(...) = tap { @owner.component(key, ...) }
|
|
187
187
|
def config!(...) = tap { @owner.config!(key, ...) }
|
|
@@ -7,6 +7,7 @@ module Sourced
|
|
|
7
7
|
# key: 'sourced.db', # the component's full path from the root
|
|
8
8
|
# type: <Plumb type>,
|
|
9
9
|
# type_name: 'Interface[exec]',
|
|
10
|
+
# description: 'The database', # as given to Component#declare. nil if not described
|
|
10
11
|
# implemented: true,
|
|
11
12
|
# mode: :singleton, # nil if not implemented
|
|
12
13
|
# status: :built,
|
|
@@ -14,7 +14,7 @@ module Sourced
|
|
|
14
14
|
# if anything raises. The members drive both the capture and the restore, so a new one can't
|
|
15
15
|
# be captured and then forgotten on the way back
|
|
16
16
|
NodeState = Data.define(
|
|
17
|
-
:implementation, :type, :implicit, :deps, :dep_nodes, :deferred, :held, :status, :value,
|
|
17
|
+
:implementation, :type, :description, :implicit, :deps, :dep_nodes, :deferred, :held, :status, :value,
|
|
18
18
|
:recycle_to, :children, :index
|
|
19
19
|
)
|
|
20
20
|
|
|
@@ -13,13 +13,14 @@ module Sourced
|
|
|
13
13
|
class Tree < Data.define(:status, :root)
|
|
14
14
|
# key: the node's segment, ex. 'db'. nil for the root of a tree
|
|
15
15
|
# path: the full path from the root, ex. 'sourced.db'. nil for the root of a tree
|
|
16
|
+
# description: what the component is for, as given to Component#declare. nil if not described
|
|
16
17
|
# namespace: whether it has no type and no implementation
|
|
17
18
|
# mounted: whether it's a component mounted here (it owns itself)
|
|
18
19
|
# deferred: whether the root's start! skips it (see Component#defer)
|
|
19
20
|
# owner: the full path of the component that declared it. nil for the root
|
|
20
21
|
# implementer: the full path of the component that implemented it. nil for the root, or if not implemented
|
|
21
22
|
class Node < Data.define(
|
|
22
|
-
:key, :path, :type, :type_name, :namespace, :mounted, :implemented, :mode, :status, :deferred, :owner, :implementer, :children
|
|
23
|
+
:key, :path, :type, :type_name, :description, :namespace, :mounted, :implemented, :mode, :status, :deferred, :owner, :implementer, :children
|
|
23
24
|
)
|
|
24
25
|
# Whether it's implemented by a component other than the one that declared it, ex. an app overriding a library's component
|
|
25
26
|
def overridden? = implemented && owner != implementer
|
data/lib/sourced/component.rb
CHANGED
|
@@ -45,11 +45,15 @@ module Sourced
|
|
|
45
45
|
# What #from_dir needs from a code loader, ex. a Zeitwerk::Loader (2.6.13+)
|
|
46
46
|
LoaderInterface = Plumb::Types::Interface[:cpath_expected_at, :on_setup]
|
|
47
47
|
|
|
48
|
+
# What #declare takes as a component's description
|
|
49
|
+
Description = Plumb::Types::String.nullable
|
|
50
|
+
|
|
48
51
|
# key: local segment, ex. 'db'. nil for a root that isn't mounted anywhere
|
|
49
52
|
# owner: the component that declared this node. Standalone components own themselves
|
|
50
53
|
# type: the declared type. Any for implicit nodes created as intermediate segments (namespaces)
|
|
51
54
|
# index: every descendant, by relative key, ex. { 'sourced' => <Component>, 'sourced.db' => <Component> }
|
|
52
|
-
|
|
55
|
+
# description: what the component is for, as given to #declare. nil for namespaces and undescribed components
|
|
56
|
+
attr_reader :key, :parent, :owner, :type, :description, :implementation, :status, :value, :children, :index, :boot_status
|
|
53
57
|
|
|
54
58
|
# notifier: receives lifecycle events. See Component::Notifier and Component::Events.
|
|
55
59
|
# The root's notifier receives the events of the whole tree, so a mounted component uses its root's.
|
|
@@ -61,6 +65,7 @@ module Sourced
|
|
|
61
65
|
@owner = owner || self
|
|
62
66
|
@implicit = Plumb::Undefined == type # not declared with a type: a namespace, unless implemented
|
|
63
67
|
@type = @implicit ? Plumb::Types::Any : Plumb::Composable.wrap(type)
|
|
68
|
+
@description = nil
|
|
64
69
|
@implementation = nil
|
|
65
70
|
@status = :open
|
|
66
71
|
@value = nil
|
|
@@ -149,8 +154,12 @@ module Sourced
|
|
|
149
154
|
|
|
150
155
|
# Declare a typed node. Dot-separated keys build the tree, creating intermediate (namespace) nodes as needed.
|
|
151
156
|
# An optional block provides a default singleton implementation.
|
|
157
|
+
# An optional description says what the component is for, for documentation, CLIs, etc. See #tree and #graph
|
|
152
158
|
# comp.declare('sourced.db.logger', Logger) { Logger.new(STDOUT) }
|
|
153
|
-
|
|
159
|
+
# comp.declare('vault', Vault, description: "A secure vault for this app's secrets")
|
|
160
|
+
def declare(ckey, type = T::Any, description: nil, &default)
|
|
161
|
+
description = Description.parse(description)
|
|
162
|
+
|
|
154
163
|
synchronize do
|
|
155
164
|
raise LockedComponentError, "can't declare #{ckey} in a locked component" if declarations_locked?
|
|
156
165
|
|
|
@@ -159,15 +168,16 @@ module Sourced
|
|
|
159
168
|
node = branch.children[leaf]
|
|
160
169
|
if node.nil?
|
|
161
170
|
node = branch.attach(leaf, Component.new(owner: self, type:))
|
|
171
|
+
node.declare_type!(type, description)
|
|
162
172
|
elsif !node.owner.equal?(self)
|
|
163
173
|
raise OwnershipError, ownership_message(node)
|
|
164
174
|
elsif !node.implicit? && reconf.nil?
|
|
165
175
|
raise DeclarationOverrideError, "#{node.path} is already declared"
|
|
166
176
|
else
|
|
167
177
|
# Idempotent while reconfiguring: an unchanged component keeps its value and status,
|
|
168
|
-
# and a changed type marks it to be recycled
|
|
178
|
+
# and a changed type marks it to be recycled. A changed description doesn't: it's replaced
|
|
169
179
|
was = node.implicit? ? nil : node.type_name
|
|
170
|
-
node.declare_type!(type) # an implicit namespace this component created, now with a type
|
|
180
|
+
node.declare_type!(type, description) # an implicit namespace this component created, now with a type
|
|
171
181
|
reconf&.changed!(node) if was && was != node.type_name
|
|
172
182
|
end
|
|
173
183
|
new_to_the_tree = reconf.nil? || reconf.new?(node)
|
|
@@ -754,7 +764,7 @@ module Sourced
|
|
|
754
764
|
# Namespaces without an implementation are left out.
|
|
755
765
|
# graph = comp.graph
|
|
756
766
|
# graph.status # => :built, the root's status
|
|
757
|
-
# graph.components # => [{ key: 'logger', type:, type_name:, implemented:, mode:, status:, deps:, missing:, dependents:, provider: }, ...]
|
|
767
|
+
# graph.components # => [{ key: 'logger', type:, type_name:, description:, implemented:, mode:, status:, deps:, missing:, dependents:, provider: }, ...]
|
|
758
768
|
# graph.to_mermaid # => a Mermaid flowchart
|
|
759
769
|
# See Component::Graph
|
|
760
770
|
def graph
|
|
@@ -772,6 +782,7 @@ module Sourced
|
|
|
772
782
|
key: n.path,
|
|
773
783
|
type: n.type,
|
|
774
784
|
type_name: n.type_name,
|
|
785
|
+
description: n.description,
|
|
775
786
|
implemented: !impl.nil?,
|
|
776
787
|
mode: impl&.mode,
|
|
777
788
|
status: n.status,
|
|
@@ -804,6 +815,7 @@ module Sourced
|
|
|
804
815
|
path:,
|
|
805
816
|
type:,
|
|
806
817
|
type_name:,
|
|
818
|
+
description:,
|
|
807
819
|
namespace: namespace?,
|
|
808
820
|
mounted: mounted?,
|
|
809
821
|
implemented: !implementation.nil?,
|
|
@@ -841,9 +853,10 @@ module Sourced
|
|
|
841
853
|
|
|
842
854
|
private def synchronize(&) = root.lock.synchronize(&)
|
|
843
855
|
|
|
844
|
-
protected def declare_type!(type)
|
|
856
|
+
protected def declare_type!(type, description)
|
|
845
857
|
@implicit = false
|
|
846
858
|
@type = Plumb::Composable.wrap(type)
|
|
859
|
+
@description = description&.dup&.freeze
|
|
847
860
|
end
|
|
848
861
|
|
|
849
862
|
# Back to an implicit namespace: a dropped component that still has children (see #reconfigure).
|
|
@@ -852,6 +865,7 @@ module Sourced
|
|
|
852
865
|
protected def undeclare_type!
|
|
853
866
|
@implicit = true
|
|
854
867
|
@type = Plumb::Types::Any
|
|
868
|
+
@description = nil
|
|
855
869
|
@deps = [].freeze
|
|
856
870
|
@dep_nodes = [].freeze
|
|
857
871
|
@implementation.tap { @implementation = nil }
|
|
@@ -897,7 +911,7 @@ module Sourced
|
|
|
897
911
|
# copied; everything else is frozen, or a reference it has to keep as it is — its value above all
|
|
898
912
|
def capture_state
|
|
899
913
|
Reconfiguration::NodeState.new(
|
|
900
|
-
implementation:, type:, implicit: implicit?, deps:, dep_nodes:, deferred: deferred?,
|
|
914
|
+
implementation:, type:, description:, implicit: implicit?, deps:, dep_nodes:, deferred: deferred?,
|
|
901
915
|
held: held?, status:, value:, recycle_to:, children: children.dup, index: index.dup
|
|
902
916
|
)
|
|
903
917
|
end
|
|
@@ -911,7 +925,7 @@ module Sourced
|
|
|
911
925
|
# Only #capture_state and #restore_state! use these: a reconfiguration rolling back is the one
|
|
912
926
|
# thing that writes a node's state wholesale
|
|
913
927
|
private attr_reader :deps
|
|
914
|
-
private attr_writer :implementation, :type, :implicit, :deps, :dep_nodes, :deferred, :held,
|
|
928
|
+
private attr_writer :implementation, :type, :description, :implicit, :deps, :dep_nodes, :deferred, :held,
|
|
915
929
|
:status, :value, :recycle_to, :children, :index
|
|
916
930
|
|
|
917
931
|
# The status a recycle is bringing this node back to, remembered before anything is torn down.
|
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.
|
|
4
|
+
version: 0.1.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ismael Celis
|
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
102
102
|
- !ruby/object:Gem::Version
|
|
103
103
|
version: '0'
|
|
104
104
|
requirements: []
|
|
105
|
-
rubygems_version: 4.0.
|
|
105
|
+
rubygems_version: 4.0.20
|
|
106
106
|
specification_version: 4
|
|
107
107
|
summary: A tree of nested, typed components with dependencies and a managed lifecycle.
|
|
108
108
|
test_files: []
|