Class: OggAlbumTagger::TagContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/ogg_album_tagger/tag_container.rb

Overview

Store the tags of an ogg track.

Each tag is associated to a Set of values.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ TagContainer

Initialize a TagContainer from an ogg file.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ogg_album_tagger/tag_container.rb', line 13

def initialize(file)
  @hash = Hash.new 

  begin
    TagLib::Ogg::Vorbis::File.open(file.to_s) do |ogg|
      ogg.tag.field_list_map.each do |tag, values|
        prepare_tag(tag.upcase)

        values.each do |value|
          @hash[tag.upcase].add(value.strip)
        end
      end
    end
  rescue Exception => ex
    #STDERR.puts ex
    raise OggAlbumTagger::ArgumentError, "#{file} does not seems to be a valid ogg file."
  end
end

Class Method Details

.pp_tag(values) ⇒ Object

Pretty print an array of values.



129
130
131
132
133
134
135
136
137
# File 'lib/ogg_album_tagger/tag_container.rb', line 129

def self.pp_tag values
  values_str = values.map { |v| v.to_s.length > 64 ? (v.to_s.slice(0, 64) + '...') : v }

  case values.length
  when 0 then '- (empty)'
  when 1 then values_str[0]
  else sprintf("(%d) [%s]", values.length, values_str.join(', '))
  end
end

.sorted_tags(tags) ⇒ Object

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



122
123
124
125
126
# File 'lib/ogg_album_tagger/tag_container.rb', line 122

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

#[](tag) ⇒ Object

Returns a Set containing the values associated to the specified tag.

If the tag does not exists, returns an empty Set. Do not use the returned Set to add new tags, use the methods provided by the class.



61
62
63
# File 'lib/ogg_album_tagger/tag_container.rb', line 61

def [](tag)
  has_tag?(tag) ? @hash[tag.upcase] : Set.new.freeze
end

#add_values(tag, *values) ⇒ Object

Add some values to the specified tag.



91
92
93
94
# File 'lib/ogg_album_tagger/tag_container.rb', line 91

def add_values(tag, *values)
  prepare_tag tag
  @hash[tag.upcase].merge(values)
end

#eachObject

 Iterate through the available tags.



111
112
113
# File 'lib/ogg_album_tagger/tag_container.rb', line 111

def each
  @hash.each { |tag, set| yield(tag, set) }
end

#first(tag) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/ogg_album_tagger/tag_container.rb', line 65

def first(tag)
  if has_tag?(tag)
    return @hash[tag.upcase].first
  else
    raise IndexError, "Tag \"#{tag}\" does not exists."
  end
end

#has_tag?(tag) ⇒ Boolean

Check if the specified tag is present in the container.

Returns:

  • (Boolean)


74
75
76
# File 'lib/ogg_album_tagger/tag_container.rb', line 74

def has_tag? tag
  @hash.has_key?(tag.upcase)
end

#rm_values(tag, *values) ⇒ Object

Remove some tags. If no value is specified, the specified tag is removed.



97
98
99
100
101
102
103
# File 'lib/ogg_album_tagger/tag_container.rb', line 97

def rm_values(tag, *values)
  if values.empty? then @hash.delete(tag.upcase)
  else
    @hash[tag.upcase].subtract(values)
    @hash.delete(tag.upcase) if @hash[tag.upcase].empty?
  end
end

#set_values(tag, *values) ⇒ Object

Add some values to the specified tag. Any previous values will be removed.



85
86
87
88
# File 'lib/ogg_album_tagger/tag_container.rb', line 85

def set_values(tag, *values)
  prepare_tag tag
  @hash[tag.upcase].replace(values)
end

#tagsObject

Returns the list of present tags.



106
107
108
# File 'lib/ogg_album_tagger/tag_container.rb', line 106

def tags
  @hash.keys
end

#to_sObject



115
116
117
118
119
# File 'lib/ogg_album_tagger/tag_container.rb', line 115

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

#write(file) ⇒ Object

Write the tags in the specified file.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ogg_album_tagger/tag_container.rb', line 33

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 => ex
    #STDERR.puts ex
    raise OggAlbumTagger::ArgumentError, "#{file} cannot be written."
  end
end