hub_kernel 0.2.0 → 0.4.0

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: d0aadeed46c5d7c19209c32b3fb4de077a80ebb2670d5f5545918fe3f9421e14
4
- data.tar.gz: d3f57b2de645c719357101e13752b464b67c31ea38fa169281c7213c95c6f273
3
+ metadata.gz: 4b68695ef96166801769081d8d607907cc32e6d215f798b6404dc8b13a1faab2
4
+ data.tar.gz: 2fb12e7fa8b2cf1e8246eea5204ae7b38f61c6b41e668e07c753e7f55f899bf1
5
5
  SHA512:
6
- metadata.gz: 2b78566e883135e653f2638c2986ba74e14153443b56fd923a14a6dae50eef107acbdc9ccf6c96774d8c96fce7802497a2643f66c316f3186ef976a5bb031a85
7
- data.tar.gz: a939a5363771f77fedf2f4c647a5e67287a6bef22fac61665a5d818f5667ed6a066fda7227efbd8b8a115f17ddbd197e14a271b8d82005d559df2bd5a19d0fdc
6
+ metadata.gz: 4cecfcff2e7565d73b3c047ff191cc57a1f1f73056727a644a82bcc6f07517678d370c8876abb7c6c97e10197d663d2f0fa046a1430939e38583a5e89dda7f7f
7
+ data.tar.gz: 7a83ad99b5cc838650469b8e436d6a8a7f3d70e2ac37685206e1a1094f98c987dde0b29044b52dc5def44b2392e2b3eb045332819bbe160581c028c27ae7df36
data/README.md CHANGED
@@ -28,6 +28,22 @@ returns its answer. An error it raises reaches the caller unchanged. Calling a p
28
28
  app left unfilled raises `HubKernel::UnwiredPortError`, such as "Supplies' spend
29
29
  recorder is not wired".
30
30
 
31
+ ### The start check
32
+ Every hub that declares a port is on `HubKernel::Hubs.list`. Call
33
+ `HubKernel::Hubs.check!` after the app fills its ports, and the app refuses to start
34
+ while any port is unfilled:
35
+
36
+ ```ruby
37
+ Rails.application.config.to_prepare do
38
+ Supplies.spend_recorder = Finance.method(:record_spend)
39
+ HubKernel::Hubs.check!
40
+ end
41
+ ```
42
+
43
+ It raises `HubKernel::UnwiredPortError` naming every unfilled port with its hub, one per
44
+ line. Running it inside `to_prepare` checks again after every code reload, and a hub
45
+ declared again on a reload stays on the list once.
46
+
31
47
  ## Installation
32
48
  Add this line to your application's Gemfile:
33
49
 
@@ -0,0 +1,15 @@
1
+ module HubKernel
2
+ module Hubs
3
+ def self.add(hub)
4
+ list.reject! { |listed| listed.name == hub.name }
5
+ list << hub
6
+ end
7
+
8
+ def self.list = @list ||= []
9
+
10
+ def self.check!
11
+ unwired = list.flat_map(&:unwired_ports)
12
+ raise UnwiredPortError, unwired.join("\n") if unwired.any?
13
+ end
14
+ end
15
+ end
@@ -1,21 +1,32 @@
1
1
  require "active_support/core_ext/string/inflections"
2
+ require "hub_kernel/hubs"
2
3
 
3
4
  module HubKernel
4
5
  class UnwiredPortError < StandardError; end
5
6
 
6
7
  module Ports
7
8
  def port(name, as:)
9
+ Hubs.add(self)
10
+ declared_ports << name
8
11
  singleton_class.attr_writer name
9
12
  define_singleton_method(name) { instance_variable_get(:"@#{name}") || unwired(name) }
10
13
  define_singleton_method(as) { |*args, **options| public_send(name).call(*args, **options) }
11
14
  end
12
15
 
16
+ def unwired_ports
17
+ declared_ports.reject { |name| instance_variable_get(:"@#{name}") }.map { |name| unwired_message(name) }
18
+ end
19
+
13
20
  private
14
21
 
22
+ def declared_ports = @declared_ports ||= []
23
+
15
24
  def unwired(name)
16
- ->(*, **) { raise UnwiredPortError, "#{hub_name} #{name.to_s.tr("_", " ")} is not wired" }
25
+ ->(*, **) { raise UnwiredPortError, unwired_message(name) }
17
26
  end
18
27
 
28
+ def unwired_message(name) = "#{hub_name} #{name.to_s.tr("_", " ")} is not wired"
29
+
19
30
  def hub_name = self.name.demodulize.then { |hub| hub.end_with?("s") ? "#{hub}'" : "#{hub}'s" }
20
31
  end
21
32
  end
@@ -1,3 +1,3 @@
1
1
  module HubKernel
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hub_kernel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tylercschneider
@@ -44,8 +44,8 @@ files:
44
44
  - lib/hub_kernel/conformance/partial.rb
45
45
  - lib/hub_kernel/context.rb
46
46
  - lib/hub_kernel/engine.rb
47
- - lib/hub_kernel/events.rb
48
47
  - lib/hub_kernel/follow_up.rb
48
+ - lib/hub_kernel/hubs.rb
49
49
  - lib/hub_kernel/markup.rb
50
50
  - lib/hub_kernel/ports.rb
51
51
  - lib/hub_kernel/version.rb
@@ -1,36 +0,0 @@
1
- module HubKernel
2
- class UndeclaredEventError < StandardError; end
3
- class UnwiredEventError < StandardError; end
4
-
5
- module Events
6
- def declared_events
7
- instance_methods(false)
8
- end
9
-
10
- def on(name, &handler)
11
- raise UndeclaredEventError, "undeclared event :#{name}; declared events are #{declared_events}" unless declared_events.include?(name)
12
-
13
- handlers[name] << handler
14
- end
15
-
16
- def emit(name, payload)
17
- handlers[name].each { |handler| handler.call(payload) }
18
- end
19
-
20
- def verify_wired!
21
- return if unwired_events.empty?
22
-
23
- raise UnwiredEventError, "unwired events: #{unwired_events}"
24
- end
25
-
26
- private
27
-
28
- def unwired_events
29
- declared_events.reject { |name| handlers[name].any? }
30
- end
31
-
32
- def handlers
33
- @handlers ||= Hash.new { |table, name| table[name] = [] }
34
- end
35
- end
36
- end