Class: Flacky::Mp3Tagger

Inherits:
Object
  • Object
show all
Defined in:
lib/flacky/mp3_tagger.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Mp3Tagger

Returns a new instance of Mp3Tagger.



14
15
16
17
# File 'lib/flacky/mp3_tagger.rb', line 14

def initialize(file)
  @taglib = TagLib::MPEG::File.new(file)
  @tag = @taglib.id3v2_tag
end

Class Method Details

.update(file, &block) ⇒ Object



8
9
10
11
12
# File 'lib/flacky/mp3_tagger.rb', line 8

def self.update(file, &block)
  instance = self.new(file)
  instance.instance_eval(&block)
  instance.update!
end

Instance Method Details

#[](attr) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flacky/mp3_tagger.rb', line 19

def [](attr)
  result = @tag.field_list_map[attr]

  if result.nil?
    nil
  elsif result.is_a?(Array) && result.size == 1
    result.first
  else
    result
  end
end

#cleanupObject



48
49
50
# File 'lib/flacky/mp3_tagger.rb', line 48

def cleanup
  @taglib.close
end

#find(attr) ⇒ Object



31
32
33
34
# File 'lib/flacky/mp3_tagger.rb', line 31

def find(attr)
  key = @tag.field_list_map.keys.find { |key| key =~ /#{attr}/i }
  key ? self[key] : nil
end

#tag(name, value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flacky/mp3_tagger.rb', line 36

def tag(name, value)
  if TAGS.include?(name.to_sym)
    @tag.public_send("#{name}=", value)
  elsif name =~ /track/i
    @tag.add_frame(new_text_frame("TRCK", value))
  elsif name =~ /disc/i
    @tag.add_frame(new_text_frame("TPOS", value))
  elsif name =~ /comment/i
    @tag.add_frame(new_comment_frame(value))
  end
end

#update!Object



52
53
54
55
# File 'lib/flacky/mp3_tagger.rb', line 52

def update!
  @taglib.save
  cleanup
end