Class: Vocalist::Nationality
- Inherits:
-
Object
- Object
- Vocalist::Nationality
- Defined in:
- lib/vocalist/nationality.rb
Overview
Class which resolves an artist to its nationality
Class Method Summary collapse
-
.__count_tag(tag, entry) ⇒ Object
Helper function to compute tag count and an maybe-nil entry.
Instance Method Summary collapse
- #add_mapping(tag, country) ⇒ Object
-
#by_scrobbler_tags(tags) ⇒ String
Classify an artist by its tags on Last.fm.
-
#by_scrobbler_tags_ex(tags) ⇒ Hash<String, Fixnum>
Classify an artist by its tags on Last.fm.
-
#has_tag?(tag) ⇒ Boolean
Do we already know about a tag?.
-
#initialize ⇒ Nationality
constructor
Create a new instance.
-
#load_file(file) ⇒ void
Load a given file.
-
#load_files(files) ⇒ void
Loads the specified Files.
Constructor Details
#initialize ⇒ Nationality
Create a new instance. There is no data loaded at this point, everything needs to be loaded separate.
9 10 11 |
# File 'lib/vocalist/nationality.rb', line 9 def initialize() @tag_map = {} end |
Class Method Details
.__count_tag(tag, entry) ⇒ Object
Helper function to compute tag count and an maybe-nil entry
89 90 91 92 |
# File 'lib/vocalist/nationality.rb', line 89 def self.__count_tag(tag, entry) count = (entry.nil? ? 0 : entry) count += [tag.count, 1].max end |
Instance Method Details
#add_mapping(tag, country) ⇒ Object
40 41 42 |
# File 'lib/vocalist/nationality.rb', line 40 def add_mapping(tag, country) @tag_map[tag.downcase] = country end |
#by_scrobbler_tags(tags) ⇒ String
Classify an artist by its tags on Last.fm.
69 70 71 72 |
# File 'lib/vocalist/nationality.rb', line 69 def () result = ().to_a.each { |s| s.reverse! }.max result[1] unless result.nil? end |
#by_scrobbler_tags_ex(tags) ⇒ Hash<String, Fixnum>
Classify an artist by its tags on Last.fm.
Extended version: Returns all possible countries.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/vocalist/nationality.rb', line 51 def () countries = {} .each do |tag| # by default we only handle tags case-insensitive country = @tag_map[tag.name.downcase] if !country.nil? then countries[country] = Nationality::__count_tag(tag, countries[country]) end end countries end |
#has_tag?(tag) ⇒ Boolean
Do we already know about a tag?
80 81 82 |
# File 'lib/vocalist/nationality.rb', line 80 def has_tag?(tag) @tag_map.has_key?(tag.downcase) end |
#load_file(file) ⇒ void
This method returns an undefined value.
Load a given file
28 29 30 31 32 33 34 |
# File 'lib/vocalist/nationality.rb', line 28 def load_file(file) file_data = YAML.load(File.read(file)) country = file_data[:country] file_data[:tags].each do |tag| @tag_map[tag.downcase] = country end end |
#load_files(files) ⇒ void
This method returns an undefined value.
Loads the specified Files
18 19 20 21 22 |
# File 'lib/vocalist/nationality.rb', line 18 def load_files(files) Dir.glob(files).each do |file| load_file(file) end end |