Class: MusicBrainz::Model::Collection
- Inherits:
-
Object
- Object
- MusicBrainz::Model::Collection
- 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
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the total number of elements for that collection on the server.
-
#offset ⇒ Object
Returns the position at which this collection starts.
Instance Method Summary collapse
-
#+(other_collection) ⇒ Object
Create a new collection containing the elements of both collections.
-
#<<(entry) ⇒ Object
Add a new element to this collection.
-
#[](index) ⇒ Object
Access a random element in the collection by the element’s index.
-
#[]=(index, entry) ⇒ Object
Set a random element in the collection by the element’s index.
-
#delete(entry) ⇒ Object
Delete an element from the collection.
-
#dup ⇒ Object
Duplicate this collection.
-
#each ⇒ Object
Iterate over the contents of the collection.
-
#empty? ⇒ Boolean
Returns true, if the collection contains no elements.
-
#initialize(count = 0, offset = 0, array = nil) ⇒ Collection
constructor
Create a new collection object.
-
#size ⇒ Object
The number of elements in the collection.
-
#to_a ⇒ Object
(also: #to_ary)
Convert the collection into an Array.
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
#count ⇒ Object
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 |
#offset ⇒ Object
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 |
#dup ⇒ Object
Duplicate this collection.
99 100 101 |
# File 'lib/rbrainz/model/collection.rb', line 99 def dup Collection.new(self.count, self.offset, @entries) end |
#each ⇒ Object
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.
88 89 90 |
# File 'lib/rbrainz/model/collection.rb', line 88 def empty? @entries.empty? end |
#size ⇒ Object
The number of elements in the collection.
83 84 85 |
# File 'lib/rbrainz/model/collection.rb', line 83 def size @entries.size end |
#to_a ⇒ Object 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 |