Class: Ehbrs::Videos::Series::Rename::File

Inherits:
LineResult show all
Includes:
BasenameParser
Defined in:
lib/ehbrs/videos/series/rename/file.rb,
lib/ehbrs/videos/series/rename/file/options.rb,
lib/ehbrs/videos/series/rename/file/basename_parser.rb

Defined Under Namespace

Modules: BasenameParser Classes: Options

Constant Summary

Constants included from BasenameParser

BasenameParser::FORMATS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BasenameParser

padding, #parse_uncached

Methods inherited from LineResult

#show

Constructor Details

#initialize(file, options) ⇒ File

Returns a new instance of File.



17
18
19
20
21
# File 'lib/ehbrs/videos/series/rename/file.rb', line 17

def initialize(file, options)
  @file = file
  @options = options
  @options = ::OpenStruct.new(options) if options.is_a?(::Hash)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



15
16
17
# File 'lib/ehbrs/videos/series/rename/file.rb', line 15

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/ehbrs/videos/series/rename/file.rb', line 15

def options
  @options
end

Instance Method Details

#dirnameObject



43
44
45
46
47
48
49
50
# File 'lib/ehbrs/videos/series/rename/file.rb', line 43

def dirname
  d = @file
  while d != '/'
    d = ::File.dirname(d)
    return ::File.expand_path(d) + '/' unless ::File.basename(d).starts_with?('_')
  end
  raise "series_dirname not found for #{@file}"
end

#episodeObject



37
38
39
40
41
# File 'lib/ehbrs/videos/series/rename/file.rb', line 37

def episode
  return '??' if parse.blank?

  parse.fetch(:e)
end

#line_outObject



52
53
54
55
56
57
58
# File 'lib/ehbrs/videos/series/rename/file.rb', line 52

def line_out
  "#{episode.green}: " + if new_name == current_name
                           current_name.light_black
                         else
                           "#{new_name} <= #{current_name}"
                         end
end

#new_nameObject



60
61
62
63
64
# File 'lib/ehbrs/videos/series/rename/file.rb', line 60

def new_name
  return "#{kernel}-s#{parse[:s]}e#{new_name_episodes}#{extension}" if parse.present?

  current_name
end

#renameObject



23
24
25
26
27
28
29
# File 'lib/ehbrs/videos/series/rename/file.rb', line 23

def rename
  target = ::File.expand_path(new_name, dirname)
  return if ::File.exist?(target)

  infov 'Renaming', @file
  FileUtils.mv(@file, target)
end

#seasonObject



31
32
33
34
35
# File 'lib/ehbrs/videos/series/rename/file.rb', line 31

def season
  return '??' if parse.blank?

  parse.fetch(:s)
end