tree_methods 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tree_methods/version.rb +1 -1
- data/lib/tree_methods.rb +40 -6
- metadata +1 -3
- data/lib/tree_methods/enumerable_tree_methods.rb +0 -32
- data/lib/tree_methods/object_tree_methods.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ba0354c6ba024e1ed9768365b7a17484619ecdb5475f459c6c327a5a3f14dea
|
4
|
+
data.tar.gz: 0bd0d539df290319fc543ea89b11ae4a24dc084858317c39bc84e931c7993133
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d20413db23e7d67403c886972699fab7ccbbb997c91dd227058fe899a06f289db1253c29035ab78eb6fa7df171d6278ecb57aa3a6e150b332b3d84c152c91460
|
7
|
+
data.tar.gz: bb56a86771cd117475864131c1ee73b4d09ea36f848d53d17696bbc3f2ac958bfbae0453f68b8b85266c7947d6d4b55a0be1659d71bf64c5700032d7d13996a7
|
data/lib/tree_methods/version.rb
CHANGED
data/lib/tree_methods.rb
CHANGED
@@ -1,18 +1,52 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "tree_methods/version"
|
4
|
-
require_relative "tree_methods/enumerable_tree_methods"
|
5
|
-
require_relative "tree_methods/object_tree_methods"
|
6
4
|
|
7
5
|
module TreeMethods
|
8
6
|
class Error < StandardError; end
|
9
|
-
end
|
10
7
|
|
11
|
-
|
12
|
-
|
8
|
+
def follow(include_self = false, &block)
|
9
|
+
result = include_self ? [self] : []
|
10
|
+
follow_implementation(result, &block)
|
11
|
+
result
|
12
|
+
end
|
13
|
+
|
14
|
+
def preorder(include_self = true, &block)
|
15
|
+
result = include_self ? [self] : []
|
16
|
+
preorder_implementation(result, &block)
|
17
|
+
result
|
18
|
+
end
|
19
|
+
|
20
|
+
def postorder(include_self = true, &block)
|
21
|
+
result = []
|
22
|
+
postorder_implementation(result, &block)
|
23
|
+
include_self ? result + [self] : result
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def follow_implementation(result, &block)
|
28
|
+
if follower = block.call(self)
|
29
|
+
result << follower
|
30
|
+
follower.send(:follow_implementation, result, &block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def preorder_implementation(result, &block)
|
35
|
+
block.call(self).each { |node|
|
36
|
+
result << node
|
37
|
+
node.send(:preorder_implementation, result, &block)
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def postorder_implementation(result, &block)
|
42
|
+
block.call(self).each { |node|
|
43
|
+
node.send(:postorder_implementation, result, &block)
|
44
|
+
result << node
|
45
|
+
}
|
46
|
+
end
|
13
47
|
end
|
14
48
|
|
15
49
|
class Object
|
16
|
-
include
|
50
|
+
include TreeMethods # FIXME Whoa!
|
17
51
|
end
|
18
52
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tree_methods
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Claus Rasmussen
|
@@ -23,8 +23,6 @@ files:
|
|
23
23
|
- README.md
|
24
24
|
- Rakefile
|
25
25
|
- lib/tree_methods.rb
|
26
|
-
- lib/tree_methods/enumerable_tree_methods.rb
|
27
|
-
- lib/tree_methods/object_tree_methods.rb
|
28
26
|
- lib/tree_methods/version.rb
|
29
27
|
- sig/tree_methods.rbs
|
30
28
|
homepage: http://www.nowhere.com/
|
@@ -1,32 +0,0 @@
|
|
1
|
-
module EnumerableTreeMethods
|
2
|
-
def preorder(include_self = true, &block)
|
3
|
-
result = include_self ? [self] : []
|
4
|
-
preorder_implementation(result, &block)
|
5
|
-
result
|
6
|
-
end
|
7
|
-
|
8
|
-
def postorder(include_self = true, &block)
|
9
|
-
result = []
|
10
|
-
postorder_implementation(result, &block)
|
11
|
-
include_self ? result + [self] : result
|
12
|
-
end
|
13
|
-
|
14
|
-
private
|
15
|
-
def preorder_implementation(result, &block)
|
16
|
-
block.call(self).each { |node|
|
17
|
-
result << node
|
18
|
-
node.send(:preorder_implementation, result, &block)
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
def postorder_implementation(result, &block)
|
23
|
-
block.call(self).each { |node|
|
24
|
-
node.send(:postorder_implementation, result, &block)
|
25
|
-
result << node
|
26
|
-
}
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module ObjectTreeMethods
|
2
|
-
def follow(include_self = false, &block)
|
3
|
-
result = include_self ? [self] : []
|
4
|
-
follow_implementation(result, &block)
|
5
|
-
result
|
6
|
-
end
|
7
|
-
|
8
|
-
private
|
9
|
-
def follow_implementation(result, &block)
|
10
|
-
if follower = block.call(self)
|
11
|
-
result << follower
|
12
|
-
follower.send(:follow_implementation, result, &block)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|