class RbFind::Walk

Constants

Params

Attributes

count[R]
current[R]
depth[R]
start[R]

Public Class Methods

new(max_depth: nil, args_depth: false, depth_first: nil, follow: nil, sort: true, dirs: false, reverse: false, error: nil, &block) click to toggle source
# File lib/rbfind.rb, line 276
def initialize max_depth: nil, args_depth: false, depth_first: nil, follow: nil,
                        sort: true, dirs: false, reverse: false, error: nil, &block
  @params = Params.new max_depth, args_depth, depth_first, follow,
              (sort_parser sort), dirs, reverse, error, block
  @start = Time.instance_eval { @start = Time.now }
  Time.instance_eval { @start = Time.now }
  @count, @depth = 0, 0
end
run(*args, **params, &block) click to toggle source
# File lib/rbfind.rb, line 264
def run *args, **params, &block
  i = new **params, &block
  i.run *args
  i.count
end

Public Instance Methods

run(*args) click to toggle source
# File lib/rbfind.rb, line 296
def run *args
  args.flatten!
  args.compact!
  if args.empty? then
    visit_dir Dir::CUR_DIR
  else
    list = args.map { |base| Entry.new base, self }
    list.select! { |e| handle_error do e.stat end }
    sort_entries list
    step_depth_args do
      list.each { |e| enter e }
    end
  end
end

Private Instance Methods

call_block() click to toggle source
# File lib/rbfind.rb, line 386
def call_block
  handle_error do
    begin
      @current.instance_eval &@params.block
    rescue Done
    end
  end
end
enter(elem) click to toggle source
# File lib/rbfind.rb, line 334
def enter elem
  c_, @current = @current, elem
  @count += 1
  if @params.depth_first then
    enter_dir
    begin
      call_block
    rescue Prune
      handle_error do
        raise "#{self.class}: prune with :depth_first is pointless."
      end
    end
  else
    begin
      call_block
      enter_dir if @current.path
    rescue Prune
    end
  end
ensure
  @current = c_
end
enter_dir() click to toggle source
# File lib/rbfind.rb, line 376
def enter_dir
  return unless @current.stat.directory? || (@params.follow &&
                @current.symlink? && @current.rstat.directory?)
  handle_error do
    @current.cyclic? and
      raise "Cyclic recursion in #{@current.path}"
    visit_dir @current.path
  end
end
handle_error() { || ... } click to toggle source
# File lib/rbfind.rb, line 395
def handle_error
  yield
rescue
  case @params.error
  when Proc   then @params.error.call
  when String then instance_eval @params.error
  else             raise
  end
  nil
end
sort_entries(list) click to toggle source
# File lib/rbfind.rb, line 366
def sort_entries list
  @params.sort.call list
  list.reverse! if @params.reverse
  if @params.dirs then
    list.replace list.partition { |e| e.rstat.directory? rescue nil }
    list.reverse! if @params.depth_first
    list.flatten!
  end
end
sort_parser(st) click to toggle source
# File lib/rbfind.rb, line 285
def sort_parser st
  case st
  when Proc       then proc { |l| l.sort_by! { |e| e.instance_eval &st } }
  when String     then proc { |l| l.sort_by! { |e| e.instance_eval st  } }
  when nil, false then proc { }
  else                 proc { |l| l.sort_by! { |e| e.name } }
  end
end
step_depth() { || ... } click to toggle source
# File lib/rbfind.rb, line 317
def step_depth
  @depth += 1
  yield
ensure
  @depth -= 1
end
step_depth_args() { || ... } click to toggle source
# File lib/rbfind.rb, line 324
def step_depth_args
  if @params[ :args_depth] then
    step_depth do
      yield
    end
  else
    yield
  end
end
visit_dir(dir) click to toggle source
# File lib/rbfind.rb, line 357
def visit_dir dir
  return if @params.max_depth and @params.max_depth <= @depth
  list = Dir.open dir do |d| d.children.map { |f| Entry.new f, self } end
  sort_entries list
  step_depth do
    list.each { |e| enter e }
  end
end