Module: Plugs::SubSelector

Defined in:
lib/sub_selector.rb

Class Method Summary collapse

Class Method Details

.children(parent_plug:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/sub_selector.rb', line 29

def children(parent_plug:)
  children = []

  parent_plug.children.each do |child_plug|
    children << child_plug
    children = [*children, *children(parent_plug: child_plug)]
  end

  children
end

.sub_select(plugs:, keys:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sub_selector.rb', line 11

def sub_select(plugs:, keys:)
  selection = {}

  plugs.slice!(*keys).each_value do |parent_plugs|
    parent_plugs.each do |parent_plug|
      selection[parent_plug.key] ||= []
      selection[parent_plug.key] << parent_plug unless selection[parent_plug.key].include?(parent_plug)

      children(parent_plug:).each do |child_plug|
        selection[child_plug.key] ||= []
        selection[child_plug.key] << child_plug unless selection[child_plug.key].include?(child_plug)
      end
    end
  end

  selection
end