Class: MusicBrainz::Model::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/rbrainz/model/relation.rb

Overview

Represents a relation between two Entities.

There may be an arbitrary number of relations between all first class objects in MusicBrainz. The Relation itself has multiple attributes, which may or may not be used for a given relation type.

Note that a Relation object only contains the target but not the source end of the relation.

TODO

Add some examples.

Constant Summary collapse

DIR_BACKWARD =

Relation reading direction is from target to source.

:backward
DIR_FORWARD =

Relation reading direction is from source to target.

:forward
DIR_BOTH =

Relation reading direction doesn’t matter.

:both
TO_ARTIST =

Identifies relations linking to an artist.

NS_REL_1 + 'Artist'
TO_RELEASE =

Identifies relations linking to a release.

NS_REL_1 + 'Release'
TO_TRACK =

Identifies relations linking to a track.

NS_REL_1 + 'Track'
TO_LABEL =

Identifies relations linking to a label.

NS_REL_1 + 'Label'
TO_URL =

Identifies relations linking to an URL.

NS_REL_1 + 'Url'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = nil, target = nil, direction = nil) ⇒ Relation

Returns a new instance of Relation.



84
85
86
87
88
89
90
# File 'lib/rbrainz/model/relation.rb', line 84

def initialize(type=nil, target=nil, direction=nil)
  @target        = nil
  @attributes    = Array.new
  self.type      = type
  self.target    = target if target
  self.direction = direction
end

Instance Attribute Details

#attributesObject (readonly)

The list of attributes describing this relation.

The attributes permitted depend on the relation type.



67
68
69
# File 'lib/rbrainz/model/relation.rb', line 67

def attributes
  @attributes
end

#begin_dateObject

The begin date.

The definition depends on the relation’s type. It may for example be the day of a marriage or the year an artist joined a band. For other relation types this may be undefined.



75
76
77
# File 'lib/rbrainz/model/relation.rb', line 75

def begin_date
  @begin_date
end

#directionObject

The reading direction.

The direction may be one of DIR_FORWARD, DIR_BACKWARD, or DIR_BOTH, depending on how the relation should be read. For example, if direction is DIR_FORWARD for a cover relation, it is read as “X is a cover of Y”. Some relations are bidirectional, like marriages. In these cases, the direction is Relation.DIR_BOTH.



56
57
58
# File 'lib/rbrainz/model/relation.rb', line 56

def direction
  @direction
end

#end_dateObject

The end date.

As with the begin date, the definition depends on the relation’s type. Depending on the relation type, this may or may not be defined.



82
83
84
# File 'lib/rbrainz/model/relation.rb', line 82

def end_date
  @end_date
end

#targetObject

The relation’s target object.

The target can either be an object of the type Model::Entity or a URL if the type of the relation is TO_URL.



62
63
64
# File 'lib/rbrainz/model/relation.rb', line 62

def target
  @target
end

#typeObject

The relation’s type.



46
47
48
# File 'lib/rbrainz/model/relation.rb', line 46

def type
  @type
end

Instance Method Details

#target_typeObject

The type of target this relation points to.

For MusicBrainz data, the following target types are defined:

  • artists: #TO_ARTIST

  • labels: #TO_LABEL

  • releases: #TO_RELEASE

  • tracks: #TO_TRACK

  • urls: #TO_URL



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rbrainz/model/relation.rb', line 112

def target_type
  if @target.is_a? Model::Entity
    case @target.entity_type
    when :artist
      return TO_ARTIST
    when :release
      return TO_RELEASE
    when :track
      return TO_TRACK
    when :label
      return TO_LABEL
    end
  elsif not @target.nil?
    return TO_URL
  end  
end