filetree 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e67c14781dc6c85ee01ba2c8168a532ea063d29b
4
+ data.tar.gz: f3a115c9f24ff821d792ead2377ebeb6b6e2ef59
5
+ SHA512:
6
+ metadata.gz: 1206d7e6234e526823b53d1e795afc2960a56932d81a543aad0d6e4bf3d81347b4d94dc37a70f2cd70cd91f4f554d44831f23c2efe6109c13619a160b1d49846
7
+ data.tar.gz: 60b2c4ffbe9e9af4f027c8c5e70eb320baabe8065d932bb5e561aa693f0c8e5967a00856f4f036e2584bf9c78046c4c4fa9c4aefc2a9b7af2d5ca70a10f0fd03
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2013, Eric S. West
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ * The name of Eric S. West may not be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # FileTree
2
+
3
+ A simple tree structure for working with FilePath objects in ruby. I simply took the simple_tree module from https://github.com/ealdent/simple-tree and hacked it into [Pathname](http://www.ruby-doc.org/stdlib-2.0/libdoc/pathname/rdoc/Pathname.html) from the std-lib. This means you get all the awesome features of working with Pathname, as well as making it easy to examine a filepath's ancestors and descendants.
4
+
5
+ # Usage
6
+
7
+ ```ruby
8
+
9
+ require "filetree"
10
+
11
+ tree = FileTree.new('/home/user/test/test1/test2')
12
+ # => #<FileTree:/home/user/test/test1/test2>
13
+
14
+ tree.parent
15
+ # => #<FileTree:/home/user/test/test1>
16
+
17
+ tree.ancestors
18
+ # => [#<FileTree:/home/user/test>,
19
+ # #<FileTree:/home/user>,
20
+ # #<FileTree:/home>,
21
+ # #<FileTree:/>]
22
+
23
+ tree.descendants
24
+ # => [#<FileTree:/home/user/test/test1/test2/testee>,
25
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg>,
26
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4>,
27
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4/myfile.txt>,
28
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3>,
29
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3/no>,
30
+ # #<FileTree:/home/user/test/test1/test2/testee/test4>,
31
+ # #<FileTree:/home/user/test/test1/test2/testee/test4/myfile.txt>,
32
+ # #<FileTree:/home/user/test/test1/test2/testee/test3>,
33
+ # #<FileTree:/home/user/test/test1/test2/testee/test3/no>]
34
+
35
+ des_arr = tree.descendants.map { |e| FileTree.new(e.relative_path_from(FileTree.new('/home/user'))) }
36
+ # => [#<FileTree:test/test1/test2/testee>,
37
+ # #<FileTree:test/test1/test2/testee/testeggg>,
38
+ # #<FileTree:test/test1/test2/testee/testeggg/test4>,
39
+ # #<FileTree:test/test1/test2/testee/testeggg/test4/myfile.txt>,
40
+ # #<FileTree:test/test1/test2/testee/testeggg/test3>,
41
+ # #<FileTree:test/test1/test2/testee/testeggg/test3/no>,
42
+ # #<FileTree:test/test1/test2/testee/test4>,
43
+ # #<FileTree:test/test1/test2/testee/test4/myfile.txt>,
44
+ # #<FileTree:test/test1/test2/testee/test3>,
45
+ # #<FileTree:test/test1/test2/testee/test3/no>]
46
+
47
+ des_arr.first
48
+ # => #<FileTree:test/test1/test2/testee>
49
+
50
+ des_arr.last.ancestors # infinite loop. "ancestors" depends on hitting "/" to stop.
51
+
52
+ # the "tree_rep" method provides prettyprinting for creating your own to_s methods
53
+ puts tree.tree_rep
54
+ # => nil
55
+
56
+ # >> #<FileTree:/home/user/test/test1/test2>
57
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee>
58
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee/testeggg>
59
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4>
60
+ # >> | | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4/myfile.txt>
61
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3>
62
+ # >> | | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3/no>
63
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee/test4>
64
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/test4/myfile.txt>
65
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee/test3>
66
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/test3/no>
67
+
68
+ ```
69
+ # Credits
70
+
71
+ All credit belongs to the following persons, I just cobbled this together from their work:
72
+
73
+ - [@ealdent](https://github.com/ealdent/simple-tree)
74
+ - The authors and maintainers of [Pathname](http://www.ruby-doc.org/stdlib-2.0/libdoc/pathname/rdoc/Pathname.html)
75
+
76
+ # License
77
+
78
+ Distributed under the BSD license, please see [LICENSE](https://github.com/edubkendo/FileTree/blob/master/LICENSE) for more information.
@@ -0,0 +1,58 @@
1
+ require "filetree"
2
+
3
+ tree = FileTree.new('/home/user/test/test1/test2')
4
+ # => #<FileTree:/home/user/test/test1/test2>
5
+
6
+ tree.parent
7
+ # => #<FileTree:/home/user/test/test1>
8
+
9
+ tree.ancestors
10
+ # => [#<FileTree:/home/user/test>,
11
+ # #<FileTree:/home/user>,
12
+ # #<FileTree:/home>,
13
+ # #<FileTree:/>]
14
+
15
+ tree.descendants
16
+ # => [#<FileTree:/home/user/test/test1/test2/testee>,
17
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg>,
18
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4>,
19
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4/myfile.txt>,
20
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3>,
21
+ # #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3/no>,
22
+ # #<FileTree:/home/user/test/test1/test2/testee/test4>,
23
+ # #<FileTree:/home/user/test/test1/test2/testee/test4/myfile.txt>,
24
+ # #<FileTree:/home/user/test/test1/test2/testee/test3>,
25
+ # #<FileTree:/home/user/test/test1/test2/testee/test3/no>]
26
+
27
+ des_arr = tree.descendants.map { |e| FileTree.new(e.relative_path_from(FileTree.new('/home/user'))) }
28
+ # => [#<FileTree:test/test1/test2/testee>,
29
+ # #<FileTree:test/test1/test2/testee/testeggg>,
30
+ # #<FileTree:test/test1/test2/testee/testeggg/test4>,
31
+ # #<FileTree:test/test1/test2/testee/testeggg/test4/myfile.txt>,
32
+ # #<FileTree:test/test1/test2/testee/testeggg/test3>,
33
+ # #<FileTree:test/test1/test2/testee/testeggg/test3/no>,
34
+ # #<FileTree:test/test1/test2/testee/test4>,
35
+ # #<FileTree:test/test1/test2/testee/test4/myfile.txt>,
36
+ # #<FileTree:test/test1/test2/testee/test3>,
37
+ # #<FileTree:test/test1/test2/testee/test3/no>]
38
+
39
+ des_arr.first
40
+ # => #<FileTree:test/test1/test2/testee>
41
+
42
+ des_arr.last.ancestors # infinite loop. "ancestors" depends on hitting "/" to stop.
43
+
44
+ # the "tree_rep" method provides prettyprinting for creating your own to_s methods
45
+ puts tree.tree_rep
46
+ # => nil
47
+
48
+ # >> #<FileTree:/home/user/test/test1/test2>
49
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee>
50
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee/testeggg>
51
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4>
52
+ # >> | | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test4/myfile.txt>
53
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3>
54
+ # >> | | \- #<FileTree:/home/user/test/test1/test2/testee/testeggg/test3/no>
55
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee/test4>
56
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/test4/myfile.txt>
57
+ # >> \- #<FileTree:/home/user/test/test1/test2/testee/test3>
58
+ # >> | \- #<FileTree:/home/user/test/test1/test2/testee/test3/no>
data/filetree.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'filetree'
3
+ s.version = '0.0.1'
4
+ s.date = '2013-05-27'
5
+ s.summary = "A simple ruby library for manipulating filepaths as tree structures"
6
+ s.description = "A simple tree structure for working with FilePath objects in ruby. I simply took the simple_tree module from https://github.com/ealdent/simple-tree and hacked it into [Pathname](http://www.ruby-doc.org/stdlib-2.0/libdoc/pathname/rdoc/Pathname.html) from the std-lib. This means you get all the awesome features of working with Pathname, as well as making it easy to examine a filepath's ancestors and descendants."
7
+ s.authors = ["Eric West"]
8
+ s.email = 'nick@quaran.to'
9
+ s.files = ["lib/filetree.rb",
10
+ "lib/filetree/simple_tree.rb",
11
+ "LICENSE",
12
+ "README.md",
13
+ "examples/simple_filetree.rb",
14
+ "filetree.gemspec"
15
+ ]
16
+ s.licenses = ["BSD"]
17
+ s.homepage =
18
+ 'https://github.com/edubkendo/FileTree'
19
+ end
data/lib/filetree.rb ADDED
@@ -0,0 +1,42 @@
1
+ require "pathname"
2
+ require "filetree/simple_tree"
3
+
4
+ class Pathname
5
+ alias :_parent :parent
6
+ alias :_children :children
7
+ end
8
+
9
+ class FileTree < Pathname
10
+ include SimpleTree
11
+
12
+ attr_accessor :name, :id, :identifier
13
+
14
+ def name
15
+ @name ||= self.inspect
16
+ end
17
+
18
+ def id
19
+ @id ||= self.inspect
20
+ end
21
+
22
+ def identifier
23
+ @identifier ||= self.inspect
24
+ end
25
+
26
+ # See #Pathname.parent
27
+
28
+ def parent
29
+ FileTree.new(_parent)
30
+ end
31
+
32
+ #See #Pathname.children
33
+
34
+ def children(*args)
35
+ if self.directory?
36
+ _children(*args)
37
+ else
38
+ []
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,185 @@
1
+ #
2
+ # Ruby Tree Interface by Jason M. Adams. Distributed under the BSD license:
3
+ # Copyright (c) 2008, Jason M. Adams
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
+ # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
+ # * The name of Jason M. Adams may not be used to endorse or promote products derived from this software without specific prior written permission.
11
+ #
12
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13
+ #
14
+ # To use this interface, drop it in your current class with +include+.
15
+ # You must then simply implement the +parent+ and +children+ methods.
16
+ # * +parent+ returns the parent node of the current node or else nil if it's a root
17
+ # * +children+ returns an +Array+ of all children of this node or an empty +Array+ if it is a leaf node
18
+ #
19
+ module SimpleTree
20
+ def parent() raise "parent must be overridden"; end
21
+ def children() raise "children must be overridden"; end
22
+
23
+
24
+ #
25
+ # Return whether this node is a leaf node in the hierarchy.
26
+ #
27
+ def is_leaf?
28
+ if children.size == 0
29
+ true
30
+ else
31
+ false
32
+ end
33
+ end
34
+
35
+
36
+ #
37
+ # Determine whether this is the root node in the hierarchy.
38
+ #
39
+ def is_root?
40
+ if parent
41
+ false
42
+ else
43
+ true
44
+ end
45
+ end
46
+
47
+
48
+ #
49
+ # Determine whether the node has a parent.
50
+ #
51
+ def has_parent?
52
+ not is_root?
53
+ end
54
+
55
+
56
+ #
57
+ # Determine whether the node has children.
58
+ #
59
+ def has_children?
60
+ not is_leaf?
61
+ end
62
+
63
+
64
+ #
65
+ # Return the height of this subtree. A single node has height 1.
66
+ #
67
+ def height
68
+ heights = children.map {|child| child.height}
69
+ return 1 if heights.size == 0
70
+ return heights.max + 1
71
+ end
72
+
73
+
74
+ #
75
+ # Return an array containing the siblings of the current node.
76
+ #
77
+ def siblings
78
+ # handle case where this is the root node
79
+ return Array.new unless parent
80
+
81
+ sibs = Array.new
82
+ parent.children.each do |child|
83
+ next if child.id == self.id
84
+ sibs << child
85
+ end
86
+
87
+ sibs
88
+ end
89
+
90
+
91
+ #
92
+ # Return every node descending from this node's parent (except this node).
93
+ # This include all of the node's descendants.
94
+ #
95
+ def family
96
+ if parent
97
+ fam = [parent] + parent.descendants
98
+ else
99
+ fam = descendants
100
+ end
101
+
102
+ fam.delete(self)
103
+ fam
104
+ end
105
+
106
+ #
107
+ # Return an array containing every ancestor of the current node.
108
+ #
109
+ def ancestors
110
+ d = Array.new
111
+ current = self.parent()
112
+
113
+ until d.include?(current.parent)
114
+ current = current.parent
115
+ d << current
116
+ end
117
+
118
+ d.flatten
119
+ end
120
+
121
+ #
122
+ # Return an array containing every descendant of the current node.
123
+ #
124
+ def descendants
125
+ d = Array.new
126
+
127
+ children.each do |child|
128
+ d << child
129
+ d << child.descendants
130
+ end
131
+
132
+ d.flatten
133
+ end
134
+
135
+
136
+ #
137
+ # Return the grandparent of this node.
138
+ #
139
+ def grandparent
140
+ parent.parent if parent # returns nil by default if no parent
141
+ end
142
+
143
+
144
+ #
145
+ # Return all the leaf nodes having the current node as an ancestor.
146
+ #
147
+ def leaves
148
+ outp = Array.new
149
+ children.each do |child|
150
+ if child.is_leaf?
151
+ outp << child
152
+ else
153
+ outp << child.leaves
154
+ end
155
+ end
156
+
157
+ outp.flatten
158
+ end
159
+
160
+
161
+ #
162
+ # Helper method for to_s, returns a tree representation of the subtree
163
+ # rooted at this node. This assumes some sort of identifier is specified
164
+ # for the object being called (self.name, self.identifier, etc)
165
+ #
166
+ def tree_rep(depth=0)
167
+ if self.name
168
+ ident = self.name
169
+ elsif self.identifier
170
+ ident = self.identifier
171
+ else
172
+ ident = self.object_id
173
+ end
174
+ if depth > 0
175
+ outp = " #{([" "] * (depth - 1)).join("|")}\\- #{ident}\n"
176
+ else
177
+ outp = "#{ident}\n"
178
+ end
179
+ children.each do |child|
180
+ outp += child.tree_rep(depth + 1)
181
+ end
182
+ outp
183
+ end
184
+
185
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filetree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric West
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple tree structure for working with FilePath objects in ruby. I
14
+ simply took the simple_tree module from https://github.com/ealdent/simple-tree and
15
+ hacked it into [Pathname](http://www.ruby-doc.org/stdlib-2.0/libdoc/pathname/rdoc/Pathname.html)
16
+ from the std-lib. This means you get all the awesome features of working with Pathname,
17
+ as well as making it easy to examine a filepath's ancestors and descendants.
18
+ email: nick@quaran.to
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/filetree.rb
24
+ - lib/filetree/simple_tree.rb
25
+ - LICENSE
26
+ - README.md
27
+ - examples/simple_filetree.rb
28
+ - filetree.gemspec
29
+ homepage: https://github.com/edubkendo/FileTree
30
+ licenses:
31
+ - BSD
32
+ metadata: {}
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 2.0.0.rc.2
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: A simple ruby library for manipulating filepaths as tree structures
53
+ test_files: []