Module: MusicBrainz::Utils

Defined in:
lib/rbrainz/utils/data.rb,
lib/rbrainz/utils/helper.rb

Overview

This module contains helper functions to make common tasks easier.

Class Method Summary collapse

Class Method Details

.add_namespace(str, namespace = Model::NS_MMD_1) ⇒ Object

Will return the given str extended by namespace. If str already includes the namespace or if str is empty it will be returned unchanged.



27
28
29
30
31
32
33
# File 'lib/rbrainz/utils/helper.rb', line 27

def add_namespace(str, namespace=Model::NS_MMD_1)
  unless str =~ /^#{namespace}/ or str.to_s.empty?
    return namespace + str.to_s
  else
    return str
  end
end

.check_options(options, *optdecl) ⇒ Object

Check an options hash for required options. Raises an ArgumentError if unknown options are present in the hash.

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
# File 'lib/rbrainz/utils/helper.rb', line 51

def check_options(options, *optdecl)   # :nodoc:
  h = options.dup
  optdecl.each do |name|
    h.delete name
  end
  raise ArgumentError, "no such option: #{h.keys.join(' ')}" unless h.empty?
end

.entity_type_to_string(entity_type) ⇒ Object

Converts an entity type constant symbol into the proper string representation



36
37
38
# File 'lib/rbrainz/utils/helper.rb', line 36

def entity_type_to_string(entity_type)
  return entity_type.to_s.sub('_', '-')
end

.entity_type_to_symbol(entity_type) ⇒ Object

Converts an entity type string into the proper symbol



41
42
43
44
45
46
47
# File 'lib/rbrainz/utils/helper.rb', line 41

def entity_type_to_symbol(entity_type)
  unless entity_type.respond_to? :to_sym
    return entity_type
  end
  entity_type = entity_type.to_sym
  return entity_type.to_s.sub('-', '_').to_sym
end

.get_country_name(id) ⇒ Object

Returns a country’s name based on an ISO-3166 country code.

The country table this function is based on has been modified for MusicBrainz purposes by using the extension mechanism defined in ISO-3166. All IDs are still valid ISO-3166 country codes, but some IDs have been added to include historic countries and some of the country names have been modified to make them better suited for display purposes.

If the country ID is not found, nil is returned. This may happen for example, when new countries are added to the MusicBrainz web service which aren’t known to this library yet.

See

#Data::COUNTRY_NAMES



34
35
36
# File 'lib/rbrainz/utils/data.rb', line 34

def self.get_country_name(id)
  return Data::COUNTRY_NAMES[id]
end

.get_language_name(id) ⇒ Object

Returns a language name based on an ISO-639-2/T code.

This function uses a subset of the ISO-639-2/T code table to map language IDs (terminologic, not bibliographic ones!) to names.

If the language ID is not found, nil is returned. This may happen for example, when new languages are added to the MusicBrainz web service which aren’t known to this library yet.

See

#Data::LANGUAGE_NAMES



48
49
50
# File 'lib/rbrainz/utils/data.rb', line 48

def self.get_language_name(id)
  return Data::LANGUAGE_NAMES[id]
end

.get_release_type_name(id) ⇒ Object

Returns the name of a release type URI.

If the release type is not found, nil is returned. This may happen for example, when new release types are added to the MusicBrainz web service which aren’t known to this library yet.

See

#Data::RELEASE_TYPE_NAMES

See

Model::Release



74
75
76
# File 'lib/rbrainz/utils/data.rb', line 74

def self.get_release_type_name(id)
  return Data::RELEASE_TYPE_NAMES[id]
end

.get_script_name(id) ⇒ Object

Returns a script name based on an ISO-15924 code.

This function uses a subset of the ISO-15924 code table to map script IDs to names.

If the script ID is not found, nil is returned. This may happen for example, when new scripts are added to the MusicBrainz web service which aren’t known to this library yet.

See

#Data::SCRIPT_NAMES



62
63
64
# File 'lib/rbrainz/utils/data.rb', line 62

def self.get_script_name(id)
  return Data::SCRIPT_NAMES[id]
end

.remove_namespace(str, namespace = Model::NS_MMD_1) ⇒ Object

Remove the given namespace from str. Will return str with the namespace removed. If the namespace was not present in str it will be returned unchanged.



17
18
19
20
21
22
23
# File 'lib/rbrainz/utils/helper.rb', line 17

def remove_namespace(str, namespace=Model::NS_MMD_1)
  if str =~ /^#{namespace}(.*)/
    return $1 
  else
    return str
  end
end