Class: FilterRename::Filename

Inherits:
Object
  • Object
show all
Defined in:
lib/filter_rename/filename.rb

Direct Known Subclasses

ImageFilename, Mp3Filename, PdfFilename

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fname, cfg) ⇒ Filename

Returns a new instance of Filename.



11
12
13
14
15
16
17
18
# File 'lib/filter_rename/filename.rb', line 11

def initialize(fname, cfg)
  @@count ||= cfg.counter_start
  @@count += 1
  @cfg = cfg

  @original = fname
  load_filename_data(fname)
end

Instance Attribute Details

#originalObject (readonly)

Returns the value of attribute original.



5
6
7
# File 'lib/filter_rename/filename.rb', line 5

def original
  @original
end

Class Method Details

.has_writable_tagsObject



7
8
9
# File 'lib/filter_rename/filename.rb', line 7

def self.has_writable_tags
  false
end

Instance Method Details

#!=(dest) ⇒ Object



24
25
26
# File 'lib/filter_rename/filename.rb', line 24

def !=(dest)
  full_filename != dest.full_filename
end

#==(dest) ⇒ Object



20
21
22
# File 'lib/filter_rename/filename.rb', line 20

def ==(dest)
  full_filename == dest.full_filename
end

#calculate_hash(hash_type = :md5) ⇒ Object

Raises:



77
78
79
80
81
# File 'lib/filter_rename/filename.rb', line 77

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

#custom?(tag) ⇒ Boolean

Returns:



110
111
112
# File 'lib/filter_rename/filename.rb', line 110

def custom?(tag)
  instance_variable_get("@#{tag}".to_sym).custom?
end

#diff(dest) ⇒ Object



83
84
85
# File 'lib/filter_rename/filename.rb', line 83

def diff(dest)
  Differ.diff_by_word(dest.full_filename, full_filename).to_s
end

#exists?Boolean

Returns:



56
57
58
# File 'lib/filter_rename/filename.rb', line 56

def exists?
  File.exists?(full_filename)
end

#filenameObject



28
29
30
# File 'lib/filter_rename/filename.rb', line 28

def filename
  @name + @ext
end

#full_filenameObject



40
41
42
# File 'lib/filter_rename/filename.rb', line 40

def full_filename
  File.join [full_path, filename]
end

#full_pathObject



32
33
34
35
36
37
38
# File 'lib/filter_rename/filename.rb', line 32

def full_path
  if @folder.to_s.empty?
    @path
  else
    File.join [@path, @folder]
  end
end

#get_string(target) ⇒ Object



48
49
50
# File 'lib/filter_rename/filename.rb', line 48

def get_string(target)
  instance_variable_get '@' + target.to_s
end

#has_target?(target) ⇒ Boolean

Returns:



52
53
54
# File 'lib/filter_rename/filename.rb', line 52

def has_target?(target)
  instance_variables.include?(('@' + target.to_s).to_sym)
end

#pretty_size(size) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/filter_rename/filename.rb', line 87

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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/filter_rename/filename.rb', line 60

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) ⇒ Object



44
45
46
# File 'lib/filter_rename/filename.rb', line 44

def set_string(target, str)
  instance_variable_set ('@' + target.to_s), str
end

#targetsObject



96
97
98
99
100
101
102
103
104
# File 'lib/filter_rename/filename.rb', line 96

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

#valuesObject



114
115
116
117
118
119
120
121
# File 'lib/filter_rename/filename.rb', line 114

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

#writable?(tag) ⇒ Boolean

Returns:



106
107
108
# File 'lib/filter_rename/filename.rb', line 106

def writable?(tag)
  instance_variable_get("@#{tag}".to_sym).writable?
end