Class: UnitF::Tag::AutoTags

Inherits:
Hash
  • Object
show all
Defined in:
lib/unitf/tag/auto_tags.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ AutoTags



4
5
6
7
8
9
10
11
12
# File 'lib/unitf/tag/auto_tags.rb', line 4

def initialize(file_path)
  super

  @file_path = ::File.realpath(file_path.to_s)
  @dirname = ::File.dirname(@file_path)

  merge!(from_path)
  merge!(from_file)
end

Instance Method Details

#auto_tag_pathObject



14
15
16
17
18
19
20
21
22
# File 'lib/unitf/tag/auto_tags.rb', line 14

def auto_tag_path
  return @auto_tag_path unless @auto_tag_path.nil?

  ["#{@dirname}/.autotag", "#{@dirname}/../.autotag"].each do |path|
    return @auto_tag_path = path if ::File.exist?(path)
  end

  nil
end

#from_fileObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/unitf/tag/auto_tags.rb', line 46

def from_file
  tags = {}
  return tags unless ::File.exist?(auto_tag_path)

  ::File.read(auto_tag_path).each_line do |line|
    line.chomp!
    tag, value = line.split(/\s*=\s*/)
    tags[tag.strip.to_sym] = value.strip
  end

  tags
rescue StandardError
  {}
end

#from_pathObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/unitf/tag/auto_tags.rb', line 24

def from_path
  tags = {}

  tags[:title] = ::File.basename(@file_path)
  tags[:track] = tags[:title].match(/^\s*\d+/).to_s.to_i

  # Specific formatting for dated radio
  if tags[:title].scan(/(\.|_|-)((\d\d\d\d)(\.|-)\d\d(\.|-)\d\d(\w*))\./).any?
    tags[:title] = ::Regexp.last_match[2]
    tags[:year] = ::Regexp.last_match[3].to_i
  else
    tags[:title].gsub!(/\.\w+$/, '')
    tags[:title].gsub!(/^\d*\s*(-|\.)*\s*/, '')
  end

  path_parts = @dirname.to_s.split('/')
  tags[:artist] = path_parts[-2]
  tags[:album] = path_parts[-1]

  tags
end