modular_tree 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6fec58fa42f91715e9a59438b1bf403976618ead4f433d70b39c8458c9396fe5
4
- data.tar.gz: 255a5d2ae6daaf623f5f6adfa7d9b7206a66e3f9613a11f061e9c2336feec943
3
+ metadata.gz: b6913b33ae629fa98c3832ba7ae313e9948c581a53811d49648233f349b85b64
4
+ data.tar.gz: eaaec7d8f9f85924c3064c18b5a88c7805baa0c70e2a7db21e798d217348b67f
5
5
  SHA512:
6
- metadata.gz: 74f3701071758d0fce55d9e103142532e760c861c96b2ca2835894b4c2e298f2a1190e4901db36dfcb5aa8cdfadb73ea976aad38551d58a452c1d1b0d8dc09e0
7
- data.tar.gz: 21785c97fe07687be2c55eb1e3f5c7c9377db855392eeaa9caa490621512ef0ba930202eb63a0b0b8d50a45e6f854217955b2eeb3db4044264bcc978da821319
6
+ metadata.gz: de1721efbd56c4fe8abdfb54f7d044273daf1938612e4316ef6d1344bc9b1e13bddda02da8f8b0f757d75e45f27a20674b365f28c5ecbc19c53c269217a2d7e5
7
+ data.tar.gz: a9c68cf3df5204e568a575b1e510fa8974cd39baddead8a52f83edceee865284729a808e8065c898a1e717f2dc7ddfccc1b7a74056c50ff9e368199780c84e29
@@ -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)
@@ -29,7 +29,7 @@ module Tree
29
29
 
30
30
  def initialize(expr = nil, &block)
31
31
  constrain expr, Proc, Symbol, Class, [Class], true, false, nil
32
- expr.nil? == block_given? or raise ArgumentError
32
+ expr.nil? == block_given? or raise ArgumentError, "Expected either an argument or a block"
33
33
  @expr = (expr.nil? ? block : expr)
34
34
  end
35
35
 
@@ -18,6 +18,8 @@ module Tree
18
18
  def key?(uid) = @pool.key?(uid)
19
19
  def keys = @pool.keys
20
20
  def nodes = @pool.values
21
+ def size = @pool.size
22
+ def empty? = @pool.empty?
21
23
  def [](uid) = @pool[uid]
22
24
  def []=(uid, node) @pool[uid] = node end
23
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tree
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
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.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-15 00:00:00.000000000 Z
11
+ date: 2022-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: constrain