Class: ALib::Find2

Inherits:
Object show all
Defined in:
lib/alib-0.5.1/find2.rb

Overview

-*- Ruby -*- Copyright © 1998, 2001 Motoyuki Kasahara

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Traverse a file tree, replacement of ‘find.rb’.

Constant Summary collapse

FIND1 =

Constatnts

1
FIND2 =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = FIND1) ⇒ Find2

Initializer.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alib-0.5.1/find2.rb', line 27

def initialize(mode = FIND1)
#--{{{
  @mode   = mode
  @depth  = false
  @follow = false
  @xdev   = false

  @dirname_stats = Array.new
  @found_files = Array.new
  @target_device = 0

  @handle = lambda { |file, stat_result, block|
    case @mode
      when FIND1
        block ? block.call(file) : @found_files.push(file)
      when FIND2
        block ? block.call(file, stat_result) : @found_files.push([file, stat_result])
    end
  }
#--}}}
end

Instance Attribute Details

#depthObject (readonly)

Methods for accessing instances.



51
52
53
# File 'lib/alib-0.5.1/find2.rb', line 51

def depth
  @depth
end

#followObject (readonly)

Returns the value of attribute follow.



52
53
54
# File 'lib/alib-0.5.1/find2.rb', line 52

def follow
  @follow
end

#xdevObject (readonly)

Returns the value of attribute xdev.



53
54
55
# File 'lib/alib-0.5.1/find2.rb', line 53

def xdev
  @xdev
end

Class Method Details

.find(*arguments, &block) ⇒ Object

Find the directory ‘dirname’. (class method)



239
240
241
# File 'lib/alib-0.5.1/find2.rb', line 239

def self.find(*arguments, &block)
  new.find(*arguments, &block)
end

.find2(*arguments, &block) ⇒ Object

Find the directory ‘dirname’. (class method)



245
246
247
# File 'lib/alib-0.5.1/find2.rb', line 245

def self.find2(*arguments, &block)
  new(FIND2).find(*arguments, &block)
end

.pruneObject

Prune the current visited directory.



273
274
275
276
277
# File 'lib/alib-0.5.1/find2.rb', line 273

def self.prune
#--{{{
  throw :prune
#--}}}
end

Instance Method Details

#find(*arguments, &block) ⇒ Object

Find the directory ‘dirname’. (private)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/alib-0.5.1/find2.rb', line 57

def find(*arguments, &block)
#--{{{
  #
  # Parse options if specified.
  #
  #if arguments[0].type == Hash
    #parse_options(arguments.shift)
  #end

# hack
  args = []
  opts = []
  arguments.each do |arg|
    case arg
      when Hash
        opts << arg
      else
        args << arg
    end
  end
  opts.each{|opt| parse_options opt}
  files = args.flatten.compact

  #
  # If a block is not given to the `find' method, found files are
  # recorded in this array.
  #
  @dirname_stats.clear
  @found_files.clear

  #
  # Loop for each file in `files'.
  #
  files.each do |file|
    catch(:prune) do
    @dirname_stats.clear

    #
    # Get `stat' or `lstat' of `file'.
    #
    begin
      if @follow
        begin
          stat_result = File.stat(file)
        rescue Errno::ENOENT, Errno::EACCES
          stat_result = File.lstat(file)
        end
      else
        stat_result = File.lstat(file)
      end
    rescue Errno::ENOENT, Errno::EACCES
      next
    end

    #
    # Push `file' to the found stack, or yield with it,
    # if the depth flag is enabled.
    #
    @handle[ file, stat_result, block ] if !@depth

    #
    # If `file' is a directory, find files recursively.
    #
    if stat_result.directory?
      @xdev_device = stat_result.dev if @xdev
      @dirname_stats.push(stat_result)
      find_directory(file, block)
    end

    #
    # Push `file' to the found stack, or yield with it.
    # if the depth flag is disabled.
    #
    @handle[ file, stat_result, block ] if @depth

    end
  end

  if block == nil
    return @found_files
  else
    return nil
  end
#--}}}
end