Class: Addic7ed::Filename

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

Constant Summary collapse

TVSHOW_REGEX =
/\A(?<showname>.*\w)[\[\. ]+S?(?<season>\d{1,2})[-\. ]?[EX]?(?<episode>\d{2})[\]\. ]+(?<tags>.*)-(?<group>\w*)(\.\w{3})?\z/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Filename

Returns a new instance of Filename.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/addic7ed/filename.rb', line 8

def initialize(filename)
  @filename = filename
  match = TVSHOW_REGEX.match basename
  if match
    @showname = match[:showname].gsub('.', ' ')
    @season   = match[:season].to_i
    @episode  = match[:episode].to_i
    @tags     = match[:tags].upcase.split(/[\. ]/)
    @group    = match[:group].upcase
  else
    raise InvalidFilename
  end
end

Instance Attribute Details

#episodeObject (readonly)

Returns the value of attribute episode.



6
7
8
# File 'lib/addic7ed/filename.rb', line 6

def episode
  @episode
end

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/addic7ed/filename.rb', line 6

def filename
  @filename
end

#groupObject (readonly)

Returns the value of attribute group.



6
7
8
# File 'lib/addic7ed/filename.rb', line 6

def group
  @group
end

#seasonObject (readonly)

Returns the value of attribute season.



6
7
8
# File 'lib/addic7ed/filename.rb', line 6

def season
  @season
end

#shownameObject (readonly)

Returns the value of attribute showname.



6
7
8
# File 'lib/addic7ed/filename.rb', line 6

def showname
  @showname
end

#tagsObject (readonly)

Returns the value of attribute tags.



6
7
8
# File 'lib/addic7ed/filename.rb', line 6

def tags
  @tags
end

Instance Method Details

#basenameObject

Lazy getters



35
36
37
# File 'lib/addic7ed/filename.rb', line 35

def basename
  @basename ||= File.basename(@filename)
end

#dirnameObject



39
40
41
# File 'lib/addic7ed/filename.rb', line 39

def dirname
  @dirname ||= File.dirname(@filename)
end

#encoded_shownameObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/addic7ed/filename.rb', line 22

def encoded_showname
  @showname.
    gsub(/ /, '_').
    gsub(/_US$/, '_(US)').
    gsub(/_US_/, '_(US)_').
    gsub(/_UK$/, '').
    gsub(/_UK_/, '_').
    gsub(/_\d{4}$/, '').
    gsub(/_\d{4}_/, '_')
end

#extnameObject



43
44
45
# File 'lib/addic7ed/filename.rb', line 43

def extname
  @extname ||= File.extname(@filename)
end

#inspectObject



51
52
53
54
55
56
57
58
# File 'lib/addic7ed/filename.rb', line 51

def inspect
"Guesses for #{@filename}:
  show:    #{@showname}
  season:  #{@season}
  episode: #{@episode}
  tags:    #{@tags}
  group:   #{@group}"
end

#to_sObject



47
48
49
# File 'lib/addic7ed/filename.rb', line 47

def to_s
  @filename
end