Class: MusicBrainz::Model::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rbrainz/model/collection.rb

Overview

A Collection is a list of models.

A collection object may only store an extract of the complete data available on the server. This is especially the case if the limit or offset filter was used in the query. The collection object makes the total number of elements on the server and the current offset available with the count respective the offset parameter.

Direct Known Subclasses

ScoredCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count = 0, offset = 0, array = nil) ⇒ Collection

Create a new collection object.

The count and offset parameters should be set according to the values returned by the server.



36
37
38
39
40
41
42
43
44
# File 'lib/rbrainz/model/collection.rb', line 36

def initialize(count=0, offset=0, array=nil)
  @count  = count.to_i
  @offset = offset.to_i
  if array
    @entries = array.to_ary.dup
  else
    @entries = Array.new
  end
end

Instance Attribute Details

#countObject

Returns the total number of elements for that collection on the server.



26
27
28
# File 'lib/rbrainz/model/collection.rb', line 26

def count
  @count
end

#offsetObject

Returns the position at which this collection starts. Used for paging through more than one page of results.



30
31
32
# File 'lib/rbrainz/model/collection.rb', line 30

def offset
  @offset
end

Instance Method Details

#+(other_collection) ⇒ Object

Create a new collection containing the elements of both collections. Count and offset will be set to nil in the new collection.



74
75
76
77
78
79
80
# File 'lib/rbrainz/model/collection.rb', line 74

def +(other_collection)
  new_collection = Collection.new
  (self.to_a + other_collection.to_a).each do |entry|
    new_collection << entry
  end
  return new_collection
end

#<<(entry) ⇒ Object

Add a new element to this collection. Returns self.



47
48
49
50
# File 'lib/rbrainz/model/collection.rb', line 47

def <<(entry)
  @entries << entry
  self # return self to allow something like collection << a << b
end

#[](index) ⇒ Object

Access a random element in the collection by the element’s index.



63
64
65
# File 'lib/rbrainz/model/collection.rb', line 63

def [](index)
  @entries[index]
end

#[]=(index, entry) ⇒ Object

Set a random element in the collection by the element’s index.



68
69
70
# File 'lib/rbrainz/model/collection.rb', line 68

def []=(index, entry)
  @entries[index] = entry
end

#delete(entry) ⇒ Object

Delete an element from the collection.



53
54
55
# File 'lib/rbrainz/model/collection.rb', line 53

def delete(entry)
  @entries.delete(entry)
end

#dupObject

Duplicate this collection.



99
100
101
# File 'lib/rbrainz/model/collection.rb', line 99

def dup
  Collection.new(self.count, self.offset, @entries)
end

#eachObject

Iterate over the contents of the collection.



58
59
60
# File 'lib/rbrainz/model/collection.rb', line 58

def each
  @entries.each {|e| yield e}
end

#empty?Boolean

Returns true, if the collection contains no elements.

Returns:



88
89
90
# File 'lib/rbrainz/model/collection.rb', line 88

def empty?
  @entries.empty?
end

#sizeObject

The number of elements in the collection.



83
84
85
# File 'lib/rbrainz/model/collection.rb', line 83

def size
  @entries.size
end

#to_aObject Also known as: to_ary

Convert the collection into an Array.



93
94
95
# File 'lib/rbrainz/model/collection.rb', line 93

def to_a
  @entries.dup
end