tree_support 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.org +37 -2
- data/examples/0231_records_to_tree.rb +30 -0
- data/examples/Gemfile.lock +1 -1
- data/lib/tree_support/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8490b76d2456c35bc4b9a891c9126872aa85b75cf082c6fcb3dd0cfde39ad10f
|
4
|
+
data.tar.gz: 2a4032b5f7371ae6f6a14fd68129b847914d2ca4600990c911048ffb1044ba94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f4823684332133d005d6b214e2a523565b275c7738058e9e9d4c3eb3f5494b97982137da3440e7c8282e2967c3e48ad93728708e779c980e5557f34fdd0c111
|
7
|
+
data.tar.gz: 9f07e3b6fd40238fa35d44c20ec365c85fa0776fa4ec468c00171f6a1a58e99ffe6f3a9ce888220bd0e83e41e6ff00aca128979d7daafc57c47fe809ecdb8b73
|
data/README.org
CHANGED
@@ -403,6 +403,41 @@ puts TreeSupport.tree(root, take: 3, drop: 1)
|
|
403
403
|
# >> └─Recover
|
404
404
|
#+END_SRC
|
405
405
|
|
406
|
+
*** Methods for easily making trees from data like CSV
|
407
|
+
|
408
|
+
Array -> Tree
|
409
|
+
|
410
|
+
#+BEGIN_SRC ruby
|
411
|
+
require "tree_support"
|
412
|
+
|
413
|
+
records = [
|
414
|
+
{key: :a, parent: nil},
|
415
|
+
{key: :b, parent: :a},
|
416
|
+
{key: :c, parent: :b},
|
417
|
+
]
|
418
|
+
|
419
|
+
# When the first node is regarded as a parent
|
420
|
+
puts TreeSupport.records_to_tree(records).first.to_s_tree
|
421
|
+
# >> a
|
422
|
+
# >> └─b
|
423
|
+
# >> └─c
|
424
|
+
|
425
|
+
# When you make a parent parenting the whole
|
426
|
+
puts TreeSupport.records_to_tree(records, root_key: :root).to_s_tree
|
427
|
+
# >> root
|
428
|
+
# >> └─a
|
429
|
+
# >> └─b
|
430
|
+
# >> └─c
|
431
|
+
|
432
|
+
# Convert from tree to array
|
433
|
+
tree = TreeSupport.records_to_tree(records)
|
434
|
+
pp TreeSupport.tree_to_records(tree.first)
|
435
|
+
# >> [
|
436
|
+
# >> {:key=>:a, :parent=>nil},
|
437
|
+
# >> {:key=>:b, :parent=>:a},
|
438
|
+
# >> {:key=>:c, :parent=>:b},
|
439
|
+
# >> ]
|
440
|
+
|
406
441
|
*** Image version also has similar options
|
407
442
|
|
408
443
|
#+BEGIN_SRC ruby
|
@@ -456,7 +491,7 @@ end
|
|
456
491
|
Just as with ordinary classes, we need parent and children methods
|
457
492
|
|
458
493
|
#+BEGIN_SRC ruby
|
459
|
-
class
|
494
|
+
class TreeModel
|
460
495
|
include MemoryRecord
|
461
496
|
memory_record [
|
462
497
|
{key: :a, parent: nil},
|
@@ -476,7 +511,7 @@ class Foo
|
|
476
511
|
end
|
477
512
|
end
|
478
513
|
|
479
|
-
puts
|
514
|
+
puts TreeModel.find_all(&:root?).collect(&:to_s_tree)
|
480
515
|
# >> A
|
481
516
|
# >> └─B
|
482
517
|
# >> └─C
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
require "tree_support"
|
3
|
+
|
4
|
+
# Methods for easily making trees from data like CSV
|
5
|
+
# Array -> Tree
|
6
|
+
|
7
|
+
records = [
|
8
|
+
{key: :a, parent: nil},
|
9
|
+
{key: :b, parent: :a},
|
10
|
+
{key: :c, parent: :b},
|
11
|
+
]
|
12
|
+
|
13
|
+
# When the first node is regarded as a parent
|
14
|
+
puts TreeSupport.records_to_tree(records).first.to_s_tree
|
15
|
+
|
16
|
+
# When you make a parent parenting the whole
|
17
|
+
puts TreeSupport.records_to_tree(records, root_key: :root).to_s_tree
|
18
|
+
|
19
|
+
# Convert from tree to array
|
20
|
+
tree = TreeSupport.records_to_tree(records)
|
21
|
+
pp TreeSupport.tree_to_records(tree.first)
|
22
|
+
|
23
|
+
# >> a
|
24
|
+
# >> └─b
|
25
|
+
# >> └─c
|
26
|
+
# >> root
|
27
|
+
# >> └─a
|
28
|
+
# >> └─b
|
29
|
+
# >> └─c
|
30
|
+
# >> [{:key=>:a, :parent=>nil}, {:key=>:b, :parent=>:a}, {:key=>:c, :parent=>:b}]
|
data/examples/Gemfile.lock
CHANGED
data/lib/tree_support/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tree_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- akicho8
|
@@ -135,6 +135,7 @@ files:
|
|
135
135
|
- examples/0210_take_drop.rb
|
136
136
|
- examples/0220_it_will_not_be_strange_to_tojson.rb
|
137
137
|
- examples/0230_list_to_tree.rb
|
138
|
+
- examples/0231_records_to_tree.rb
|
138
139
|
- examples/0240_memory_record.rb
|
139
140
|
- examples/Gemfile
|
140
141
|
- examples/Gemfile.lock
|