arbolobra 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +66 -20
- data/arbolobra.gemspec +2 -2
- data/lib/arbolobra/chars.rb +2 -3
- data/lib/arbolobra/formatter.rb +28 -0
- data/lib/arbolobra/node.rb +28 -29
- data/lib/arbolobra/tree.rb +8 -10
- data/lib/arbolobra/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18869c79acefcad19ab23b03fb60631b791a6c0c
|
4
|
+
data.tar.gz: 8300a503605354e94d6ff232b869bc37553caa0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 806746df05318376da007008cb30c3afc60a7354a302f1a7cf7e5d86a28aea6e515b21f49cc0a8addf5742120dcaeb13eca95358824510439b5605a772269709
|
7
|
+
data.tar.gz: cfebe7bf871c8e0e95b664beee6247125f4d52bcc4444e9d6dc0faa75778f92fb581936bf07a043797e1b7f88b101f5619e2070de2b3e792aa673815e72a0d01
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Arbolobra
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
that code, run `bin/console` for an interactive prompt.
|
3
|
+
Arbolobra is for reading and writing a hierarchical set of data, i.e., a tree. ("Arbol" is Spanish
|
4
|
+
for "tree", so the palindrome "arbolobra" means a tree, backward and forward.)
|
6
5
|
|
7
|
-
|
6
|
+
Arbolobra makes it simple to take a flat set of data (such as a list of files of the form
|
7
|
+
/path/to/filename) and write that as a tree, and the inverse.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -26,9 +26,14 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
Arbolobra is for reading and writing a hierarchical set of data, i.e., a tree.
|
28
28
|
|
29
|
-
|
29
|
+
### Generating from Data
|
30
|
+
|
31
|
+
For example, a simple viewer (a rough equivalent of "find") of files and directories, as a tree:
|
30
32
|
|
31
33
|
```ruby
|
34
|
+
require 'arbolobra/node'
|
35
|
+
require 'pathname'
|
36
|
+
|
32
37
|
def build pn
|
33
38
|
children = pn.directory? ? pn.children.sort : Array.new
|
34
39
|
Arbolobra::Node.new pn.basename, children.collect { |child| build child }
|
@@ -39,49 +44,90 @@ args = ARGV.empty? ? %w{ . } : ARGV
|
|
39
44
|
args.each do |arg|
|
40
45
|
pn = Pathname.new arg
|
41
46
|
node = build pn
|
42
|
-
|
43
47
|
node.print
|
44
48
|
end
|
45
49
|
```
|
46
50
|
|
47
|
-
|
51
|
+
The output of running the above in the Arbolobra project for the arguments [ "lib", "test" ]:
|
52
|
+
|
53
|
+
```text
|
54
|
+
lib
|
55
|
+
+---arbolobra
|
56
|
+
| +---chars.rb
|
57
|
+
| +---node.rb
|
58
|
+
| +---tree.rb
|
59
|
+
| \---version.rb
|
60
|
+
\---arbolobra.rb
|
61
|
+
test
|
62
|
+
+---arbolobra
|
63
|
+
| +---chars_test.rb
|
64
|
+
| +---node_test.rb
|
65
|
+
| \---tree_test.rb
|
66
|
+
+---arbolobra_test.rb
|
67
|
+
\---test_helper.rb
|
68
|
+
```
|
69
|
+
|
70
|
+
### Generating from Strings (Lines)
|
71
|
+
|
72
|
+
To convert strings, such as from a list of files, from flat to hierarchical:
|
48
73
|
|
49
74
|
```ruby
|
75
|
+
require 'arbolobra/tree
|
76
|
+
|
50
77
|
tree = Arbolobra::Tree.new $stdin.readlines, "/"
|
51
78
|
tree.root.print
|
52
79
|
```
|
53
80
|
|
81
|
+
In raw form:
|
82
|
+
|
54
83
|
```shell
|
55
|
-
% git diff-index --name-only --no-commit-id
|
84
|
+
% git diff-index --name-only --no-commit-id c2ef
|
56
85
|
```
|
57
86
|
|
87
|
+
The output:
|
88
|
+
|
58
89
|
```text
|
90
|
+
README.md
|
91
|
+
arbolobra.gemspec
|
92
|
+
lib/arbolobra/chars.rb
|
93
|
+
lib/arbolobra/formatter.rb
|
94
|
+
lib/arbolobra/node.rb
|
95
|
+
lib/arbolobra/tree.rb
|
96
|
+
test/arbolobra/chars_test.rb
|
97
|
+
test/arbolobra/formatter_test.rb
|
98
|
+
test/arbolobra/node_test.rb
|
99
|
+
test/arbolobra/tc.rb
|
100
|
+
test/arbolobra/tree_test.rb
|
101
|
+
```
|
102
|
+
|
103
|
+
Using the above script, converting Git output to a tree:
|
104
|
+
|
105
|
+
```shell
|
106
|
+
% git diff-index --name-only --no-commit-id c2ef |
|
107
|
+
ruby -I lib -rarbolobra/tree -e 'tree = Arbolobra::Tree.new $stdin.readlines, "/"; tree.root.print'
|
108
|
+
```
|
59
109
|
|
60
|
-
|
110
|
+
Output:
|
111
|
+
|
112
|
+
```text
|
113
|
+
.
|
61
114
|
+---README.md
|
115
|
+
+---arbolobra.gemspec
|
62
116
|
+---lib
|
63
117
|
| \---arbolobra
|
64
118
|
| +---chars.rb
|
119
|
+
| +---formatter.rb
|
65
120
|
| +---node.rb
|
66
121
|
| \---tree.rb
|
67
122
|
\---test
|
68
123
|
\---arbolobra
|
69
124
|
+---chars_test.rb
|
125
|
+
+---formatter_test.rb
|
70
126
|
+---node_test.rb
|
127
|
+
+---tc.rb
|
71
128
|
\---tree_test.rb
|
72
129
|
```
|
73
130
|
|
74
|
-
## Development
|
75
|
-
|
76
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run
|
77
|
-
the tests. You can also run `bin/console` for an interactive prompt that will allow you to
|
78
|
-
experiment.
|
79
|
-
|
80
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new
|
81
|
-
version, update the version number in `version.rb`, and then run `bundle exec rake release`, which
|
82
|
-
will create a git tag for the version, push git commits and tags, and push the `.gem` file to
|
83
|
-
[rubygems.org](https://rubygems.org).
|
84
|
-
|
85
131
|
## Contributing
|
86
132
|
|
87
133
|
Bug reports and pull requests are welcome on GitHub at https://github.com/jpace/arbolobra.
|
data/arbolobra.gemspec
CHANGED
@@ -30,9 +30,9 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
|
-
spec.add_development_dependency "bundler", "~>
|
33
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
34
34
|
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
-
spec.add_development_dependency "test-unit", "~> 3.1"
|
35
|
+
spec.add_development_dependency "test-unit", "~> 3.1.5"
|
36
36
|
spec.add_development_dependency "paramesan", "~> 0.1.1"
|
37
37
|
spec.add_development_dependency "logue", "~> 1.0"
|
38
38
|
end
|
data/lib/arbolobra/chars.rb
CHANGED
@@ -25,7 +25,6 @@ end
|
|
25
25
|
# x +---arbolobra_test.rb
|
26
26
|
# x \---test_helper.rb
|
27
27
|
|
28
|
-
|
29
28
|
# this is the "intro" (i) and the "lead" (l):
|
30
29
|
# <iiiiii><ll>
|
31
30
|
# x | +---node_test.rb
|
@@ -60,7 +59,7 @@ class Arbolobra::CharList
|
|
60
59
|
end
|
61
60
|
|
62
61
|
def expand is_last
|
63
|
-
leftchr
|
62
|
+
leftchr = @chars[is_last ? 0 : 1]
|
64
63
|
repeatchr = @chars[2]
|
65
64
|
leftchr + repeatchr * (@width - 1)
|
66
65
|
end
|
@@ -75,7 +74,7 @@ class Arbolobra::CharSet
|
|
75
74
|
|
76
75
|
def initialize intro, lead
|
77
76
|
@intro = intro
|
78
|
-
@lead
|
77
|
+
@lead = lead
|
79
78
|
end
|
80
79
|
|
81
80
|
DEFAULT = new Arbolobra::CharList::DEFAULT_INTRO, Arbolobra::CharList::DEFAULT_LEAD
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# -*- ruby -*-
|
3
|
+
|
4
|
+
require 'arbolobra/chars'
|
5
|
+
|
6
|
+
module Arbolobra
|
7
|
+
end
|
8
|
+
|
9
|
+
class Arbolobra::Formatter
|
10
|
+
def initialize charset: Arbolobra::CharSet::DEFAULT, output: $stdout
|
11
|
+
@charset = charset
|
12
|
+
@output = output
|
13
|
+
end
|
14
|
+
|
15
|
+
def print node, intro = "", lead = ""
|
16
|
+
@output.print lead, node.value.to_s, "\n"
|
17
|
+
children = node.children
|
18
|
+
children.each_with_index do |child, idx|
|
19
|
+
print_child child, intro, idx == children.size - 1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def print_child child, intro, is_last
|
24
|
+
nextintro = intro + @charset.intro.expand(is_last)
|
25
|
+
nextlead = intro + @charset.lead.expand(is_last)
|
26
|
+
print child, nextintro, nextlead
|
27
|
+
end
|
28
|
+
end
|
data/lib/arbolobra/node.rb
CHANGED
@@ -2,40 +2,39 @@
|
|
2
2
|
# -*- ruby -*-
|
3
3
|
|
4
4
|
require 'arbolobra/chars'
|
5
|
+
require 'arbolobra/formatter'
|
5
6
|
|
6
7
|
module Arbolobra
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
def print intro: "", lead: "", output: $stdout, chars: Arbolobra::CharSet::DEFAULT
|
21
|
-
output.print lead, @value.to_s, "\n"
|
22
|
-
childargs = { intro: intro, output: output, chars: chars }
|
23
|
-
@children.each_with_index do |child, idx|
|
24
|
-
print_child child, childargs.merge({ is_last: idx == @children.size - 1 })
|
8
|
+
class Node
|
9
|
+
include Comparable
|
10
|
+
|
11
|
+
attr_reader :value
|
12
|
+
attr_reader :children
|
13
|
+
|
14
|
+
def initialize value, *children
|
15
|
+
@children = if children.size == 1 && children.first.class == Array
|
16
|
+
children.first
|
17
|
+
else
|
18
|
+
children
|
19
|
+
end
|
20
|
+
@value = value
|
25
21
|
end
|
26
|
-
end
|
27
22
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
23
|
+
def print intro: "", lead: "", output: $stdout, charset: Arbolobra::CharSet::DEFAULT
|
24
|
+
fmt = Formatter.new charset: charset, output: output
|
25
|
+
fmt.print self, intro, lead
|
26
|
+
end
|
33
27
|
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
def to_s
|
29
|
+
"value: #{@value}, #{@children && @children.size}"
|
30
|
+
end
|
37
31
|
|
38
|
-
|
39
|
-
|
32
|
+
def <=> other
|
33
|
+
cmp = value <=> other.value
|
34
|
+
if cmp == 0
|
35
|
+
cmp = children <=> other.children
|
36
|
+
end
|
37
|
+
cmp
|
38
|
+
end
|
40
39
|
end
|
41
40
|
end
|
data/lib/arbolobra/tree.rb
CHANGED
@@ -3,20 +3,18 @@
|
|
3
3
|
|
4
4
|
require 'arbolobra/node'
|
5
5
|
|
6
|
-
module Arbolobra
|
7
|
-
end
|
8
|
-
|
9
6
|
class Arbolobra::Tree
|
10
7
|
attr_reader :root
|
11
8
|
attr_reader :nodes
|
12
9
|
|
13
|
-
def initialize lines, separator
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
10
|
+
def initialize lines, separator = "/"
|
11
|
+
if lines
|
12
|
+
@root = Arbolobra::Node.new "."
|
13
|
+
|
14
|
+
lines.each do |line|
|
15
|
+
elements = line.chomp.split separator
|
16
|
+
create_nodes elements
|
17
|
+
end
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
data/lib/arbolobra/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arbolobra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Pace
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '2.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '2.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.1.5
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.1.5
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: paramesan
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- bin/setup
|
100
100
|
- lib/arbolobra.rb
|
101
101
|
- lib/arbolobra/chars.rb
|
102
|
+
- lib/arbolobra/formatter.rb
|
102
103
|
- lib/arbolobra/node.rb
|
103
104
|
- lib/arbolobra/tree.rb
|
104
105
|
- lib/arbolobra/version.rb
|