class FilterRename::Filename

Public Class Methods

has_writable_tags() click to toggle source
# File lib/filter_rename/filename.rb, line 5
def self.has_writable_tags
  false
end
new(fname, cfg) click to toggle source
# File lib/filter_rename/filename.rb, line 9
def initialize(fname, cfg)
  @@count ||= cfg.counter_start
  @@count += 1
  @cfg = cfg

  load_filename_data(fname)
end

Public Instance Methods

!=(dest) click to toggle source
# File lib/filter_rename/filename.rb, line 21
def !=(dest)
  full_filename != dest.full_filename
end
==(dest) click to toggle source
# File lib/filter_rename/filename.rb, line 17
def ==(dest)
  full_filename == dest.full_filename
end
calculate_hash(hash_type = :md5) click to toggle source
# File lib/filter_rename/filename.rb, line 74
def calculate_hash(hash_type = :md5)
  raise UnknownHashCode, hash_type unless [:sha1, :sha2, :md5].include?(hash_type.to_sym)
  klass = Object.const_get("Digest::#{hash_type.to_s.upcase}")
  klass.file(full_filename).to_s
end
diff(dest) click to toggle source
# File lib/filter_rename/filename.rb, line 80
def diff(dest)
  Differ.diff_by_word(dest.full_filename, full_filename).to_s
end
exists?() click to toggle source
# File lib/filter_rename/filename.rb, line 53
def exists?
  File.exists?(full_filename)
end
filename() click to toggle source
# File lib/filter_rename/filename.rb, line 25
def filename
  @name + @ext
end
full_filename() click to toggle source
# File lib/filter_rename/filename.rb, line 37
def full_filename
  File.join [full_path, filename]
end
full_path() click to toggle source
# File lib/filter_rename/filename.rb, line 29
def full_path
  if @folder.to_s.empty?
    @path
  else
    File.join [@path, @folder]
  end
end
get_string(target) click to toggle source
# File lib/filter_rename/filename.rb, line 45
def get_string(target)
  instance_variable_get '@' + target.to_s
end
has_target?(target) click to toggle source
# File lib/filter_rename/filename.rb, line 49
def has_target?(target)
  instance_variables.include?(('@' + target.to_s).to_sym)
end
pretty_size(size) click to toggle source
# File lib/filter_rename/filename.rb, line 84
def pretty_size(size)
  i = 0; size = size.to_i
  while ((size >= 1024) && (i < FILE_SIZES.length))
    size = size.to_f / 1024
    i += 1
  end
  size.round(2).to_s.gsub(/.0$/, '') + FILE_SIZES[i]
end
rename!(dest) click to toggle source
# File lib/filter_rename/filename.rb, line 57
def rename!(dest)
  old_data = {}

  if full_filename != dest.full_filename
    if full_path != dest.full_path
      FileUtils.mkdir_p(dest.full_path) unless Dir.exists? dest.full_path
    end
    unless File.exists?(dest.full_filename)
      FileUtils.mv full_filename, dest.full_filename
      old_data = { full_filename: full_filename, full_path: full_path, filename: filename }
      load_filename_data(dest.full_filename)
    end
  end

  old_data
end
set_string(target, str) click to toggle source
# File lib/filter_rename/filename.rb, line 41
def set_string(target, str)
  instance_variable_set ('@' + target.to_s), str
end
targets() click to toggle source
# File lib/filter_rename/filename.rb, line 93
def targets
  res = {:readonly => [], :writable => []}
  instance_variables.each do |v|
    next if v == :@cfg
    res[instance_variable_get(v).writable? ? :writable : :readonly] << v
  end

  res
end
values() click to toggle source
# File lib/filter_rename/filename.rb, line 103
def values
  res = {}
  instance_variables.each do |v|
    next if v == :@cfg
    res[v.to_s.delete('@').to_sym] = instance_variable_get(v)
  end
  res
end

Protected Instance Methods

metatag_to_var!(key, value, readonly = true) click to toggle source
# File lib/filter_rename/filename.rb, line 114
def metatag_to_var!(key, value, readonly = true)
  var_name = key.downcase.gsub(/[^a-z]/, '_').gsub(/_+/, '_')
  instance_variable_set('@' + var_name, value.to_s.gsub('/', '_'))
  instance_variable_get('@' + var_name).readonly! if readonly
end

Private Instance Methods

load_filename_data(fname) click to toggle source
# File lib/filter_rename/filename.rb, line 122
def load_filename_data(fname)
  @ext = File.extname(fname)
  @name = File.basename(fname, @ext)
  @path= File.dirname(File.expand_path(fname))
  @folder = File.basename(@path)
  @path = File.dirname(@path)

  # read only stuff
  @count = @@count.to_s.rjust(@cfg.counter_length.to_i, '0')
  @ctime = File.ctime(fname).strftime(@cfg.date_format)
  @mtime = File.mtime(fname).strftime(@cfg.date_format)
  @size = File.size(fname).to_s
  @pretty_size = pretty_size(@size)

  [@count, @ctime, @mtime, @size, @pretty_size].map(&:readonly!)

  metatag_to_var!('hash', calculate_hash(@cfg.hash_type), true) unless @cfg.hash_type == :none
end