rbrainz 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +13 -1
- data/LICENSE +1 -1
- data/README +2 -2
- data/Rakefile +2 -2
- data/TODO +9 -2
- data/doc/README.rdoc +5 -5
- data/examples/getartist.rb +3 -2
- data/examples/getlabel.rb +3 -2
- data/examples/getrelease.rb +5 -2
- data/examples/getreleasegroup.rb +53 -0
- data/examples/gettrack.rb +3 -2
- data/examples/getuser.rb +2 -1
- data/examples/rate.rb +44 -0
- data/examples/searchartists.rb +3 -2
- data/examples/searchcdstubs.rb +41 -0
- data/examples/searchlabels.rb +3 -2
- data/examples/searchreleasegroups.rb +36 -0
- data/examples/searchreleases.rb +6 -4
- data/examples/searchtracks.rb +3 -2
- data/examples/submit_isrcs.rb +52 -0
- data/examples/tag.rb +3 -2
- data/lib/rbrainz.rb +2 -1
- data/lib/rbrainz/core_ext.rb +2 -1
- data/lib/rbrainz/core_ext/mbid.rb +2 -1
- data/lib/rbrainz/core_ext/net_http_digest.rb +3 -2
- data/lib/rbrainz/core_ext/range.rb +3 -2
- data/lib/rbrainz/core_ext/range/equality.rb +2 -1
- data/lib/rbrainz/data/countrynames.rb +6 -3
- data/lib/rbrainz/data/languagenames.rb +3 -2
- data/lib/rbrainz/data/releasetypenames.rb +3 -2
- data/lib/rbrainz/data/scriptnames.rb +3 -2
- data/lib/rbrainz/model.rb +3 -2
- data/lib/rbrainz/model/alias.rb +3 -2
- data/lib/rbrainz/model/artist.rb +11 -3
- data/lib/rbrainz/model/collection.rb +3 -2
- data/lib/rbrainz/model/default_factory.rb +18 -6
- data/lib/rbrainz/model/disc.rb +3 -2
- data/lib/rbrainz/model/entity.rb +2 -102
- data/lib/rbrainz/model/incomplete_date.rb +3 -2
- data/lib/rbrainz/model/individual.rb +11 -2
- data/lib/rbrainz/model/isrc.rb +100 -0
- data/lib/rbrainz/model/label.rb +5 -2
- data/lib/rbrainz/model/mbid.rb +28 -9
- data/lib/rbrainz/model/rateable.rb +34 -0
- data/lib/rbrainz/model/rating.rb +56 -0
- data/lib/rbrainz/model/relateable.rb +118 -0
- data/lib/rbrainz/model/relation.rb +2 -1
- data/lib/rbrainz/model/release.rb +17 -3
- data/lib/rbrainz/model/release_event.rb +3 -2
- data/lib/rbrainz/model/release_group.rb +97 -0
- data/lib/rbrainz/model/scored_collection.rb +3 -2
- data/lib/rbrainz/model/tag.rb +5 -4
- data/lib/rbrainz/model/taggable.rb +27 -0
- data/lib/rbrainz/model/track.rb +15 -2
- data/lib/rbrainz/model/user.rb +3 -2
- data/lib/rbrainz/utils.rb +2 -1
- data/lib/rbrainz/utils/data.rb +3 -2
- data/lib/rbrainz/utils/helper.rb +8 -2
- data/lib/rbrainz/version.rb +3 -2
- data/lib/rbrainz/webservice.rb +12 -7
- data/lib/rbrainz/webservice/filter.rb +53 -4
- data/lib/rbrainz/webservice/includes.rb +72 -11
- data/lib/rbrainz/webservice/mbxml.rb +129 -67
- data/lib/rbrainz/webservice/query.rb +156 -16
- data/lib/rbrainz/webservice/webservice.rb +104 -116
- data/test/lib/mock_webservice.rb +9 -2
- data/test/lib/test_entity.rb +2 -97
- data/test/lib/test_factory.rb +9 -1
- data/test/lib/test_rateable.rb +31 -0
- data/test/lib/test_relateable.rb +103 -0
- data/test/lib/test_taggable.rb +36 -0
- data/test/lib/testing_helper.rb +17 -2
- data/test/test-data/invalid/artist/ratings_1.xml +6 -0
- data/test/test-data/invalid/artist/ratings_2.xml +6 -0
- data/test/test-data/valid/artist/Tchaikovsky-2.xml +6 -0
- data/test/test-data/valid/label/Atlantic_Records_3.xml +6 -0
- data/test/test-data/valid/release-group/The_Cure_1.xml +36 -0
- data/test/test-data/valid/release/Highway_61_Revisited_2.xml +6 -0
- data/test/test-data/valid/track/Silent_All_These_Years_4.xml +3 -0
- data/test/test-data/valid/track/Silent_All_These_Years_6.xml +8 -0
- data/test/test_alias.rb +2 -1
- data/test/test_artist.rb +24 -2
- data/test/test_artist_filter.rb +2 -1
- data/test/test_artist_includes.rb +13 -3
- data/test/test_collection.rb +3 -2
- data/test/test_default_factory.rb +8 -1
- data/test/test_disc.rb +2 -1
- data/test/test_incomplete_date.rb +2 -1
- data/test/test_isrc.rb +87 -0
- data/test/test_label.rb +8 -1
- data/test/test_label_filter.rb +2 -1
- data/test/test_label_includes.rb +10 -3
- data/test/test_mbid.rb +2 -1
- data/test/test_mbxml.rb +93 -2
- data/test/test_query.rb +68 -5
- data/test/test_range_equality.rb +2 -1
- data/test/test_rating.rb +46 -0
- data/test/test_relation.rb +2 -1
- data/test/test_release.rb +37 -2
- data/test/test_release_event.rb +2 -1
- data/test/test_release_filter.rb +15 -2
- data/test/test_release_group.rb +104 -0
- data/test/test_release_group_filter.rb +61 -0
- data/test/test_release_group_includes.rb +46 -0
- data/test/test_release_includes.rb +16 -3
- data/test/test_scored_collection.rb +3 -2
- data/test/test_tag.rb +2 -1
- data/test/test_track.rb +28 -1
- data/test/test_track_filter.rb +2 -1
- data/test/test_track_includes.rb +13 -3
- data/test/test_utils.rb +2 -1
- data/test/test_webservice.rb +11 -1
- metadata +38 -20
- data/debian/changelog +0 -11
- data/debian/compat +0 -1
- data/debian/control +0 -13
- data/debian/copyright +0 -25
- data/debian/rules +0 -48
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: incomplete_date.rb 254 2009-05-13 20:04:36Z phw $
|
2
3
|
#
|
3
4
|
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
4
5
|
# Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
|
@@ -93,4 +94,4 @@ module MusicBrainz
|
|
93
94
|
end
|
94
95
|
|
95
96
|
end
|
96
|
-
end
|
97
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: individual.rb 254 2009-05-13 20:04:36Z phw $
|
2
3
|
#
|
3
4
|
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
4
5
|
# Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
|
@@ -6,7 +7,11 @@
|
|
6
7
|
# See LICENSE[file:../LICENSE.html] for permissions.
|
7
8
|
|
8
9
|
require 'rbrainz/model/entity'
|
10
|
+
require 'rbrainz/model/alias'
|
9
11
|
require 'rbrainz/model/incomplete_date'
|
12
|
+
require 'rbrainz/model/rateable'
|
13
|
+
require 'rbrainz/model/relateable'
|
14
|
+
require 'rbrainz/model/taggable'
|
10
15
|
|
11
16
|
module MusicBrainz
|
12
17
|
module Model
|
@@ -16,6 +21,10 @@ module MusicBrainz
|
|
16
21
|
# Aggregates the common attributes of artists and labels.
|
17
22
|
class Individual < Entity
|
18
23
|
|
24
|
+
include Rateable
|
25
|
+
include Relateable
|
26
|
+
include Taggable
|
27
|
+
|
19
28
|
# The name of the artist or label.
|
20
29
|
attr_accessor :name
|
21
30
|
|
@@ -100,4 +109,4 @@ module MusicBrainz
|
|
100
109
|
end
|
101
110
|
|
102
111
|
end
|
103
|
-
end
|
112
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: isrc.rb 263 2009-05-24 22:15:12Z phw $
|
3
|
+
#
|
4
|
+
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
5
|
+
# Copyright:: Copyright (c) 2009, Philipp Wolfer
|
6
|
+
# License:: RBrainz is free software distributed under a BSD style license.
|
7
|
+
# See LICENSE[file:../LICENSE.html] for permissions.
|
8
|
+
|
9
|
+
module MusicBrainz
|
10
|
+
module Model
|
11
|
+
|
12
|
+
# The format of a ISRC was wrong.
|
13
|
+
#
|
14
|
+
# See:: ISRC
|
15
|
+
class InvalidISRCError < Exception
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Represents an International Standard Recording Code (ISRC).
|
20
|
+
#
|
21
|
+
# The International Standard Recording Code or short ISRC is an identification
|
22
|
+
# system for audio and music video recordings. It is standarized by the IFPI
|
23
|
+
# in ISO 3901:2001 and used by IFPI members to assign unique identifiers to
|
24
|
+
# every distinct recording they release.
|
25
|
+
#
|
26
|
+
# See:: http://wiki.musicbrainz.org/ISRC
|
27
|
+
class ISRC
|
28
|
+
|
29
|
+
ISRC_PATTERN = /^([0-9A-Z]{2})-?([0-9A-Z]{3})-?([0-9A-Z]{2})-?([0-9A-Z]{5})$/i
|
30
|
+
|
31
|
+
# 2-letter country code according to the ISO 3166-1-Alpha-2 standard.
|
32
|
+
attr_reader :country
|
33
|
+
|
34
|
+
# The Registrant Code identifies the entity assigning the Designation Code in an ISRC.
|
35
|
+
attr_reader :registrant
|
36
|
+
|
37
|
+
# Year of reference (2 digits).
|
38
|
+
attr_reader :year
|
39
|
+
|
40
|
+
# Designation code (5 digits)
|
41
|
+
attr_reader :designation
|
42
|
+
|
43
|
+
# Tries to convert _obj_ into an ISRC object.
|
44
|
+
#
|
45
|
+
# If _obj_ is an ISRC it is returned. Otherwise a new ISRC object
|
46
|
+
# is created by converting _obj_ into a string and parsing it.
|
47
|
+
#
|
48
|
+
# Raises:: InvalidISRCError
|
49
|
+
def self.parse(obj)
|
50
|
+
if obj.is_a? ISRC
|
51
|
+
return obj
|
52
|
+
else
|
53
|
+
return ISRC.new(obj)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Create a new ISRC object from the given string.
|
58
|
+
#
|
59
|
+
# Raises:: InvalidISRCError
|
60
|
+
def initialize(isrc)
|
61
|
+
unless isrc.to_s =~ ISRC_PATTERN
|
62
|
+
raise InvalidISRCError
|
63
|
+
end
|
64
|
+
@country = $1.upcase
|
65
|
+
@registrant = $2.upcase
|
66
|
+
@year = $3.upcase
|
67
|
+
@designation = $4.upcase
|
68
|
+
end
|
69
|
+
|
70
|
+
# Convert this ISRC into a String.
|
71
|
+
# Will return the readable version of the ISRC with dashes added,
|
72
|
+
# e.g. FR-Z03-91-01231
|
73
|
+
def to_s
|
74
|
+
return [@country, @registrant, @year, @designation].join('-')
|
75
|
+
end
|
76
|
+
|
77
|
+
# Convert this ISRC into a String.
|
78
|
+
# Will return the 12 character representation of the ISRC without dashes,
|
79
|
+
# e.g. FRZ039101231
|
80
|
+
def to_str
|
81
|
+
return [@country, @registrant, @year, @designation].join
|
82
|
+
end
|
83
|
+
|
84
|
+
# Compares this ISRC with another one.
|
85
|
+
#
|
86
|
+
# If _other_ is not of the type ISRC an attempt is made to convert it into one.
|
87
|
+
# This may cause an InvalidISRCError to be raised. Please note that
|
88
|
+
# comparing an ISRC with a different type of object is usually
|
89
|
+
# not commutative.
|
90
|
+
#
|
91
|
+
# Raises:: InvalidISRCError
|
92
|
+
def eql?(other)
|
93
|
+
self.to_s == ISRC.parse(other).to_s
|
94
|
+
end
|
95
|
+
alias :== eql?
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
data/lib/rbrainz/model/label.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: label.rb 254 2009-05-13 20:04:36Z phw $
|
2
3
|
#
|
3
4
|
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
4
5
|
# Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
|
@@ -26,6 +27,8 @@ module MusicBrainz
|
|
26
27
|
# See:: http://musicbrainz.org/doc/Label.
|
27
28
|
class Label < Individual
|
28
29
|
|
30
|
+
include Taggable
|
31
|
+
|
29
32
|
# Used if the type of the label is unknown.
|
30
33
|
TYPE_UNKNOWN = NS_MMD_1 + 'Unknown'
|
31
34
|
# Companies mainly distributing other labels production,
|
@@ -62,4 +65,4 @@ module MusicBrainz
|
|
62
65
|
end
|
63
66
|
|
64
67
|
end
|
65
|
-
end
|
68
|
+
end
|
data/lib/rbrainz/model/mbid.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: mbid.rb 254 2009-05-13 20:04:36Z phw $
|
2
3
|
#
|
3
4
|
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
4
5
|
# Copyright:: Copyright (c) 2007, Nigel Graham, Philipp Wolfer
|
@@ -44,8 +45,8 @@ module MusicBrainz
|
|
44
45
|
# See:: http://musicbrainz.org/doc/MusicBrainzIdentifier
|
45
46
|
class MBID
|
46
47
|
|
47
|
-
# The entity type (<tt>:artist</tt>, <tt>:label</tt>, <tt>:
|
48
|
-
# <tt>:track</tt>) this MBID references.
|
48
|
+
# The entity type (<tt>:artist</tt>, <tt>:label</tt>, <tt>:release_group</tt>,
|
49
|
+
# <tt>:release</tt>, <tt>:track</tt>) this MBID references.
|
49
50
|
attr_reader :entity
|
50
51
|
|
51
52
|
# The UUID of the referenced entity.
|
@@ -53,7 +54,7 @@ module MusicBrainz
|
|
53
54
|
|
54
55
|
module PATTERN #:nodoc:
|
55
56
|
UUID = '([a-fA-F0-9]{8}(:?-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12})'
|
56
|
-
ENTITY_TYPE = '(artist|release|track|label)'
|
57
|
+
ENTITY_TYPE = '(artist|release-group|release|track|label)'
|
57
58
|
ENTITY_URI = "http://musicbrainz\\.org/#{ENTITY_TYPE}/#{UUID}"
|
58
59
|
end
|
59
60
|
|
@@ -74,6 +75,9 @@ module MusicBrainz
|
|
74
75
|
#
|
75
76
|
# See:: new
|
76
77
|
# See:: String#to_mbid, URI::HTTP#to_mbid
|
78
|
+
#
|
79
|
+
# Raises:: UnknownEntityError, EntityTypeNotMatchingError,
|
80
|
+
# InvalidMBIDError
|
77
81
|
def self.parse(str, entity_type=nil)
|
78
82
|
if str.respond_to? :to_mbid
|
79
83
|
str.to_mbid(entity_type)
|
@@ -86,7 +90,8 @@ module MusicBrainz
|
|
86
90
|
#
|
87
91
|
# _str_ can be either a complete identifier or just the UUID part of it.
|
88
92
|
# In the latter case the entity type (<tt>:artist</tt>, <tt>:label</tt>,
|
89
|
-
# <tt>:release</tt> or <tt>:track</tt>) has to be
|
93
|
+
# <tt>:release_group</tt>, <tt>:release</tt> or <tt>:track</tt>) has to be
|
94
|
+
# specified as well.
|
90
95
|
#
|
91
96
|
# Examples:
|
92
97
|
# require 'rbrainz'
|
@@ -98,13 +103,13 @@ module MusicBrainz
|
|
98
103
|
# InvalidMBIDError
|
99
104
|
def initialize(str, entity_type=nil)
|
100
105
|
str = str.to_s if str.respond_to? :to_s
|
101
|
-
|
106
|
+
unless is_valid_entity_type_or_nil(entity_type)
|
102
107
|
raise UnknownEntityError, entity_type
|
103
108
|
end
|
104
|
-
entity_type = entity_type
|
109
|
+
entity_type = entity_type_to_symbol(entity_type)
|
105
110
|
|
106
111
|
if str =~ ENTITY_URI_REGEXP
|
107
|
-
@entity = $1
|
112
|
+
@entity = entity_type_to_symbol($1)
|
108
113
|
@uuid = $2.downcase
|
109
114
|
unless entity_type.nil? || @entity == entity_type
|
110
115
|
raise EntityTypeNotMatchingError, "#{@entity}, #{entity_type}"
|
@@ -135,7 +140,7 @@ module MusicBrainz
|
|
135
140
|
|
136
141
|
# Returns the string representation of the MBID (that is the complete URI).
|
137
142
|
def to_s
|
138
|
-
ENTITY_URI % [entity
|
143
|
+
ENTITY_URI % [Utils.entity_type_to_string(entity), uuid]
|
139
144
|
end
|
140
145
|
|
141
146
|
# Compares this MBID with another one.
|
@@ -147,6 +152,20 @@ module MusicBrainz
|
|
147
152
|
self.entity == other.entity and self.uuid == other.uuid
|
148
153
|
end
|
149
154
|
|
155
|
+
private
|
156
|
+
|
157
|
+
def is_valid_entity_type_or_nil(entity_type)
|
158
|
+
return entity_type.nil? || Utils.entity_type_to_string(entity_type) =~ ENTITY_TYPE_REGEXP
|
159
|
+
end
|
160
|
+
|
161
|
+
def entity_type_to_symbol(entity_type)
|
162
|
+
unless entity_type.respond_to? :to_sym
|
163
|
+
return entity_type
|
164
|
+
end
|
165
|
+
entity_type = entity_type.to_sym
|
166
|
+
return entity_type.to_s.sub('-', '_').to_sym
|
167
|
+
end
|
168
|
+
|
150
169
|
end
|
151
170
|
|
152
171
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: rateable.rb 266 2009-05-24 22:15:22Z phw $
|
3
|
+
#
|
4
|
+
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
5
|
+
# Copyright:: Copyright (c) 2009, Philipp Wolfer
|
6
|
+
# License:: RBrainz is free software distributed under a BSD style license.
|
7
|
+
# See LICENSE[file:../LICENSE.html] for permissions.
|
8
|
+
|
9
|
+
require 'rbrainz/model/rating'
|
10
|
+
|
11
|
+
module MusicBrainz
|
12
|
+
module Model
|
13
|
+
|
14
|
+
# Mixin module to add rating capabilities to Entity classes.
|
15
|
+
#
|
16
|
+
# see:: Rating
|
17
|
+
module Rateable
|
18
|
+
|
19
|
+
# The community Rating from MusicBrainz.
|
20
|
+
def rating
|
21
|
+
@rating ||= Rating.new
|
22
|
+
end
|
23
|
+
attr_writer :rating
|
24
|
+
|
25
|
+
# The user Rating from MusicBrainz.
|
26
|
+
def user_rating
|
27
|
+
@user_rating ||= Rating.new
|
28
|
+
end
|
29
|
+
attr_writer :user_rating
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: rating.rb 273 2009-05-24 22:29:04Z phw $
|
3
|
+
#
|
4
|
+
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
5
|
+
# Copyright:: Copyright (c) 2008 Philipp Wolfer
|
6
|
+
# License:: RBrainz is free software distributed under a BSD style license.
|
7
|
+
# See LICENSE[file:../LICENSE.html] for permissions.
|
8
|
+
|
9
|
+
module MusicBrainz
|
10
|
+
module Model
|
11
|
+
|
12
|
+
#
|
13
|
+
# Represents a rating of an entity.
|
14
|
+
#
|
15
|
+
# MusicBrainz allows it's users to rate artists, labels, releases and tracks.
|
16
|
+
# Each user rating will be a Float between 1 and 5 or nil, where nil means no rating.
|
17
|
+
# The system will aggregate user ratings to compute the average community rating.
|
18
|
+
#
|
19
|
+
# See:: http://wiki.musicbrainz.org/RatingSystem
|
20
|
+
class Rating
|
21
|
+
# The rating (an Float between 1 and 5).
|
22
|
+
# If :count is greater than 1 this will be the average rating.
|
23
|
+
attr_accessor :value
|
24
|
+
|
25
|
+
# The rating count indicating how often entity was rated.
|
26
|
+
attr_accessor :count
|
27
|
+
|
28
|
+
def initialize(value=nil, count=nil)
|
29
|
+
@value = value.nil? ? nil : value.to_f
|
30
|
+
@count = count.nil? ? nil : count.to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
# Convert this rating into a String. Will return rating.
|
34
|
+
def to_s
|
35
|
+
return value.to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
# Convert this rating into an Integer. Will return rating.
|
39
|
+
def to_i
|
40
|
+
return value.to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
# Convert this rating into a Float. Will return rating.
|
44
|
+
def to_f
|
45
|
+
return value.to_f
|
46
|
+
end
|
47
|
+
|
48
|
+
# Tests if this rating is empty
|
49
|
+
def empty?
|
50
|
+
return value.nil?
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# $Id: relateable.rb 266 2009-05-24 22:15:22Z phw $
|
3
|
+
#
|
4
|
+
# Author:: Philipp Wolfer (mailto:phw@rubyforge.org)
|
5
|
+
# Copyright:: Copyright (c) 2009, Philipp Wolfer
|
6
|
+
# License:: RBrainz is free software distributed under a BSD style license.
|
7
|
+
# See LICENSE[file:../LICENSE.html] for permissions.
|
8
|
+
|
9
|
+
require 'rbrainz/model/relation'
|
10
|
+
require 'rbrainz/model/collection'
|
11
|
+
|
12
|
+
module MusicBrainz
|
13
|
+
module Model
|
14
|
+
|
15
|
+
# Mixin module to add add advanced relationship capabilities to Entity classes.
|
16
|
+
#
|
17
|
+
# see:: Relation
|
18
|
+
module Relateable
|
19
|
+
|
20
|
+
#
|
21
|
+
# Adds a Relation.
|
22
|
+
#
|
23
|
+
# This method adds _relation_ to the list of relations. The
|
24
|
+
# given relation has to be initialized, at least the target
|
25
|
+
# type has to be set.
|
26
|
+
#
|
27
|
+
def add_relation(relation)
|
28
|
+
relations[relation.target_type] << relation
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Returns a list of Relation objects.
|
33
|
+
#
|
34
|
+
# If _target_type_ is given, only relations of that target
|
35
|
+
# type are returned. For MusicBrainz, the following target
|
36
|
+
# types are defined:
|
37
|
+
# - Relation::TO_ARTIST
|
38
|
+
# - Relation::TO_RELEASE
|
39
|
+
# - Relation::TO_TRACK
|
40
|
+
# - Relation::TO_URL
|
41
|
+
#
|
42
|
+
# If _target_type_ is Relation::TO_ARTIST, for example,
|
43
|
+
# this method returns all relations between this Entity and
|
44
|
+
# artists.
|
45
|
+
#
|
46
|
+
# You may use the _relation_type_ parameter to further restrict
|
47
|
+
# the selection. If it is set, only relations with the given
|
48
|
+
# relation type are returned. The _required_attributes_ sequence
|
49
|
+
# lists attributes that have to be part of all returned relations.
|
50
|
+
#
|
51
|
+
# If _direction_ is set, only relations with the given reading
|
52
|
+
# direction are returned. You can use the Relation::DIR_FORWARD,
|
53
|
+
# Relation::DIR_BACKWARD, and Relation::DIR_BOTH constants
|
54
|
+
# for this.
|
55
|
+
#
|
56
|
+
def get_relations(options = {:target_type => nil, :relation_type => nil,
|
57
|
+
:required_attributes => [], :direction => nil})
|
58
|
+
Utils.check_options options,
|
59
|
+
:target_type, :relation_type, :required_attributes, :direction
|
60
|
+
|
61
|
+
target_type = Utils.add_namespace(options[:target_type], NS_REL_1)
|
62
|
+
relation_type = Utils.add_namespace(options[:relation_type], NS_REL_1)
|
63
|
+
required_attributes =
|
64
|
+
options[:required_attributes] ? options[:required_attributes] : []
|
65
|
+
direction = options[:direction]
|
66
|
+
|
67
|
+
# Select all relevant relations depending on the requested target type
|
68
|
+
if target_type
|
69
|
+
result = relations[target_type].to_a
|
70
|
+
else
|
71
|
+
result = relations.values.flatten
|
72
|
+
end
|
73
|
+
|
74
|
+
# Filter for direction
|
75
|
+
result = result.find_all { |r| r.direction == direction } if direction
|
76
|
+
|
77
|
+
# Filter for relation type
|
78
|
+
result = result.find_all{ |r| r.type == relation_type } if relation_type
|
79
|
+
|
80
|
+
# Filter for attributes
|
81
|
+
required = required_attributes.map{|a| Utils.add_namespace(a, NS_REL_1)}.to_set
|
82
|
+
result.find_all do |r|
|
83
|
+
required.subset?( r.attributes.to_set )
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
#
|
88
|
+
# Returns a list of target types available for this entity.
|
89
|
+
#
|
90
|
+
# Use this to find out to which types of targets this entity
|
91
|
+
# has relations. If the entity only has relations to tracks and
|
92
|
+
# artists, for example, then a list containg the strings
|
93
|
+
# Relation::TO_TRACK and Relation::TO_ARTIST is returned.
|
94
|
+
#
|
95
|
+
def relation_target_types
|
96
|
+
result = []
|
97
|
+
relations.each_pair {|type, relations|
|
98
|
+
result << type unless relations.empty?
|
99
|
+
}
|
100
|
+
return result
|
101
|
+
end
|
102
|
+
|
103
|
+
private #-----------------------------------------------------------------
|
104
|
+
|
105
|
+
def relations
|
106
|
+
@relations ||= {
|
107
|
+
Relation::TO_ARTIST => Collection.new,
|
108
|
+
Relation::TO_RELEASE => Collection.new,
|
109
|
+
Relation::TO_TRACK => Collection.new,
|
110
|
+
Relation::TO_LABEL => Collection.new,
|
111
|
+
Relation::TO_URL => Collection.new,
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
end
|