modular_tree 0.4.0 → 0.5.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: 6fec58fa42f91715e9a59438b1bf403976618ead4f433d70b39c8458c9396fe5
4
- data.tar.gz: 255a5d2ae6daaf623f5f6adfa7d9b7206a66e3f9613a11f061e9c2336feec943
3
+ metadata.gz: '0900ab1e0482487fcb6b63c28fccb0d7dc7f71e5ce54230f3ee551ff67f0dfb5'
4
+ data.tar.gz: bf6b30a19df1fa8211b355bf06392dbefad2950a2a52bd494dad99605f392669
5
5
  SHA512:
6
- metadata.gz: 74f3701071758d0fce55d9e103142532e760c861c96b2ca2835894b4c2e298f2a1190e4901db36dfcb5aa8cdfadb73ea976aad38551d58a452c1d1b0d8dc09e0
7
- data.tar.gz: 21785c97fe07687be2c55eb1e3f5c7c9377db855392eeaa9caa490621512ef0ba930202eb63a0b0b8d50a45e6f854217955b2eeb3db4044264bcc978da821319
6
+ metadata.gz: 52d2ea9f3c82c6e7578f50b51804c223d267b1757f0709776addf39752e1034a57b14354f6877236d12017f2368300c5f3064537a3592f2fcf72bd69e2f991de
7
+ data.tar.gz: ad298843f7e45b03654ef53b96c507cf51ee0219c01abc4f30619a9396b17c9e775b64fb10b4fa9d3a9bbce0232f7235e2de2e283632a1cba38690634b9c909b
@@ -77,6 +77,21 @@ module Tree
77
77
  end
78
78
  end
79
79
 
80
+ # Filter children. Doesn't recurse. If a block is given, it should return
81
+ # truish to select a child node
82
+ #
83
+ # TODO: Maybe make #children an Array extended with filtering
84
+ def choose(match_expr = nil, &block)
85
+ matcher = Matcher.new(match_expr, &block)
86
+ if block_given?
87
+ a = []
88
+ each_branch { |branch, key| a << branch if matcher.match? branch }
89
+ a
90
+ else
91
+ Enumerator.new { |enum| each_branch { |branch, key| enum << branch if matcher.match? branch } }
92
+ end
93
+ end
94
+
80
95
  # Like #each but the block is called with node, key, and parent instead of
81
96
  # value, key, and parent
82
97
  def nodes(*filter, this: true, &block) = common_each(*filter, :node, :do_each_preorder, this, &block)
@@ -101,9 +116,9 @@ module Tree
101
116
 
102
117
  # Return array of [previous-matching-node, matching-node] tuples. Returns
103
118
  # the empty array ff there is no matching set of nodes
104
- def pairs(first_matcher_expr, second_matcher_expr, this: true)
105
- first_matcher = Matcher.new(first_matcher_expr)
106
- second_matcher = Matcher.new(second_matcher_expr)
119
+ def pairs(first_match_expr, second_match_expr, this: true)
120
+ first_matcher = Matcher.new(first_match_expr)
121
+ second_matcher = Matcher.new(second_match_expr)
107
122
  or_matcher = first_matcher | second_matcher # avoids re-computing this value over and over
108
123
  result = []
109
124
  nodes(first_matcher, false, this: this) { |node|
@@ -22,7 +22,7 @@ module Tree
22
22
  #
23
23
  # when +traverse+ is
24
24
  # true Traverse always. This is the default
25
- # false Traverse only if select didn't match
25
+ # false Traverse only if select didn't match # TODO: Never traverse to be able to use filters on children
26
26
  # nil Expects +select+ to be a Proc object that returns a [select,
27
27
  # traverse] tuple of booleans
28
28
  #
@@ -33,7 +33,7 @@ module Tree
33
33
  # It is not an error if the method doesn't exists on the a given node but
34
34
  # the node is not selected/traversed. If the expression is a class or an
35
35
  # array of classes, a given node matches if it is an instance of one of the
36
- # classes or any subclass
36
+ # classes and their subclasses
37
37
  #
38
38
  # If a block is given, it is supposed to return a [select, traverse] tuple
39
39
  # of booleans
@@ -42,6 +42,7 @@ module Tree
42
42
  # enumerators that doesn't execute the filter unless the enumerator is
43
43
  # evaluated
44
44
  #
45
+ # TODO: Accept RegExp -> use to_s to match
45
46
  def initialize(select_expr = nil, traverse_expr = nil, &block)
46
47
  if select_expr.nil? && block_given?
47
48
  @matcher = block
@@ -164,6 +164,7 @@ module Tree
164
164
  end
165
165
 
166
166
  def each_child(&block) = @children.map(&block)
167
+
167
168
  def attach(child) = @children << child
168
169
  def detach(arg)
169
170
  key = arg.is_a?(Integer) ? arg : @children.index(arg)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tree
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modular_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen