modular_tree 0.0.2 → 0.0.3

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: ab7be4bf786460addacf0cabad30d5b8c0f5a0aa00900e894f933fd6f53230be
4
- data.tar.gz: 6b9c53ba9e2edccaa298bf1696e466dccfec7262c0cb5190bd82e1cf3055d60f
3
+ metadata.gz: 1b145f3b7daeb0bddc2a2415e3a98d6d6232d95afeca8147908bffa2089ff412
4
+ data.tar.gz: 68271887a655b0196e5f9819f72ee80ca9e853c15568ab48cc1d7e15c4a29d22
5
5
  SHA512:
6
- metadata.gz: 471dc9d79129be7692a3442a72ad03da18203461a7741a3815d2cb71a69c93ae68c5af787769cee2c6af14a8d787b5a1867551d681b8a88826be05227c47b2d1
7
- data.tar.gz: 1b676cc1284e7bd48611da0cf99912262ca7f9eb4cfd8edd801e4f1ecbd5ec73941bd8cfebd069242024f71993302aa1766ef44cc38278fd0659d5791c2bec8b
6
+ metadata.gz: b9a39254ef102c23dbba9445b5b95b3d925d0954ef28de91107318c24ce9c450dd1fc2612cae18fc2c4033314bcd85b14a7126eb09e01daee076e380b05fe54c
7
+ data.tar.gz: 3b84c591c7816199835041074b6fb8b9506c5b85b438000045ce4d21627559835edeadf0336ca6635d76cef563082254318a1cf13d15342c431f407f51868615
@@ -167,6 +167,33 @@ module Tree
167
167
 
168
168
  def each_child(&block) = @children.map(&block)
169
169
  def attach(child) = @children << child
170
+
171
+ # Can be used with any array implementation
172
+ def insert(where, child) = insert_append(:insert, where, child)
173
+ def append(where, child) = insert_append(:append, where, child)
174
+
175
+ def replace(where, *children)
176
+ children = Array(children).flatten
177
+ case where
178
+ when Integer
179
+ subject = @children[where] or raise ArgumentError
180
+ index = where
181
+ else
182
+ subject = where
183
+ index = @children.index(where) or raise ArgumentError
184
+ end
185
+ @children = @children[0...index] + children + @children[index + 1..-1]
186
+ subject
187
+ end
188
+
189
+ protected
190
+ def insert_append(which, where, child)
191
+ if !where.is_a?(Integer)
192
+ where = @children.index(where) or raise ArgumentError, "Can't find object"
193
+ end
194
+ where += 1 if which == :append
195
+ @children.insert(where, child)
196
+ end
170
197
  end
171
198
 
172
199
  module InternalChildrenHashImplementation
@@ -198,5 +225,28 @@ module Tree
198
225
  child.send(:parent=, self)
199
226
  end
200
227
  end
228
+
229
+ module InternalParentChildArrayImplementation
230
+ include InternalParentChildImplementation
231
+
232
+ def insert(where, child)
233
+ super
234
+ child.send(:parent=, self)
235
+ end
236
+
237
+ def append(where, child)
238
+ super
239
+ child.send(:parent=, self)
240
+ end
241
+
242
+ # Requires that Child classes already has defined this
243
+ def replace(where, *children)
244
+ children = Array(children).flatten
245
+ subject = super(where, children)
246
+ subject.send(:parent=, nil)
247
+ children.each { |child| child.send(:parent=, self) }
248
+ subject
249
+ end
250
+ end
201
251
  end
202
252
 
@@ -36,6 +36,10 @@ module Tree
36
36
  def attach(child) = abstract_method
37
37
  end
38
38
 
39
+ # TODO
40
+ module ParentChildProperty
41
+ end
42
+
39
43
  module KeyProperty # Set
40
44
  def key = abstract_method
41
45
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tree
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
data/lib/modular_tree.rb CHANGED
@@ -39,7 +39,7 @@ module Tree
39
39
  class ArrayTree < AbstractTree # Aka. SetTree aka. Tree
40
40
  include InternalParentImplementation
41
41
  include InternalChildrenArrayImplementation
42
- include InternalParentChildImplementation
42
+ include InternalParentChildArrayImplementation
43
43
  include UpTreeAlgorithms
44
44
  include DownTreeAlgorithms
45
45
  include DownTreeFilteredAlgorithms
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.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen