class Files

Attributes

files[W]
path[R]
pattern[R]

Public Class Methods

find(options = {}) click to toggle source
# File lib/Files.rb, line 66
def find(options = {})
  path, pattern, return_files_object = options[:path], options[:pattern], options[:return_files_object]
  case
  when path && pattern
    in_path___with_pattern___(path, pattern, return_files_object)
  when path && !pattern
    in_path___(path, return_files_object)
  when !path && pattern
    with_pattern___(pattern, return_files_object)
  when !path && !pattern
    here(return_files_object)
  end
end
gsub!(filenames, replacement_pattern, replacement_text, selection_pattern = nil) click to toggle source
# File lib/Files.rb, line 127
def gsub!(filenames, replacement_pattern, replacement_text, selection_pattern = nil)
  filenames.each do |filename|
    File.open(filename, 'r+') do |f|
      f.gsub!(replacement_pattern, replacement_text, selection_pattern)
    end
  end
end
here(return_files_object = true) click to toggle source
# File lib/Files.rb, line 104
def here(return_files_object = true)
  in_path___matching___('./', '*', return_files_object)
end
in_path___(path, return_files_object = true) click to toggle source
# File lib/Files.rb, line 94
def in_path___(path, return_files_object = true)
  in_path___matching___(path, '*', return_files_object)
end
in_path___with_pattern___(path, pattern, return_files_object = true) click to toggle source
# File lib/Files.rb, line 81
def in_path___with_pattern___(path, pattern, return_files_object = true)
  files = with_attributes(Dir["#{path}/#{pattern}"])
  if return_files_object
    files_object = Files.new
    files_object.files = files
    files_object
  else
    files
  end
end
move(filenames, replacement_pattern, replacement_text) click to toggle source
# File lib/Files.rb, line 135
def move(filenames, replacement_pattern, replacement_text)
  filenames.each do |filename|
    new_filename = filename.gsub(/#{replacement_pattern}/, replacement_text)
    FileUtils.mv(filename, new_filename)
  end
end
new(*args) click to toggle source
# File lib/Files.rb, line 151
def initialize(*args)
  options = args.extract_options!
  @args, @path, @pattern = args, options[:path], options[:pattern]
  load_metaprogrammed_methods
end
with_attributes(paths) click to toggle source
# File lib/Files.rb, line 108
def with_attributes(paths)
  paths.collect do |path|
    begin
      f = {}
      f[:path] = path
      f[:size] = File.stat(path).size
      f[:ftype] = File.stat(path).ftype
      f[:mode] = File.stat(path).mode
      f[:gid] = File.stat(path).gid
      f[:uid] = File.stat(path).uid
      f[:ctime] = File.stat(path).ctime
      f[:mtime] = File.stat(path).mtime
      f[:atime] = File.stat(path).atime
      OpenStruct.new(f)
    rescue
    end
  end
end
with_pattern___(pattern, return_files_object = true) click to toggle source
# File lib/Files.rb, line 99
def with_pattern___(pattern, return_files_object = true)
  in_path___matching___('./', pattern, return_files_object)
end

Public Instance Methods

absolute_dirnames() click to toggle source
# File lib/Files.rb, line 189
def absolute_dirnames
  files.collect{|f| File.dirname(File.absolute_path(f.path))}
end
absolute_paths() click to toggle source
# File lib/Files.rb, line 181
def absolute_paths
  files.collect{|f| File.expand_path(f.path)}
end
dirnames() click to toggle source
# File lib/Files.rb, line 185
def dirnames
  files.collect{|f| File.dirname(f.path)}
end
each() { |f| ... } click to toggle source
# File lib/Files.rb, line 173
def each
  files.each{|f| yield f}
end
files() click to toggle source
# File lib/Files.rb, line 157
def files
  @files ||= (
    if !@args.empty?
      Files.with_attributes(@args.flatten)
    elsif path && pattern
      Files.find(path: path, pattern: pattern, return_files_object: false)
    elsif path
      Files.find(path: path, return_files_object: false)
    elsif pattern
      Files.find(pattern: pattern, return_files_object: false)
    else
      []
    end
  )
end
gsub!(replacement_pattern, replacement_text) click to toggle source
# File lib/Files.rb, line 193
def gsub!(replacement_pattern, replacement_text)
  Files.gsub!(paths, replacement_pattern, replacement_text)
end
move(replacement_pattern, replacement_text) click to toggle source
# File lib/Files.rb, line 197
def move(replacement_pattern, replacement_text)
  Files.move(paths, replacement_pattern, replacement_text)
end
path=(new_path) click to toggle source
# File lib/Files.rb, line 202
def path=(new_path)
  @path = new_path
  set_files
end
paths() click to toggle source
# File lib/Files.rb, line 177
def paths
  files.collect{|f| f.path}
end
pattern=(new_pattern) click to toggle source
# File lib/Files.rb, line 207
def pattern=(new_pattern)
  @pattern = new_pattern
  set_files
end

Private Instance Methods

load_metaprogrammed_methods() click to toggle source
# File lib/Files.rb, line 219
def load_metaprogrammed_methods
  sort_method_macro
  reverse_sort_method_macro
  sorter_macro
end
reverse_sort_method_macro() click to toggle source
# File lib/Files.rb, line 248
def reverse_sort_method_macro
  %w(atime ctime mtime size ftype mode gid uid filename).each do |method|
    instance_eval("
      def reverse_sort_by_#{method}
        files.sort! do |a,b|
          by_#{method}(a,b)
        end.reverse!
        self
      end
    ")
  end
end
set_files() click to toggle source
# File lib/Files.rb, line 214
def set_files
  @files = nil
  files
end
sort_method_macro() click to toggle source
# File lib/Files.rb, line 225
def sort_method_macro
  %w(atime ctime mtime size ftype mode gid uid filename).each do |method|
    instance_eval("
      def sort_by_#{method}
        files.sort! do |a,b|
          by_#{method}(a,b)
        end
        self
      end
    ")
  end
end
sorter_macro() click to toggle source
# File lib/Files.rb, line 238
def sorter_macro
  %w(atime ctime mtime size ftype mode gid uid filename).each do |method|
    instance_eval("
      def by_#{method}(a,b)
        a.#{method} <=> b.#{method}
      end
    ")
  end
end