Class: FilterRename::Filename

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

Overview

Basic file attributes translated to tags to be replaced.

Direct Known Subclasses

AudioFilename, 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.



15
16
17
18
19
20
21
22
# File 'lib/filter_rename/filename.rb', line 15

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

  @original = fname
  load_filename_data(fname)
end

Instance Attribute Details

#original ⇒ Object (readonly)

Returns the value of attribute original.



9
10
11
# File 'lib/filter_rename/filename.rb', line 9

def original
  @original
end

Class Method Details

.writable_tags? ⇒ Boolean

Returns:



11
12
13
# File 'lib/filter_rename/filename.rb', line 11

def self.writable_tags?
  false
end

Instance Method Details

#!=(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

def ==(other)
  full_filename == other.full_filename
end

#calculate_hash(hash_type = :md5) ⇒ Object

Raises:



79
80
81
82
83
84
# File 'lib/filter_rename/filename.rb', line 79

def calculate_hash(hash_type = :md5)
  raise UnknownHashCode, hash_type unless %i[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:



115
116
117
# File 'lib/filter_rename/filename.rb', line 115

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

#diff(dest) ⇒ Object



86
87
88
# File 'lib/filter_rename/filename.rb', line 86

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

#exists? ⇒ Boolean

Returns:



60
61
62
# File 'lib/filter_rename/filename.rb', line 60

def exists?
  File.exist?(full_filename)
end

#filename ⇒ Object



32
33
34
# File 'lib/filter_rename/filename.rb', line 32

def filename
  @name + @ext
end

#full_filename ⇒ Object



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

def full_filename
  File.join [full_path, filename]
end

#full_path ⇒ Object



36
37
38
39
40
41
42
# File 'lib/filter_rename/filename.rb', line 36

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

#get_string(target) ⇒ Object



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

def get_string(target)
  instance_variable_get "@#{target}"
end

#pretty_size(size) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/filter_rename/filename.rb', line 90

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/filter_rename/filename.rb', line 64

def rename!(dest)
  old_data = {}

  if full_filename != dest.full_filename
    FileUtils.mkdir_p(dest.full_path) if full_path != dest.full_path && !(Dir.exist? dest.full_path)
    unless File.exist?(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



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

def set_string(target, str)
  instance_variable_set "@#{target}", str
end

#target?(target) ⇒ Boolean

Returns:



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

def target?(target)
  instance_variables.include?(:"@#{target}")
end

#targets ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/filter_rename/filename.rb', line 100

def targets
  res = { readonly: [], writable: [] }
  instance_variables.each do |v|
    next if v == :@cfg

    res[instance_variable_get(v).writable? ? :writable : :readonly] << v.to_s.delete("@").to_sym
  end

  res
end

#values ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/filter_rename/filename.rb', line 119

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:



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

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