hub_kernel 0.1.0 → 0.3.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: 0e433f46a0cd33d7236e9f57f150a4c8e59b778a320c4dcf1252093a405fbcce
4
- data.tar.gz: c8898bc61f4c65dd72a0008ca1dd75e36508102089636964c68da9ec4863115a
3
+ metadata.gz: 2e21318b337715e24c66ec08ab39f65b4a11aa4eb43783a05bf6f7548dd7f8fc
4
+ data.tar.gz: 36790eed17097fefd6b6c2eaa1a6c42b712b7b1cc9df510ad1b0b8f2e4136a3c
5
5
  SHA512:
6
- metadata.gz: a453b3e41b68ba7f7b9c06b31546fb92ec0d840e6f949657a5a5ec6a8d4b4708d88c3fd21963bd37fe9ebb224362a69e4d287d20f65c24eb275d557dacdee923
7
- data.tar.gz: 1eb7d7a09b26da2997ebee5145d148c5f98111fe8372b868e3bdb1290bc2decb401aa406fe6405844d9b0aa00527ef03101939482c970f0f7e3de35ad40da4d1
6
+ metadata.gz: 776e13a1c4fb5be4f499fe4a2647d845fb5bf9b972bd7e045b5fa4c754130bb633a0841230f2aeee684d017fc8d23be144db551915e84eb240a9458c5c8bc78d
7
+ data.tar.gz: 238d3e0722eb7c89ed1a32237a1d7edcf68933034aa98df541001ac8bbaccaaf81ee0d96d90029e4fed3cb523d4c80510ccbb72c9b3ae2d3948257e0a1e9f448
data/README.md CHANGED
@@ -1,8 +1,48 @@
1
1
  # HubKernel
2
- Short description and motivation.
2
+ Gives every hub in a Rails app one way to declare what it needs from another hub.
3
3
 
4
4
  ## Usage
5
- How to use my plugin.
5
+
6
+ ### Ports
7
+ A port is something a hub needs from outside itself. The hub declares it by name,
8
+ with the method its own code calls:
9
+
10
+ ```ruby
11
+ module Supplies
12
+ extend HubKernel::Ports
13
+
14
+ port :spend_recorder, as: :record_spend
15
+ end
16
+ ```
17
+
18
+ The app fills each port with any callable, usually another hub's method:
19
+
20
+ ```ruby
21
+ Rails.application.config.to_prepare do
22
+ Supplies.spend_recorder = Finance.method(:record_spend)
23
+ end
24
+ ```
25
+
26
+ `Supplies.record_spend(amount: 5)` then calls what the app filled the port with and
27
+ returns its answer. An error it raises reaches the caller unchanged. Calling a port the
28
+ app left unfilled raises `HubKernel::UnwiredPortError`, such as "Supplies' spend
29
+ recorder is not wired".
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.
6
46
 
7
47
  ## Installation
8
48
  Add this line to your application's Gemfile:
@@ -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
@@ -0,0 +1,32 @@
1
+ require "active_support/core_ext/string/inflections"
2
+ require "hub_kernel/hubs"
3
+
4
+ module HubKernel
5
+ class UnwiredPortError < StandardError; end
6
+
7
+ module Ports
8
+ def port(name, as:)
9
+ Hubs.add(self)
10
+ declared_ports << name
11
+ singleton_class.attr_writer name
12
+ define_singleton_method(name) { instance_variable_get(:"@#{name}") || unwired(name) }
13
+ define_singleton_method(as) { |*args, **options| public_send(name).call(*args, **options) }
14
+ end
15
+
16
+ def unwired_ports
17
+ declared_ports.reject { |name| instance_variable_get(:"@#{name}") }.map { |name| unwired_message(name) }
18
+ end
19
+
20
+ private
21
+
22
+ def declared_ports = @declared_ports ||= []
23
+
24
+ def unwired(name)
25
+ ->(*, **) { raise UnwiredPortError, unwired_message(name) }
26
+ end
27
+
28
+ def unwired_message(name) = "#{hub_name} #{name.to_s.tr("_", " ")} is not wired"
29
+
30
+ def hub_name = self.name.demodulize.then { |hub| hub.end_with?("s") ? "#{hub}'" : "#{hub}'s" }
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module HubKernel
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/hub_kernel.rb CHANGED
@@ -4,6 +4,7 @@ require "hub_kernel/action"
4
4
  require "hub_kernel/answer"
5
5
  require "hub_kernel/follow_up"
6
6
  require "hub_kernel/markup"
7
+ require "hub_kernel/ports"
7
8
 
8
9
  module HubKernel
9
10
  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.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tylercschneider
@@ -46,7 +46,9 @@ files:
46
46
  - lib/hub_kernel/engine.rb
47
47
  - lib/hub_kernel/events.rb
48
48
  - lib/hub_kernel/follow_up.rb
49
+ - lib/hub_kernel/hubs.rb
49
50
  - lib/hub_kernel/markup.rb
51
+ - lib/hub_kernel/ports.rb
50
52
  - lib/hub_kernel/version.rb
51
53
  homepage: https://github.com/tylercschneider/hub_kernel
52
54
  licenses: