class Tagmv::Filesystem

Attributes

root[RW]
files[R]
reorder[R]
tag_order[R]
tags[R]
top_level_tags[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/tagmv/filesystem.rb, line 11
def initialize(opts={})
  @tags =  scrub_tags(opts[:tags])
  @files = opts[:files]
  @dry_run = opts[:dry_run]
  @reorder = opts[:reorder]
  @tag_order = opts[:tag_order]
  @top_level_tags = opts[:top_level_tags]
end

Public Instance Methods

move_files() click to toggle source
# File lib/tagmv/filesystem.rb, line 59
def move_files
  # skip duplicate moves
  return if reorder && scrub_files.size == 1 && (scrub_files.first.sub(target_dir + '/','') !=~ /\//)

  FileUtils.mv(scrub_files, target_dir, options)
rescue ArgumentError
end
prepare_dir() click to toggle source
# File lib/tagmv/filesystem.rb, line 52
def prepare_dir
  @@prepare_dir ||= Hash.new do |h, key|
    h[key] = FileUtils.mkdir_p(key, options)
  end
  @@prepare_dir[target_dir]
end
scrub_files() click to toggle source
# File lib/tagmv/filesystem.rb, line 26
def scrub_files
  files.select do |file|
    path = File.expand_path(file)
    if File.exist?(path)
      path
    else
      puts "tmv: rename #{file} to #{target_dir}/#{File.basename(file)}: #{Errno::ENOENT.exception}"
      false
    end
  end
end
scrub_tags(tags) click to toggle source
# File lib/tagmv/filesystem.rb, line 20
def scrub_tags(tags)
  # only keep legit file characters & remove trailing periods, remove duplicates after
  bad_chars =  /^[\-]|[^0-9A-Za-z\.\-\_]|[\.]+$/
  tags.map {|t| t.gsub(bad_chars, '') }.uniq
end
tag_dirs() click to toggle source
# File lib/tagmv/filesystem.rb, line 44
def tag_dirs
  tags_in_order.map {|x| x.gsub(/$/, '-') }
end
tags_in_order() click to toggle source
# File lib/tagmv/filesystem.rb, line 38
def tags_in_order
  return tags unless reorder

  (top_level_tags | tag_order) & tags
end
target_dir() click to toggle source
# File lib/tagmv/filesystem.rb, line 48
def target_dir
  File.join(Filesystem.root, *tag_dirs)
end
transfer() click to toggle source
# File lib/tagmv/filesystem.rb, line 67
def transfer
  prepare_dir && move_files
end

Private Instance Methods

options() click to toggle source
# File lib/tagmv/filesystem.rb, line 72
def options
  if @dry_run
    {noop: true, verbose: true}
  else
    {}
  end
end