plugs 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: c81be8b0475267381d393f250def37b876ef358e534c5e645b1715ad11cfc019
4
- data.tar.gz: 1bd993051f7a21157998b22d392c2b1c603dafa924afde6dbd53aaed0abdeb6b
3
+ metadata.gz: 591e9658be2f12d42b001438cf1769587613e0faee11b711802729c1954446a8
4
+ data.tar.gz: ac8b3e12b9f4219980c53c1dac2e9a0c9412ebe637669ce7a0bee9741e06f4fb
5
5
  SHA512:
6
- metadata.gz: 5e1c50904d9e030755c37db1ef6041fdd9a58c4ac11987884c581efcb0063e105509b63712bd5f4b4a4f84229c4ad7f2bf1c3a5dabbcaabacb885000020e860b
7
- data.tar.gz: 0c5d1bda47cc590873c14847a31b34c84eeb5257e5c9f668f91fb0ad2c99b7b3c97d69d33f096b4a7fdb08216dd5af16559caec66c3e80f20f3355962a70630d
6
+ metadata.gz: 3967854087c6bfebdd66ab3fb09f3042dabeae4797ab0ed5a532b34c04a9b3ea5db6bdc70a1b71950a04e240c42e0b50d1e3f42ae29faae597af0980b5f8e1e1
7
+ data.tar.gz: 127f2b45b30a81ce320b8c73755dcbbfef39d4acfa4f24671764ef155f167958dade67c3836c87a92d74273576be34b1d6833c2169cfce8c1cafdf15ae0ac99a
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,75 @@
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
+ def initialize(plugs:, keys:)
10
+ @plugs = plugs
11
+ @keys = keys
12
+ end
7
13
 
8
14
  def [](*keys)
9
- @plugs.values_at(*keys)
15
+ self.class.new(plugs: SubSelector.sub_select(plugs:, keys:), keys:)
16
+ end
17
+
18
+ def +(b)
19
+ @plugs.merge!(b.plugs) { |key, a_values, b_values| [*a_values, *b_values] }
20
+
21
+ self
22
+ end
23
+
24
+ def to_a
25
+ plugs.values.flatten.map(&:result)
26
+ end
27
+
28
+ def to_h
29
+ plugs.values.each_with_object({}) do |values, hash|
30
+ values.each do |plug|
31
+ hash[plug.key] ||= []
32
+ hash[plug.key] << plug.result
33
+ end
34
+ end
10
35
  end
11
36
 
12
37
  def self.included(base)
13
- @plugs = {}
14
38
  base.extend(ClassMethods)
15
39
  end
16
40
 
17
41
  module ClassMethods
18
- def [](*keys)
19
- instance = new
20
- instance.plugs = plugs.values_at(*keys).flatten
21
- instance
22
- end
23
-
24
42
  def plug(key, &block)
25
- plug = Plug.new(key:, eager: true, &block)
43
+ plug = Plug.new(key:, &block)
26
44
 
27
45
  plugs[key] ||= []
28
46
  plugs[key] << plug
47
+ plug_stack << plug
29
48
 
30
49
  plug.result
50
+
51
+ plug_stack.pop
52
+ plug_stack.last.children << plug if plug_stack.last
53
+
54
+ nil
55
+ end
56
+
57
+ def [](*keys)
58
+ new(plugs: SubSelector.sub_select(plugs:, keys:), keys:)
59
+ end
60
+
61
+ def to_a
62
+ plugs.values.flatten.map(&:result)
31
63
  end
32
64
 
33
65
  def plugs
34
66
  @plugs ||= {}
35
67
  @plugs
36
68
  end
69
+
70
+ def plug_stack
71
+ @plug_stack ||= []
72
+ @plug_stack
73
+ end
37
74
  end
38
75
  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.3.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.3.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: []