Class: OggAlbumTagger::OggFile

Inherits:
TagContainer show all
Defined in:
lib/ogg_album_tagger/ogg_file.rb

Overview

Store the tags of an ogg track.

Each tag is associated to a Set of values.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TagContainer

#[], #add_values, #each, #first, #has_tag?, pp_tag, #rm_values, #set_values, #tags, valid_tag?

Constructor Details

#initialize(file) ⇒ OggFile

Initialize a TagContainer from an ogg file.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ogg_album_tagger/ogg_file.rb', line 16

def initialize(file)
    begin
        h = Hash.new

        TagLib::Ogg::Vorbis::File.open(file.to_s) do |ogg|
            ogg.tag.field_list_map.each do |tag, values|
                h[tag] = Set.new
                values.each do |value|
                    h[tag].add(value.strip)
                end
            end
        end

        super(h)
        @path = file
    rescue Exception => ex
        STDERR.puts ex
        raise OggAlbumTagger::ArgumentError, "#{file} does not seems to be a valid ogg file."
    end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/ogg_album_tagger/ogg_file.rb', line 13

def path
  @path
end

Class Method Details

.sorted_tags(tags) ⇒ Object

 Sort the tag keys alphabetically, but put METADATA_BLOCK_PICTURE at the end.



68
69
70
71
72
# File 'lib/ogg_album_tagger/ogg_file.rb', line 68

def self.sorted_tags(tags)
    a = tags.sort
    a.delete('METADATA_BLOCK_PICTURE') and a.push('METADATA_BLOCK_PICTURE')
    block_given? ? a.each { |v| yield v } : a
end

Instance Method Details

#to_sObject



61
62
63
64
65
# File 'lib/ogg_album_tagger/ogg_file.rb', line 61

def to_s
    OggFile.sorted_tags(@hash.keys).map do |tag|
        OggFile.pp_tag(@hash[tag])
    end.join "\n"
end

#write(file) ⇒ Object

Write the tags in the specified file.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ogg_album_tagger/ogg_file.rb', line 38

def write(file)
    begin
        TagLib::Ogg::Vorbis::File.open(file.to_s) do |ogg|
            tags = ogg.tag

            # Remove old tags
            tags.field_list_map.keys.each { |t| tags.remove_field(t) }

            # Set new tags (Taglib will write them sorted)
            @hash.each do |tag, values|
                values.sort.each do |v|
                    tags.add_field(tag, v, false)
                end
            end

            # Save everything
            ogg.save
        end
    rescue Exception
        raise OggAlbumTagger::ArgumentError, "#{file} cannot be written."
    end
end