Class: Flacky::FlacTagger

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FlacTagger

Returns a new instance of FlacTagger.



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

def initialize(file)
  @taglib = TagLib::FLAC::File.new(file)
  @tag = @taglib.xiph_comment
end

Class Method Details

.update(file, &block) ⇒ Object



8
9
10
11
12
# File 'lib/flacky/flac_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/flac_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



40
41
42
# File 'lib/flacky/flac_tagger.rb', line 40

def cleanup
  @taglib.close
end

#find(attr) ⇒ Object



31
32
33
34
# File 'lib/flacky/flac_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
# File 'lib/flacky/flac_tagger.rb', line 36

def tag(name, value)
  @tag.add_field(name.to_s.upcase, value, true)
end

#update!Object



44
45
46
47
# File 'lib/flacky/flac_tagger.rb', line 44

def update!
  @taglib.save
  cleanup
end