dirtravel 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dirtravel.rb CHANGED
@@ -1,15 +1,15 @@
1
- # DirTravel is a library for getting information about files and
1
+ # {DirTravel} is a library for getting information about files and
2
2
  # directories recursively to a tree structure. This library extends
3
3
  # the RubyTree classes to include directory and files info. Please
4
4
  # refer to RubyTree documentation for RubyTree related features.
5
5
  #
6
- # A proxy object "DirTravel::Travel" is used to gather the directory
6
+ # A proxy object {DirTravel::Travel} is used to gather the directory
7
7
  # content. The "filetree" class method provides interface for getting
8
- # the content with few options (see: DirTravel::Travel.filetree for
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.
11
+ # See the {DirTravel::Entry} methods doc for possibilities in
12
+ # examining the directory hierachy.
13
13
  #
14
14
  #
15
15
  # Examples:
@@ -28,24 +28,30 @@
28
28
  # puts i.abspath
29
29
  # end
30
30
  #
31
- # # Get MP3 files and create a list of all album directories
32
- # # (assuming ".../<album>/<song>" hierarhcy).
31
+ # # Get MP3 files and create a list of all album directories.
32
+ # # Assume: ".../<album>/<song>" hierarhcy
33
+ # #
33
34
  # d = DirTravel::Travel.filetree( '.', { :suffix => '.mp3' } )
34
35
  # albums = d.select_level( d.node_height - 1 )
35
36
  #
36
37
 
37
38
  module DirTravel
38
-
39
+
39
40
  require 'rubytree'
40
41
 
41
42
 
43
+ # {DirTravel} error.
42
44
  class DirTravelError < RuntimeError; end
43
-
45
+
44
46
 
45
47
  # Extend RubyTree base class with file and directory features.
46
48
  class Entry < Tree::TreeNode
49
+
50
+ # Node name.
47
51
  attr_accessor :name
48
52
 
53
+ # Set name for {Entry} (Dir/File).
54
+ # Initialize abspath.
49
55
  def initialize( name )
50
56
  super( name, nil )
51
57
  @abspath = nil
@@ -103,7 +109,7 @@ module DirTravel
103
109
 
104
110
 
105
111
  # Relative path of parenting directory.
106
- #
112
+ #
107
113
  # @param basedir [Entry] Starting level for the hierarchy.
108
114
  # @return [String] Containing directory.
109
115
  def dir( basedir = self )
@@ -114,7 +120,7 @@ module DirTravel
114
120
  # Select all siblings from given node depth.
115
121
  #
116
122
  # @param level [Integer] Selected level in hierachy. Level is
117
- # number of steps down in hierarhcy.
123
+ # number of steps down in hierarhcy.
118
124
  # @return [Array] Array of siblings in selected hierarchy.
119
125
  def select_level( level )
120
126
  select do |i| i.node_depth == level; end
@@ -127,23 +133,25 @@ module DirTravel
127
133
  end
128
134
 
129
135
 
130
- # File.stat data for the Entry.
136
+ # File.stat data for the {Entry}.
131
137
  def stat
132
138
  File.stat( path )
133
139
  end
134
140
 
135
- # Relative path Entry.
141
+
142
+ # Relative path {Entry}.
136
143
  def relative?
137
144
  @name[0] != '/'
138
145
  end
139
146
 
147
+
140
148
  # Rename node.
141
149
  #
142
- # @param name [String] If name is abspath then Entry becomes
150
+ # @param name [String] If name is abspath then {Entry} becomes
143
151
  # abspath.
144
152
  def rename( name )
145
153
  @name = name
146
-
154
+
147
155
  # Absolute or relative path?
148
156
  if name[0] == "/"
149
157
  @abspath = name
@@ -153,15 +161,16 @@ module DirTravel
153
161
  end
154
162
 
155
163
  end
156
-
164
+
157
165
 
158
166
  # Directory type entry.
159
167
  class DirEntry < Entry
160
168
 
161
- # Instantiate.
169
+ # Set name and abspath if given.
162
170
  #
163
171
  # @param name [String] Directory name.
164
- # @param abspath [String] Set abspath if given.
172
+ # @param abspath [String] Set abspath if given. Otherwise it
173
+ # is generated from pieces.
165
174
  def initialize( name, abspath = nil )
166
175
  super( name )
167
176
  if abspath
@@ -170,25 +179,33 @@ module DirTravel
170
179
  end
171
180
 
172
181
  end
173
-
174
-
182
+
183
+
175
184
  # File type entry.
176
185
  class FileEntry < Entry
177
186
  attr_reader :suffix, :basename
178
-
187
+
188
+ # Set name, suffix, and basename.
189
+ #
190
+ # @param name [String] File name.
179
191
  def initialize( name )
180
192
  super( name )
181
193
  @suffix = File.extname( name )
182
194
  @basename = File.basename( name, @suffix )
183
195
  end
184
-
196
+
185
197
  end
186
198
 
187
199
 
188
200
  # Create directory recursion tree (with
189
- # Travel.filetree). Optionally filter with suffix and modify
201
+ # {Travel.filetree}). Optionally filter with suffix and modify
190
202
  # tree building with options Hash (see below).
191
- #
203
+ #
204
+ # If basedir is an absolute path, the root name is the search
205
+ # path. If basedir is a relative path, the current directory is
206
+ # changed to referenced directory before search and root name is
207
+ # the current directory, single level.
208
+ #
192
209
  # == Parameters:
193
210
  # sort::
194
211
  # Sort directory entries (default: false).
@@ -204,65 +221,104 @@ module DirTravel
204
221
 
205
222
  class Travel
206
223
 
207
- # Root DirEntry of Travel.
224
+ # Root {DirEntry} of {Travel}.
208
225
  attr_accessor :root
209
226
 
210
- # Default options for Travel.
227
+ # Default options for {Travel}.
211
228
  attr_accessor :defaults
212
229
 
213
- # Starting directory for Travel.
230
+ # Starting directory for {Travel}.
214
231
  attr_accessor :basedir
215
232
  attr_accessor :abspath
216
-
217
- # Create directory recursion tree.
233
+
234
+
235
+ # Create directory recursion tree and return root.
236
+ #
218
237
  # @param basedir [String] Starting directory (top).
219
- # @param options [Hash] Hash optionally including keys: :sort, :suffix, :files.
220
- # @return [DirEntry] Root item of the file system hierarchy.
238
+ # @param options [Hash] {Travel} options.
239
+ # @return [DirEntry] Root of the file travel hierarchy.
221
240
  def Travel.filetree( basedir = '.', options = {} )
222
241
 
242
+ # Non-nil if directory needs to be changed.
223
243
  pwd = nil
224
244
 
225
245
  if basedir[0] == '/'
246
+
247
+ # Absolue path.
226
248
  t = Travel.new( basedir, basedir, options )
227
249
  t.travel
250
+
228
251
  else
252
+
253
+ # Relative path.
254
+
255
+ # Store current directory before chdir.
229
256
  pwd = Dir.pwd
257
+
258
+ # Generate target directory for chdir. One up from
259
+ # the reference.
230
260
  full = File.absolute_path( basedir )
231
261
  base = File.basename( full )
232
262
  dir = File.dirname( full )
263
+
264
+ # Goto target.
233
265
  Dir.chdir( dir )
266
+
234
267
  t = Travel.new( base, full, options )
235
268
  t.travel
236
269
  end
237
270
 
271
+
238
272
  if t.defaults[ :inclusive ]
273
+
274
+ # With inclusive the root is changed to one-up from
275
+ # target.
276
+
277
+ # One up from root.
239
278
  uppath = File.dirname( t.root.abspath )
240
- path = uppath
241
279
  if t.root.relative?
242
280
  path = File.basename( uppath )
281
+ else
282
+ path = uppath
243
283
  end
284
+
285
+ # Create the "one-up" root.
244
286
  newRoot = DirEntry.new( path, uppath )
287
+
288
+ # Rename old root.
245
289
  t.root.rename( t.root.tip )
290
+
291
+ # Add to one-up root.
246
292
  newRoot.add( t.root )
293
+
294
+ # Set root to one-up root.
247
295
  t.root = newRoot
248
296
  end
249
297
 
298
+ # Return back to start directory if dir changed.
250
299
  Dir.chdir( pwd ) if pwd
251
300
 
301
+ # Return root {DirEntry}.
252
302
  t.root
253
303
  end
254
304
 
255
305
 
306
+ # Create directory recursion object. Overlay options on top of
307
+ # defaults. Initialize root {DirEntry}.
308
+ #
309
+ # @param basedir [String] Starting directory (top).
310
+ # @param abspath [String] Absolute path for root.
311
+ # @param options [Hash] {Travel} options.
256
312
  def initialize( basedir, abspath, options = {} )
257
313
  @basedir = basedir
258
314
  @abspath = abspath
259
315
 
260
316
  @defaults = {
261
- :suffix => nil,
262
- :sort => false,
263
- :files => true,
264
- :inclusive => false,
265
- }
317
+ :suffix => nil,
318
+ :sort => false,
319
+ :files => true,
320
+ :inclusive => false,
321
+ }
266
322
 
267
323
  @defaults.merge!( options )
268
324
 
@@ -288,7 +344,7 @@ module DirTravel
288
344
  list.each do |i|
289
345
 
290
346
  if File.file?( dir + '/' + i )
291
-
347
+
292
348
  # File entry.
293
349
 
294
350
  if ( !suffix || suffix == File.extname( i ) ) &&
@@ -325,5 +381,8 @@ module DirTravel
325
381
 
326
382
  end
327
383
 
328
-
384
+
329
385
  end
386
+
387
+
388
+ require_relative 'version'
data/lib/version.rb ADDED
@@ -0,0 +1,6 @@
1
+ module DirTravel
2
+ VERSION = "0.0.7"
3
+ def DirTravel.version
4
+ DirTravel::VERSION
5
+ end
6
+ end
@@ -2,7 +2,7 @@ require 'test/unit'
2
2
  require 'dirtravel'
3
3
 
4
4
  class DirTravelTest < Test::Unit::TestCase
5
-
5
+
6
6
  require 'fileutils'
7
7
 
8
8
  Dir.chdir 'test'
@@ -24,12 +24,12 @@ class DirTravelTest < Test::Unit::TestCase
24
24
  data = DirTravel::Travel.filetree( 'test' )
25
25
  assert_equal( 3, data.node_height )
26
26
  end
27
-
27
+
28
28
  def test_suffix_selection
29
29
  data = DirTravel::Travel.filetree( 'test', { :suffix => ".txt" } )
30
- data.files.each do |i| assert_match( i.name, /.txt$/ ) end
30
+ data.files.each do |i| assert_match( /.txt$/, i.name ) end
31
31
  end
32
-
32
+
33
33
  def test_paths
34
34
  data = DirTravel::Travel.filetree( 'test' )
35
35
  2.times do
@@ -134,5 +134,5 @@ class DirTravelTest < Test::Unit::TestCase
134
134
  assert_equal( 'test_0', root.tip )
135
135
  assert_equal( 'test/test_0', root.path )
136
136
  end
137
-
137
+
138
138
  end
metadata CHANGED
@@ -1,32 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dirtravel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
5
- prerelease:
4
+ version: 0.0.7
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tero Isannainen
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-01-23 00:00:00.000000000 Z
11
+ date: 2024-06-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rubytree
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: 0.8.3
19
+ version: 2.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: 0.8.3
26
+ version: 2.0.0
30
27
  description: DirTravel provides content of filesystem directories by recursively travelling
31
28
  the directory hierarchy. The content is organized in RubyTree structure and RubyTree
32
29
  classes are overloaded with directory entry methods for usability.
@@ -35,58 +32,56 @@ executables: []
35
32
  extensions: []
36
33
  extra_rdoc_files:
37
34
  - README.rdoc
35
+ - CHANGELOG.rdoc
38
36
  files:
39
- - README.rdoc
40
37
  - CHANGELOG.rdoc
41
38
  - LICENSE
42
- - Rakefile
43
- - lib/dirtravel.rb
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
39
+ - README.rdoc
40
+ - doc/DirTravel.html
54
41
  - doc/DirTravel/DirEntry.html
55
42
  - doc/DirTravel/DirTravelError.html
56
43
  - doc/DirTravel/Entry.html
57
44
  - 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
45
+ - doc/DirTravel/Travel.html
63
46
  - doc/_index.html
47
+ - doc/class_list.html
48
+ - doc/css/common.css
49
+ - doc/css/full_list.css
50
+ - doc/css/style.css
64
51
  - doc/file.CHANGELOG.html
52
+ - doc/file.README.html
53
+ - doc/file_list.html
65
54
  - doc/frames.html
66
- homepage:
55
+ - doc/index.html
56
+ - doc/js/app.js
57
+ - doc/js/full_list.js
58
+ - doc/js/jquery.js
59
+ - doc/method_list.html
60
+ - doc/top-level-namespace.html
61
+ - lib/dirtravel.rb
62
+ - lib/version.rb
63
+ - test/test_dirtravel.rb
64
+ homepage:
67
65
  licenses:
68
66
  - Ruby
67
+ metadata: {}
69
68
  post_install_message: Check README...
70
69
  rdoc_options: []
71
70
  require_paths:
72
71
  - lib
73
72
  required_ruby_version: !ruby/object:Gem::Requirement
74
- none: false
75
73
  requirements:
76
- - - ! '>='
74
+ - - ">="
77
75
  - !ruby/object:Gem::Version
78
- version: 1.9.3
76
+ version: 3.0.0
79
77
  required_rubygems_version: !ruby/object:Gem::Requirement
80
- none: false
81
78
  requirements:
82
- - - ! '>='
79
+ - - ">="
83
80
  - !ruby/object:Gem::Version
84
81
  version: '0'
85
82
  requirements: []
86
- rubyforge_project:
87
- rubygems_version: 1.8.23
88
- signing_key:
89
- specification_version: 3
83
+ rubygems_version: 3.4.20
84
+ signing_key:
85
+ specification_version: 4
90
86
  summary: Small and simple library for accessing filesystem directories.
91
87
  test_files: []
92
- has_rdoc:
data/Rakefile DELETED
@@ -1,28 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << 'test'
5
- end
6
-
7
- desc "Run tests"
8
- task :default => :test
9
-
10
- task :cleanup_test do
11
- sh "rm -rf test/test"
12
- end
13
-
14
- task :build => :doc do
15
- sh "gem build dirtravel.gemspec"
16
- end
17
-
18
- task :doc do
19
- sh "yardoc lib/* - README.rdoc CHANGELOG.rdoc"
20
- end
21
-
22
- task :publish do
23
- if Dir.glob('dirtravel-*gem').length == 1
24
- sh "gem push dirtravel*.gem"
25
- else
26
- raise "Multiple gems in the directory..."
27
- end
28
- end