Class: OggAlbumTagger::TagContainer
- Inherits:
-
Object
- Object
- OggAlbumTagger::TagContainer
- Defined in:
- lib/ogg_album_tagger/tag_container.rb
Overview
A TagContainer is basically a hashmap associating a name to a set of string values.
The keys of the hashmap (aka the tags) are case insensitive (they are stored upcase). A tag cannot be an empty string or containe the “=” or “~” characters. Tags with no values are automatically removed.
Direct Known Subclasses
Class Method Summary collapse
-
.pp_tag(values) ⇒ Object
Pretty print an array of values.
-
.valid_tag?(tag) ⇒ Boolean
Check if the tag is valid, otherwise raise an ArgumentError.
Instance Method Summary collapse
-
#[](tag) ⇒ Object
Returns a Set containing the values associated to the specified tag.
-
#add_values(tag, *values) ⇒ Object
Add some values to the specified tag.
-
#each ⇒ Object
Iterate through the available tags.
- #first(tag) ⇒ Object
-
#has_tag?(tag) ⇒ Boolean
Check if the specified tag is present in the container.
-
#initialize(tags = {}) ⇒ TagContainer
constructor
Allow to build a TagContainer using the following syntax:.
-
#rm_values(tag, *values) ⇒ Object
Remove some tags.
-
#set_values(tag, *values) ⇒ Object
Add some values to the specified tag.
-
#tags ⇒ Object
Returns the list of present tags.
Constructor Details
#initialize(tags = {}) ⇒ TagContainer
Allow to build a TagContainer using the following syntax:
t = TagContainer.new artist: "Alice", genre: %w{Pop Rock}, tracknumber: 1, ...
The values associated to a tag are automatically casted to a String. Single values are treated as an array of one value.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 18 def initialize = {} @hash = Hash.new .each { |tag, values| next if values.nil? values = [values] unless values.is_a?(Array) or values.is_a?(Set) next if values.empty? prepare_tag(tag.to_s.upcase) values.each { |value| @hash[tag.to_s.upcase].add(value.to_s) } } end |
Class Method Details
.pp_tag(values) ⇒ Object
Pretty print an array of values.
114 115 116 117 118 119 120 121 122 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 114 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 |
.valid_tag?(tag) ⇒ Boolean
Check if the tag is valid, otherwise raise an ArgumentError. Valid tags are composed of any character from “ ” to “}”, excluding “=” and “~”.
56 57 58 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 56 def self.valid_tag? tag raise ArgumentError, "Invalid tag." unless tag =~ /^[\x20-\x3C\x3E-\x7D]+$/ 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.
37 38 39 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 37 def [](tag) has_tag?(tag) ? @hash[tag.upcase] : Set.new.freeze end |
#add_values(tag, *values) ⇒ Object
Add some values to the specified tag.
80 81 82 83 84 85 86 87 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 80 def add_values(tag, *values) return if values.empty? prepare_tag tag @hash[tag.upcase].merge(values) self end |
#each ⇒ Object
Iterate through the available tags.
109 110 111 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 109 def each @hash.each { |tag, set| yield(tag, set) } end |
#first(tag) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 41 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.
50 51 52 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 50 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.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 90 def rm_values(tag, *values) TagContainer::valid_tag? tag if values.empty? @hash.delete(tag.upcase) else @hash[tag.upcase].subtract(values) @hash.delete(tag.upcase) if @hash[tag.upcase].empty? end self end |
#set_values(tag, *values) ⇒ Object
Add some values to the specified tag. Any previous values will be removed.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 68 def set_values(tag, *values) if values.empty? rm_values(tag) else prepare_tag tag @hash[tag.upcase].replace(values) end self end |
#tags ⇒ Object
Returns the list of present tags.
104 105 106 |
# File 'lib/ogg_album_tagger/tag_container.rb', line 104 def @hash.keys end |