modular_tree 0.2.0 → 0.4.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: 420d72c774f0602ebf2b50b7346ee64759aaf48f89b8e041c80fe76464dbd379
4
- data.tar.gz: 4ceb72733d99138a6244c4b163b46d0fb10fcdf20b8f7d61e582903bab750bc8
3
+ metadata.gz: 6fec58fa42f91715e9a59438b1bf403976618ead4f433d70b39c8458c9396fe5
4
+ data.tar.gz: 255a5d2ae6daaf623f5f6adfa7d9b7206a66e3f9613a11f061e9c2336feec943
5
5
  SHA512:
6
- metadata.gz: a5b2b90bcaaa71fd2754093cbd3ccca1fea3c9193c3261a82911bd10c7c8c6e74476804006238215aaf448062424dbfeef606550b69f378fd742d159d58b05b2
7
- data.tar.gz: 8330f74bf4368cc7d56f35b42e48ff4f1dd51641ed94f05f4340f222eef22d62d0a3b32c883e2e6d916b78762a3d23870e62131b3e2f461c10ccabf16070d6ce
6
+ metadata.gz: 74f3701071758d0fce55d9e103142532e760c861c96b2ca2835894b4c2e298f2a1190e4901db36dfcb5aa8cdfadb73ea976aad38551d58a452c1d1b0d8dc09e0
7
+ data.tar.gz: 21785c97fe07687be2c55eb1e3f5c7c9377db855392eeaa9caa490621512ef0ba930202eb63a0b0b8d50a45e6f854217955b2eeb3db4044264bcc978da821319
@@ -89,9 +89,8 @@ module Tree
89
89
 
90
90
  # Enumerator of edges in the tree. Edges are [previous-matching-node,
91
91
  # matching-node] tuples. Top-level nodes have previous-matching-node set to
92
- # nil
93
- #
94
- # FIXME: Not working right now. Not sure how #edges relates to #pairs
92
+ # nil. If the filter matches all nodes the value is an edge-representation
93
+ # of the tree
95
94
  def edges(*filter, this: true, &block)
96
95
  if block_given?
97
96
  each(*filter, this: this) { |node, _, parent| yield parent, node }
@@ -100,6 +99,19 @@ module Tree
100
99
  end
101
100
  end
102
101
 
102
+ # Return array of [previous-matching-node, matching-node] tuples. Returns
103
+ # 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)
107
+ or_matcher = first_matcher | second_matcher # avoids re-computing this value over and over
108
+ result = []
109
+ nodes(first_matcher, false, this: this) { |node|
110
+ node.do_pairs(result, first_matcher, or_matcher)
111
+ }
112
+ result
113
+ end
114
+
103
115
  # Find nodes matching +filter+ and call +traverse_block+ with a node and a
104
116
  # block argument. +traverse_block+ can recurse into children by calling the
105
117
  # supplied inner block
@@ -117,18 +129,6 @@ module Tree
117
129
  nodes(filter, this: this).map { |node| traverse_block.call(node, inner_block) }
118
130
  end
119
131
 
120
- # Return array of [previous-matching-node, matching-node] tuples
121
- def pairs(first_matcher_expr, second_matcher_expr, this: true)
122
- first_matcher = Matcher.new(first_matcher_expr)
123
- second_matcher = Matcher.new(second_matcher_expr)
124
- or_matcher = first_matcher | second_matcher # avoids re-computing this value over and over
125
- result = []
126
- nodes(first_matcher, false, this: this) { |node|
127
- node.do_pairs(result, first_matcher, or_matcher)
128
- }
129
- result
130
- end
131
-
132
132
  # Traverse the tree top-down while accumulating information in an
133
133
  # accumulator object. The block takes a [accumulator, node] tuple and is
134
134
  # responsible for adding itself to the accumulator. The return value from
@@ -15,8 +15,6 @@
15
15
  #
16
16
  # Internal trees adds a parent/children relation
17
17
  #
18
- #
19
- #
20
18
  # Only internal trees have parent/child relations. External trees have only branch/branches relations
21
19
 
22
20
  module Tree
@@ -167,6 +165,12 @@ module Tree
167
165
 
168
166
  def each_child(&block) = @children.map(&block)
169
167
  def attach(child) = @children << child
168
+ def detach(arg)
169
+ key = arg.is_a?(Integer) ? arg : @children.index(arg)
170
+ child = @children.delete_at(key) or raise ArgumentError, "Can't find object"
171
+ child.send(:instance_variable_set, :@parent, nil)
172
+ child
173
+ end
170
174
 
171
175
  # Can be used with any array implementation. +where+ is either an Integer
172
176
  # index or an object
@@ -35,6 +35,12 @@ module Tree
35
35
  def children = abstract_method
36
36
  def each_child(&block) = children.each(&block)
37
37
  def attach(child) = abstract_method
38
+
39
+ # :call-seq:
40
+ # detach(key)
41
+ # detach(child)
42
+ #
43
+ def detach(arg) = abstract_method
38
44
  end
39
45
 
40
46
  # TODO
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tree
4
- VERSION = "0.2.0"
4
+ VERSION = "0.4.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.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen