dirtravel 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,190 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html>
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+
7
+ <link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" charset="utf-8" />
8
+
9
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
10
+
11
+
12
+
13
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
14
+
15
+ <script type="text/javascript" charset="utf-8" src="js/full_list.js"></script>
16
+
17
+
18
+ <base id="base_target" target="_parent" />
19
+ </head>
20
+ <body>
21
+ <script type="text/javascript" charset="utf-8">
22
+ if (window.top.frames.main) {
23
+ document.getElementById('base_target').target = 'main';
24
+ document.body.className = 'frames';
25
+ }
26
+ </script>
27
+ <div id="content">
28
+ <h1 id="full_list_header">Method List</h1>
29
+ <div id="nav">
30
+
31
+ <span><a target="_self" href="class_list.html">
32
+ Classes
33
+ </a></span>
34
+
35
+ <span><a target="_self" href="method_list.html">
36
+ Methods
37
+ </a></span>
38
+
39
+ <span><a target="_self" href="file_list.html">
40
+ Files
41
+ </a></span>
42
+
43
+ </div>
44
+ <div id="search">Search: <input type="text" /></div>
45
+
46
+ <ul id="full_list" class="method">
47
+
48
+
49
+ <li class="r1 ">
50
+ <span class='object_link'><a href="DirTravel/Entry.html#abspath-instance_method" title="DirTravel::Entry#abspath (method)">#abspath</a></span>
51
+ <small>DirTravel::Entry</small>
52
+ </li>
53
+
54
+
55
+ <li class="r2 ">
56
+ <span class='object_link'><a href="DirTravel/Travel.html#basedir-instance_method" title="DirTravel::Travel#basedir (method)">#basedir</a></span>
57
+ <small>DirTravel::Travel</small>
58
+ </li>
59
+
60
+
61
+ <li class="r1 ">
62
+ <span class='object_link'><a href="DirTravel/FileEntry.html#basename-instance_method" title="DirTravel::FileEntry#basename (method)">#basename</a></span>
63
+ <small>DirTravel::FileEntry</small>
64
+ </li>
65
+
66
+
67
+ <li class="r2 ">
68
+ <span class='object_link'><a href="DirTravel/Travel.html#defaults-instance_method" title="DirTravel::Travel#defaults (method)">#defaults</a></span>
69
+ <small>DirTravel::Travel</small>
70
+ </li>
71
+
72
+
73
+ <li class="r1 ">
74
+ <span class='object_link'><a href="DirTravel/Entry.html#dir-instance_method" title="DirTravel::Entry#dir (method)">#dir</a></span>
75
+ <small>DirTravel::Entry</small>
76
+ </li>
77
+
78
+
79
+ <li class="r2 ">
80
+ <span class='object_link'><a href="DirTravel/Entry.html#files-instance_method" title="DirTravel::Entry#files (method)">#files</a></span>
81
+ <small>DirTravel::Entry</small>
82
+ </li>
83
+
84
+
85
+ <li class="r1 ">
86
+ <span class='object_link'><a href="DirTravel/Travel.html#filetree-class_method" title="DirTravel::Travel.filetree (method)">filetree</a></span>
87
+ <small>DirTravel::Travel</small>
88
+ </li>
89
+
90
+
91
+ <li class="r2 ">
92
+ <span class='object_link'><a href="DirTravel/FileEntry.html#initialize-instance_method" title="DirTravel::FileEntry#initialize (method)">#initialize</a></span>
93
+ <small>DirTravel::FileEntry</small>
94
+ </li>
95
+
96
+
97
+ <li class="r1 ">
98
+ <span class='object_link'><a href="DirTravel/Entry.html#initialize-instance_method" title="DirTravel::Entry#initialize (method)">#initialize</a></span>
99
+ <small>DirTravel::Entry</small>
100
+ </li>
101
+
102
+
103
+ <li class="r2 ">
104
+ <span class='object_link'><a href="DirTravel/Travel.html#initialize-instance_method" title="DirTravel::Travel#initialize (method)">#initialize</a></span>
105
+ <small>DirTravel::Travel</small>
106
+ </li>
107
+
108
+
109
+ <li class="r1 ">
110
+ <span class='object_link'><a href="DirTravel/DirEntry.html#initialize-instance_method" title="DirTravel::DirEntry#initialize (method)">#initialize</a></span>
111
+ <small>DirTravel::DirEntry</small>
112
+ </li>
113
+
114
+
115
+ <li class="r2 ">
116
+ <span class='object_link'><a href="DirTravel/Entry.html#name-instance_method" title="DirTravel::Entry#name (method)">#name</a></span>
117
+ <small>DirTravel::Entry</small>
118
+ </li>
119
+
120
+
121
+ <li class="r1 ">
122
+ <span class='object_link'><a href="DirTravel/Entry.html#parts-instance_method" title="DirTravel::Entry#parts (method)">#parts</a></span>
123
+ <small>DirTravel::Entry</small>
124
+ </li>
125
+
126
+
127
+ <li class="r2 ">
128
+ <span class='object_link'><a href="DirTravel/Entry.html#path-instance_method" title="DirTravel::Entry#path (method)">#path</a></span>
129
+ <small>DirTravel::Entry</small>
130
+ </li>
131
+
132
+
133
+ <li class="r1 ">
134
+ <span class='object_link'><a href="DirTravel/Entry.html#relative%3F-instance_method" title="DirTravel::Entry#relative? (method)">#relative?</a></span>
135
+ <small>DirTravel::Entry</small>
136
+ </li>
137
+
138
+
139
+ <li class="r2 ">
140
+ <span class='object_link'><a href="DirTravel/Entry.html#rename-instance_method" title="DirTravel::Entry#rename (method)">#rename</a></span>
141
+ <small>DirTravel::Entry</small>
142
+ </li>
143
+
144
+
145
+ <li class="r1 ">
146
+ <span class='object_link'><a href="DirTravel/Travel.html#root-instance_method" title="DirTravel::Travel#root (method)">#root</a></span>
147
+ <small>DirTravel::Travel</small>
148
+ </li>
149
+
150
+
151
+ <li class="r2 ">
152
+ <span class='object_link'><a href="DirTravel/Entry.html#select_level-instance_method" title="DirTravel::Entry#select_level (method)">#select_level</a></span>
153
+ <small>DirTravel::Entry</small>
154
+ </li>
155
+
156
+
157
+ <li class="r1 ">
158
+ <span class='object_link'><a href="DirTravel/Entry.html#stat-instance_method" title="DirTravel::Entry#stat (method)">#stat</a></span>
159
+ <small>DirTravel::Entry</small>
160
+ </li>
161
+
162
+
163
+ <li class="r2 ">
164
+ <span class='object_link'><a href="DirTravel/Entry.html#subpath-instance_method" title="DirTravel::Entry#subpath (method)">#subpath</a></span>
165
+ <small>DirTravel::Entry</small>
166
+ </li>
167
+
168
+
169
+ <li class="r1 ">
170
+ <span class='object_link'><a href="DirTravel/FileEntry.html#suffix-instance_method" title="DirTravel::FileEntry#suffix (method)">#suffix</a></span>
171
+ <small>DirTravel::FileEntry</small>
172
+ </li>
173
+
174
+
175
+ <li class="r2 ">
176
+ <span class='object_link'><a href="DirTravel/Entry.html#tip-instance_method" title="DirTravel::Entry#tip (method)">#tip</a></span>
177
+ <small>DirTravel::Entry</small>
178
+ </li>
179
+
180
+
181
+ <li class="r1 ">
182
+ <span class='object_link'><a href="DirTravel/Travel.html#travel-instance_method" title="DirTravel::Travel#travel (method)">#travel</a></span>
183
+ <small>DirTravel::Travel</small>
184
+ </li>
185
+
186
+
187
+ </ul>
188
+ </div>
189
+ </body>
190
+ </html>
@@ -0,0 +1,112 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.8.6.1
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ hasFrames = window.top.frames.main ? true : false;
19
+ relpath = '';
20
+ framesUrl = "frames.html#!" + escape(window.location.href);
21
+ </script>
22
+
23
+
24
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
25
+
26
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
27
+
28
+
29
+ </head>
30
+ <body>
31
+ <div id="header">
32
+ <div id="menu">
33
+
34
+ <a href="_index.html">Index</a> &raquo;
35
+
36
+
37
+ <span class="title">Top Level Namespace</span>
38
+
39
+
40
+ <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
41
+ </div>
42
+
43
+ <div id="search">
44
+
45
+ <a class="full_list_link" id="class_list_link"
46
+ href="class_list.html">
47
+ Class List
48
+ </a>
49
+
50
+ <a class="full_list_link" id="method_list_link"
51
+ href="method_list.html">
52
+ Method List
53
+ </a>
54
+
55
+ <a class="full_list_link" id="file_list_link"
56
+ href="file_list.html">
57
+ File List
58
+ </a>
59
+
60
+ </div>
61
+ <div class="clear"></div>
62
+ </div>
63
+
64
+ <iframe id="search_frame"></iframe>
65
+
66
+ <div id="content"><h1>Top Level Namespace
67
+
68
+
69
+
70
+ </h1>
71
+
72
+ <dl class="box">
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ </dl>
82
+ <div class="clear"></div>
83
+
84
+ <h2>Defined Under Namespace</h2>
85
+ <p class="children">
86
+
87
+
88
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="DirTravel.html" title="DirTravel (module)">DirTravel</a></span>
89
+
90
+
91
+
92
+
93
+ </p>
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+ </div>
104
+
105
+ <div id="footer">
106
+ Generated on Sat Jan 18 17:51:32 2014 by
107
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
108
+ 0.8.6.1 (ruby-1.9.3).
109
+ </div>
110
+
111
+ </body>
112
+ </html>
data/lib/dirtravel.rb CHANGED
@@ -7,7 +7,11 @@
7
7
  # content. The "filetree" class method provides interface for getting
8
8
  # the content with few options (see: DirTravel::Travel.filetree for
9
9
  # details).
10
- #
10
+ #
11
+ # See the DirTravel::Entry methods doc for possibilities in examining
12
+ # the directory hierachy.
13
+ #
14
+ #
11
15
  # Examples:
12
16
  # require 'dirtravel'
13
17
  #
@@ -25,7 +29,7 @@
25
29
  # end
26
30
  #
27
31
  # # Get MP3 files and create a list of all album directories
28
- # # (assuming "...<album>/<song>" hierarhcy).
32
+ # # (assuming ".../<album>/<song>" hierarhcy).
29
33
  # d = DirTravel::Travel.filetree( '.', { :suffix => '.mp3' } )
30
34
  # albums = d.select_level( d.node_height - 1 )
31
35
  #
@@ -35,12 +39,16 @@ module DirTravel
35
39
  require 'rubytree'
36
40
 
37
41
 
42
+ class DirTravelError < RuntimeError; end
43
+
44
+
38
45
  # Extend RubyTree base class with file and directory features.
39
46
  class Entry < Tree::TreeNode
40
47
  attr_accessor :name
41
48
 
42
49
  def initialize( name )
43
50
  super( name, nil )
51
+ @abspath = nil
44
52
  end
45
53
 
46
54
 
@@ -48,64 +56,125 @@ module DirTravel
48
56
  #
49
57
  # @param basedir [Entry] Starting level for the hierarchy.
50
58
  # @return [Array] Array of names in hierarchy.
51
- def pathArray( basedir = self )
59
+ def parts( basedir = self )
52
60
  parents = []
53
61
  while basedir
54
- parents.push basedir.name
62
+ parents.push basedir.tip
55
63
  basedir = basedir.parent
56
64
  end
57
65
  parents.reverse
58
66
  end
59
67
 
68
+ alias pathArray parts
69
+
70
+
60
71
  # Relative path.
61
72
  def path
62
- pathArray.join( '/' )
73
+ parts.join( '/' )
63
74
  end
64
75
 
76
+
65
77
  # Relative path under root.
66
- def subpath
67
- pathArray[ 1 .. -1 ].join( '/' )
78
+ #
79
+ # @param level [Integer] Level down from root.
80
+ def subpath( level = 1 )
81
+ pa = parts
82
+ if level < 0 || level > pa.length
83
+ raise DirTravelError, "Invalid index for subpath level!"
84
+ end
85
+ pa[ level .. -1 ].join( '/' )
68
86
  end
69
87
 
88
+
70
89
  # Absolute path.
71
90
  def abspath
72
- File.absolute_path( path )
91
+ if @abspath
92
+ @abspath
93
+ elsif self == root
94
+ Dir.pwd
95
+ else
96
+ root.abspath + '/' + subpath
97
+ end
98
+ end
99
+
100
+
101
+ # Top directory name (usually same as name).
102
+ def tip
103
+ @name.split( "/" )[-1]
73
104
  end
74
105
 
75
- # Return containing directory name.
106
+
107
+ # Relative path of parenting directory.
76
108
  #
77
109
  # @param basedir [Entry] Starting level for the hierarchy.
78
110
  # @return [String] Containing directory.
79
111
  def dir( basedir = self )
80
- pathArray( basedir.parent ).join( '/' )
112
+ parts( basedir.parent ).join( '/' )
81
113
  end
82
114
 
115
+
83
116
  # Select all siblings from given node depth.
84
117
  #
85
118
  # @param level [Integer] Selected level in hierachy. Level is
86
- # number of step down in hierarhcy.
119
+ # number of steps down in hierarhcy.
87
120
  # @return [Array] Array of siblings in selected hierarchy.
88
121
  def select_level( level )
89
122
  select do |i| i.node_depth == level; end
90
123
  end
91
124
 
125
+
92
126
  # Return all file entries in hierarchy.
93
127
  def files
94
128
  select do |i| i.kind_of?( FileEntry ) end
95
129
  end
96
130
 
131
+
97
132
  # File.stat data for the Entry.
98
133
  def stat
99
134
  File.stat( path )
100
135
  end
136
+
137
+ # Relative path Entry.
138
+ def relative?
139
+ @name[0] != '/'
140
+ end
141
+
142
+ # Rename node.
143
+ #
144
+ # @param name [String] If name is abspath then Entry becomes
145
+ # abspath.
146
+ def rename( name )
147
+ @name = name
148
+
149
+ # Absolute or relative path?
150
+ if name[0] == "/"
151
+ @abspath = name
152
+ else
153
+ @abspath = nil
154
+ end
155
+ end
156
+
101
157
  end
102
158
 
103
159
 
104
160
  # Directory type entry.
105
161
  class DirEntry < Entry
106
- def initialize( name )
162
+
163
+ # Instantiate.
164
+ #
165
+ # @param name [String] Directory name.
166
+ # @param root [Boolean] If root, abspath is set now.
167
+ def initialize( name, root = false )
107
168
  super( name )
169
+ if root
170
+ if name[0] == '/'
171
+ @abspath = name
172
+ else
173
+ @abspath = Dir.pwd + '/' + name
174
+ end
175
+ end
108
176
  end
177
+
109
178
  end
110
179
 
111
180
 
@@ -133,6 +202,8 @@ module DirTravel
133
202
  # Limit search to files with "suffix" (default: nil).
134
203
  # files::
135
204
  # Include files to search (default: true).
205
+ # inclusive::
206
+ # Basedirs parent becomes the basedir (default: false).
136
207
  #
137
208
  # Example:
138
209
  # d1 = DirTravel::Travel.filetree( dir1, { :sort => true, :suffix => '.mp3' } )
@@ -153,8 +224,21 @@ module DirTravel
153
224
  # @param options [Hash] Hash optionally including keys: :sort, :suffix, :files.
154
225
  # @return [DirEntry] Root item of the file system hierarchy.
155
226
  def Travel.filetree( basedir = '.', options = {} )
227
+
156
228
  r = Travel.new( basedir, options )
157
229
  r.travel
230
+
231
+ if r.defaults[ :inclusive ]
232
+ path = File.dirname( r.root.abspath )
233
+ if r.root.relative?
234
+ path = File.basename( path )
235
+ end
236
+ newRoot = DirEntry.new( path, true )
237
+ r.root.rename( r.root.tip )
238
+ newRoot.add( r.root )
239
+ r.root = newRoot
240
+ end
241
+
158
242
  r.root
159
243
  end
160
244
 
@@ -163,14 +247,16 @@ module DirTravel
163
247
  @basedir = basedir
164
248
 
165
249
  @defaults = {
166
- :suffix => nil,
167
- :sort => false,
168
- :files => true,
250
+ :suffix => nil,
251
+ :sort => false,
252
+ :files => true,
253
+ :inclusive => false,
169
254
  }
170
255
 
171
256
  @defaults.merge!( options )
172
257
 
173
- @root = DirEntry.new( @basedir )
258
+ @root = DirEntry.new( clean( @basedir ), true )
259
+
174
260
  end
175
261
 
176
262
 
@@ -183,6 +269,17 @@ module DirTravel
183
269
 
184
270
  private
185
271
 
272
+ # Clean relative path head (remove dot etc).
273
+ def clean( dir )
274
+ if dir[0..1] == './'
275
+ dir = dir[ 2..-1 ]
276
+ elsif dir[0] == '.'
277
+ dir = ""
278
+ end
279
+ dir
280
+ end
281
+
282
+
186
283
  # Recursively get all files with suffix. Ignore suffix if
187
284
  # suffix is nil.
188
285
  def entriesIn( dir, node, suffix = nil )
@@ -32,10 +32,16 @@ class DirTravelTest < Test::Unit::TestCase
32
32
 
33
33
  def test_paths
34
34
  data = DirTravel::Travel.filetree( 'test' )
35
- assert_equal( 'test/test_0/test_0_1', data.select_level( 2 )[ 0 ].path )
36
- assert_equal( 'test/test_0/test_0_0', data.select_level( 2 )[ 1 ].path )
37
- assert_equal( 'test_0/test_0_1', data.select_level( 2 )[ 0 ].subpath )
38
- assert_equal( 'test/test_0', data.select_level( 2 )[ 0 ].dir )
35
+ 2.times do
36
+ assert_equal( 'test/test_0/test_0_1', data.select_level( 2 )[ 0 ].path )
37
+ assert_equal( 'test/test_0/test_0_0', data.select_level( 2 )[ 1 ].path )
38
+ assert_equal( 'test_0/test_0_1', data.select_level( 2 )[ 0 ].subpath )
39
+ assert_equal( 'test_0_1', data.select_level( 2 )[ 0 ].subpath( 2 ) )
40
+ assert_equal( 'test/test_0', data.select_level( 2 )[ 0 ].dir )
41
+ assert_equal( Dir.pwd + '/test/test_0/test_0_1',
42
+ data.select_level( 2 )[ 0 ].abspath )
43
+ data = DirTravel::Travel.filetree( Dir.pwd + '/test' )
44
+ end
39
45
  end
40
46
 
41
47
  def test_stat
@@ -59,4 +65,45 @@ class DirTravelTest < Test::Unit::TestCase
59
65
  assert_equal( 5, data.length )
60
66
  end
61
67
 
68
+ def test_tip_rel
69
+ target = 'test'
70
+ data = DirTravel::Travel.filetree( target )
71
+ assert_equal( target, data.name )
72
+ assert_equal( 'test', data.tip )
73
+ end
74
+
75
+ def test_tip_abs
76
+ pwd = Dir.pwd
77
+ target = pwd + '/test'
78
+ data = DirTravel::Travel.filetree( target )
79
+ assert_equal( target, data.name )
80
+ assert_equal( 'test', data.tip )
81
+ end
82
+
83
+ def test_inclusive_rel
84
+ pwd = 'test'
85
+ target = pwd + '/test_0'
86
+ data = DirTravel::Travel.filetree( target, { :inclusive => true } )
87
+ assert_equal( pwd, data.name )
88
+ assert_equal( 'test', data.tip )
89
+ assert_equal( 1, data.children.length )
90
+ root = data.children[0]
91
+ assert_equal( 'test_0', root.name )
92
+ assert_equal( 'test_0', root.tip )
93
+ assert_equal( 'test/test_0', root.path )
94
+ end
95
+
96
+ def test_inclusive_abs
97
+ pwd = Dir.pwd + '/test'
98
+ target = pwd + '/test_0'
99
+ data = DirTravel::Travel.filetree( target, { :inclusive => true } )
100
+ assert_equal( pwd, data.name )
101
+ assert_equal( 'test', data.tip )
102
+ assert_equal( 1, data.children.length )
103
+ root = data.children[0]
104
+ assert_equal( 'test_0', root.name )
105
+ assert_equal( 'test_0', root.tip )
106
+ assert_equal( 'test/test_0', root.path )
107
+ end
108
+
62
109
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirtravel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-08 00:00:00.000000000 Z
12
+ date: 2014-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubytree
@@ -37,10 +37,32 @@ extra_rdoc_files:
37
37
  - README.rdoc
38
38
  files:
39
39
  - README.rdoc
40
+ - CHANGELOG.rdoc
40
41
  - LICENSE
41
42
  - Rakefile
42
43
  - lib/dirtravel.rb
43
44
  - test/test_dirtravel.rb
45
+ - doc/top-level-namespace.html
46
+ - doc/js/full_list.js
47
+ - doc/js/app.js
48
+ - doc/js/jquery.js
49
+ - doc/index.html
50
+ - doc/css/common.css
51
+ - doc/css/full_list.css
52
+ - doc/css/style.css
53
+ - doc/DirTravel/Travel.html
54
+ - doc/DirTravel/DirEntry.html
55
+ - doc/DirTravel/DirTravelError.html
56
+ - doc/DirTravel/Entry.html
57
+ - doc/DirTravel/FileEntry.html
58
+ - doc/file.README.html
59
+ - doc/method_list.html
60
+ - doc/file_list.html
61
+ - doc/DirTravel.html
62
+ - doc/class_list.html
63
+ - doc/_index.html
64
+ - doc/file.CHANGELOG.html
65
+ - doc/frames.html
44
66
  homepage:
45
67
  licenses:
46
68
  - Ruby