plugs 0.1.0 → 0.2.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: c81be8b0475267381d393f250def37b876ef358e534c5e645b1715ad11cfc019
4
- data.tar.gz: 1bd993051f7a21157998b22d392c2b1c603dafa924afde6dbd53aaed0abdeb6b
3
+ metadata.gz: b1b3807befe22040b899d38a6d974a45d3b25bc80d1ada5dfcb86a490cb4c329
4
+ data.tar.gz: 1d896a56d175b9f8f48ff35d35f918264d1fa63eddad0b98dfcc13394d27f2b0
5
5
  SHA512:
6
- metadata.gz: 5e1c50904d9e030755c37db1ef6041fdd9a58c4ac11987884c581efcb0063e105509b63712bd5f4b4a4f84229c4ad7f2bf1c3a5dabbcaabacb885000020e860b
7
- data.tar.gz: 0c5d1bda47cc590873c14847a31b34c84eeb5257e5c9f668f91fb0ad2c99b7b3c97d69d33f096b4a7fdb08216dd5af16559caec66c3e80f20f3355962a70630d
6
+ metadata.gz: 17def0a554be709a6c3304169003483ab1659add80aa5e4642c38236944c7664f8ff0d6f302bbcd43b289e039c0148cf4bb8607174c2d4ad03e3891c60e32a91
7
+ data.tar.gz: b5c038d5cfd10163808d3973f9c19054fc2f52d92b69801cb9057fd04d47a9683ee603cabe4a488dcb737f48ef04e61b55a4434b2eae4af6d03e5c7b484a6595
data/lib/plug.rb CHANGED
@@ -2,12 +2,13 @@
2
2
 
3
3
  module Plugs
4
4
  class Plug
5
- attr_reader :key, :result
5
+ attr_reader :key, :children
6
6
 
7
- def initialize(key:, eager: false, &block)
7
+ def initialize(key:, &block)
8
8
  @key = key
9
9
  @proc = block
10
- @result = eager ? block.call : nil
10
+ @result = nil
11
+ @children = []
11
12
  end
12
13
 
13
14
  def result
data/lib/plugs.rb CHANGED
@@ -1,38 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'plug'
4
+ require_relative 'sub_selector'
4
5
 
5
6
  module Plugs
6
- attr_accessor :plugs
7
+ attr_reader :plugs
8
+
9
+ class DuplicateKeyError < StandardError; end
10
+
11
+ def initialize(plugs:, keys:)
12
+ @plugs = plugs
13
+ @keys = keys
14
+ end
7
15
 
8
16
  def [](*keys)
9
- @plugs.values_at(*keys)
17
+ self.class.new(plugs: SubSelector.sub_select(plugs:, keys:), keys:)
18
+ end
19
+
20
+ def to_a
21
+ plugs.values.flatten.map(&:result)
22
+ end
23
+
24
+ def to_h
25
+ plugs.values.each_with_object({}) do |values, hash|
26
+ values.each do |plug|
27
+ hash[plug.key] ||= []
28
+ hash[plug.key] << plug.result
29
+ end
30
+ end
10
31
  end
11
32
 
12
33
  def self.included(base)
13
- @plugs = {}
14
34
  base.extend(ClassMethods)
15
35
  end
16
36
 
17
37
  module ClassMethods
18
- def [](*keys)
19
- instance = new
20
- instance.plugs = plugs.values_at(*keys).flatten
21
- instance
22
- end
23
-
24
38
  def plug(key, &block)
25
- plug = Plug.new(key:, eager: true, &block)
39
+ plug = Plug.new(key:, &block)
26
40
 
27
41
  plugs[key] ||= []
28
42
  plugs[key] << plug
43
+ plug_stack << plug
29
44
 
30
45
  plug.result
46
+
47
+ plug_stack.pop
48
+ plug_stack.last.children << plug if plug_stack.last
49
+
50
+ nil
51
+ end
52
+
53
+ def [](*keys)
54
+ new(plugs: SubSelector.sub_select(plugs:, keys:), keys:)
31
55
  end
32
56
 
33
57
  def plugs
34
58
  @plugs ||= {}
35
59
  @plugs
36
60
  end
61
+
62
+ def plug_stack
63
+ @plug_stack ||= []
64
+ @plug_stack
65
+ end
37
66
  end
38
67
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plugs
4
+ module Refinements
5
+ refine Hash do
6
+ def slice!(*keys)
7
+ keys.zip(fetch_values(*keys)).to_h
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'refinements'
4
+
5
+ module Plugs
6
+ module SubSelector
7
+ using Refinements
8
+
9
+ module_function
10
+
11
+ def sub_select(plugs:, keys:)
12
+ selection = {}
13
+
14
+ plugs.slice!(*keys).each_value do |parent_plugs|
15
+ parent_plugs.each do |parent_plug|
16
+ selection[parent_plug.key] ||= []
17
+ selection[parent_plug.key] << parent_plug unless selection[parent_plug.key].include?(parent_plug)
18
+
19
+ children(parent_plug:).each do |child_plug|
20
+ selection[child_plug.key] ||= []
21
+ selection[child_plug.key] << child_plug unless selection[child_plug.key].include?(child_plug)
22
+ end
23
+ end
24
+ end
25
+
26
+ selection
27
+ end
28
+
29
+ def children(parent_plug:)
30
+ children = []
31
+
32
+ parent_plug.children.each do |child_plug|
33
+ children << child_plug
34
+ children = [*children, *children(parent_plug: child_plug)]
35
+ end
36
+
37
+ children
38
+ end
39
+ end
40
+ end
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plugs
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plugs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi
@@ -10,7 +10,7 @@ cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: "Plugs are dependencies that are loosely coupled internally but externally
13
- appear as one entity such as a feature, config object or plugin. \nA plug is reusable,
13
+ appear as one entity such as a feature, configuration or plugin. \nA plug is reusable,
14
14
  shareable and overridable.\n"
15
15
  email:
16
16
  - maediprichard@gmail.com
@@ -20,6 +20,8 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/plug.rb
22
22
  - lib/plugs.rb
23
+ - lib/refinements.rb
24
+ - lib/sub_selector.rb
23
25
  - lib/version.rb
24
26
  homepage: https://github.com/raindeer-rb/plugs
25
27
  licenses: []
@@ -42,5 +44,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
44
  requirements: []
43
45
  rubygems_version: 4.0.6
44
46
  specification_version: 4
45
- summary: Keep code loosely coupled, keep features tightly coupled
47
+ summary: Keep code loosely coupled. Keep features tightly coupled
46
48
  test_files: []