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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc32b4d87ac536560722828526789df4b1bae17d0872c75aafdee114bf22f187
4
- data.tar.gz: 5974d22129202db8a20e92d0b99d567a4afe5ea8f1a4af709fd785105b647a82
3
+ metadata.gz: 8490b76d2456c35bc4b9a891c9126872aa85b75cf082c6fcb3dd0cfde39ad10f
4
+ data.tar.gz: 2a4032b5f7371ae6f6a14fd68129b847914d2ca4600990c911048ffb1044ba94
5
5
  SHA512:
6
- metadata.gz: 2b5365879d03dc973df6de1caf2ca9d33e217ea8649fd2129a953cc288e97c3aa806763fe04a74be01aca33ce1c501ad1a7edfa3e098fbd22bd2cdc1219f24cb
7
- data.tar.gz: 4492c979351ef948321ba535d1d0172852275d89c0c136a6f981279c026a62fc2434895e6c2ba3c994fb5c4c5742a875fcdc86861d49c22465db5096e18b5593
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 Foo
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 Foo.find_all(&:root?).collect(&:to_s_tree)
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}]
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- tree_support (0.1.4)
4
+ tree_support (0.1.5)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,3 +1,3 @@
1
1
  module TreeSupport
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
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.5
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