Module: MusicBrainz::Model::Relateable

Included in:
Individual, Release, Track
Defined in:
lib/rbrainz/model/relateable.rb

Overview

Mixin module to add add advanced relationship capabilities to Entity classes.

see

Relation

Instance Method Summary collapse

Instance Method Details

#add_relation(relation) ⇒ Object

Adds a Relation.

This method adds relation to the list of relations. The given relation has to be initialized, at least the target type has to be set.



28
29
30
# File 'lib/rbrainz/model/relateable.rb', line 28

def add_relation(relation)
  relations[relation.target_type] << relation
end

#get_relations(options = {:target_type => nil, :relation_type => nil, :required_attributes => [], :direction => nil}) ⇒ Object

Returns a list of Relation objects.

If target_type is given, only relations of that target type are returned. For MusicBrainz, the following target types are defined:

  • Relation::TO_ARTIST

  • Relation::TO_RELEASE

  • Relation::TO_TRACK

  • Relation::TO_URL

If target_type is Relation::TO_ARTIST, for example, this method returns all relations between this Entity and artists.

You may use the relation_type parameter to further restrict the selection. If it is set, only relations with the given relation type are returned. The required_attributes sequence lists attributes that have to be part of all returned relations.

If direction is set, only relations with the given reading direction are returned. You can use the Relation::DIR_FORWARD, Relation::DIR_BACKWARD, and Relation::DIR_BOTH constants for this.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbrainz/model/relateable.rb', line 57

def get_relations(options = {:target_type => nil, :relation_type => nil,
                             :required_attributes => [], :direction => nil})
  Utils.check_options options, 
      :target_type, :relation_type, :required_attributes, :direction
  
  target_type   = Utils.add_namespace(options[:target_type], NS_REL_1)
  relation_type = Utils.add_namespace(options[:relation_type], NS_REL_1)
  required_attributes = 
    options[:required_attributes] ? options[:required_attributes] : []
  direction = options[:direction]
  
  # Select all relevant relations depending on the requested target type
  if target_type
    result = relations[target_type].to_a
  else
    result = relations.values.flatten
  end
  
  # Filter for direction
  result = result.find_all { |r| r.direction == direction } if direction
  
  # Filter for relation type
  result = result.find_all{ |r| r.type == relation_type } if relation_type
  
  # Filter for attributes
  required = required_attributes.map{|a| Utils.add_namespace(a, NS_REL_1)}.to_set
  result.find_all do |r|
       required.subset?( r.attributes.to_set )
  end
end

#relation_target_typesObject

Returns a list of target types available for this entity.

Use this to find out to which types of targets this entity has relations. If the entity only has relations to tracks and artists, for example, then a list containg the strings Relation::TO_TRACK and Relation::TO_ARTIST is returned.



96
97
98
99
100
101
102
# File 'lib/rbrainz/model/relateable.rb', line 96

def relation_target_types
  result = []
  relations.each_pair {|type, relations|
    result << type unless relations.empty?
  }
  return result
end