Class: OggAlbumTagger::OggFile
- Inherits:
-
TagContainer
- Object
- TagContainer
- OggAlbumTagger::OggFile
- 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
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.sorted_tags(tags) ⇒ Object
Sort the tag keys alphabetically, but put METADATA_BLOCK_PICTURE at the end.
Instance Method Summary collapse
-
#initialize(file) ⇒ OggFile
constructor
Initialize a TagContainer from an ogg file.
- #to_s ⇒ Object
-
#write(file) ⇒ Object
Write the tags in the specified file.
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
#path ⇒ Object
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.() a = .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_s ⇒ Object
61 62 63 64 65 |
# File 'lib/ogg_album_tagger/ogg_file.rb', line 61 def to_s OggFile.(@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| = ogg.tag # Remove old tags .field_list_map.keys.each { |t| .remove_field(t) } # Set new tags (Taglib will write them sorted) @hash.each do |tag, values| values.sort.each do |v| .add_field(tag, v, false) end end # Save everything ogg.save end rescue Exception raise OggAlbumTagger::ArgumentError, "#{file} cannot be written." end end |