class Blobsterix::DirectoryWalker

Attributes

child_index[RW]
child_walker[RW]
path[RW]

Public Class Methods

new(base_path, opts = {}) click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 5
def initialize(base_path, opts = {})
  @current_id = 0
  @child_walker = nil
  @path = Pathname.new base_path
  @child_index = opts[:child_index]
  init_path(opts[:start_path]) if opts[:start_path]
end

Public Instance Methods

current() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 71
def current
  current_(
    lambda{|walker|walker.current},
    lambda{|walker|walker.next},
    lambda{|new_path|new_path}
  )
end
current_entry() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 38
def current_entry
  entries[@current_id-1]
end
current_file() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 63
def current_file
  current_(
    lambda{|walker|walker.current_file},
    lambda{|walker|walker.current_file},
    lambda{|new_path|entries[current_id-1]}
  )
end
current_id() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 42
def current_id
  return @current_id if @current_id > 0
  increment_id
end
current_path() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 55
def current_path
  current_(
    lambda{|walker|walker.current_path},
    lambda{|walker|walker.current_path},
    lambda{|new_path|path}
  )
end
entries() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 34
def entries
  @entries ||= Dir.entries(path).sort
end
increment_id() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 47
def increment_id
  begin
    return nil if @current_id+1 > entries.size
    @current_id+=1
  end while (current_entry == "." || current_entry == ".." || current_entry == ".keep")
  @current_id
end
init_path(start_path) click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 13
def init_path(start_path)
  @start = Pathname.new(start_path)
  myentry = path_root(@start)

  entries.each_with_index do |entry,index|
    @current_id=index+1 if myentry.to_s == entry.to_s
  end

  set_childwalker(path.join(myentry), current_id-1, @start.relative_path_from(myentry)) if path.join(myentry).directory?
end
next() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 24
def next
  out = nil
  begin
    return current if @child_walker && @child_walker.next
    return nil unless increment_id
    out = current
  end while out == nil
  out
end

Private Instance Methods

current_(on_valid, on_new, on_file) click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 96
def current_(on_valid, on_new, on_file)
  return nil unless current_id
  return on_valid.call(@child_walker) if valid_childwalker?

  new_path = path.join(entries[current_id-1])
  if new_path.directory?
    @child_walker = DirectoryWalker.new(new_path, :child_index => current_id-1)
    on_new.call(@child_walker)
  else
    on_file.call(new_path)
  end
end
path_root(path_) click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 87
def path_root(path_)
  myentry = nil
  path_.descend do |entry|
    myentry = entry
    break
  end
  myentry
end
set_childwalker(path_, index_=nil, start_path_=nil) click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 80
def set_childwalker(path_, index_=nil, start_path_=nil)
  options = {}
  options[:child_index] = index_      if index_
  options[:start_path]  = start_path_ if start_path_
  @child_walker = DirectoryWalker.new(path_, options)
end
valid_childwalker?() click to toggle source
# File lib/blobsterix/helper/directory_list.rb, line 109
def valid_childwalker?
  @child_walker && @child_walker.child_index == current_id-1
end