Class: Hallon::Enumerator
- Inherits:
-
Object
- Object
- Hallon::Enumerator
- Includes:
- Enumerable
- Defined in:
- lib/hallon/enumerator.rb
Overview
Hallon::Enumerator is like a lazy Array.
It provides methods from Enumerable to enumerate through its’ contents, size information and Array access methods. It’s used throughout Hallon for collections of items such as artist tracks, albums and so on.
Direct Known Subclasses
AlbumBrowse::Copyrights, AlbumBrowse::Tracks, ArtistBrowse::Albums, ArtistBrowse::PortraitLinks, ArtistBrowse::Portraits, ArtistBrowse::SimilarArtists, ArtistBrowse::TopHits, ArtistBrowse::Tracks, Playlist::Tracks, PlaylistContainer::Contents, Search::Albums, Search::Artists, Search::PlaylistEnumerator, Search::Tracks, Toplist::Albums, Toplist::Artists, Toplist::Tracks, Track::Artists
Instance Attribute Summary collapse
- #pointer ⇒ Spotify::ManagedPointer readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Works exactly the same as Array#[], including the special cases.
-
#each { ... } ⇒ Enumerator
Yield each item out of the enumerator.
-
#empty? ⇒ Boolean
True if the size is zero.
-
#initialize(subject) ⇒ Enumerator
constructor
initialize the enumerator with
subject
. -
#to_s ⇒ String
String representation of the Enumerator.
Constructor Details
#initialize(subject) ⇒ Enumerator
initialize the enumerator with subject
.
53 54 55 |
# File 'lib/hallon/enumerator.rb', line 53 def initialize(subject) @pointer = subject.pointer end |
Instance Attribute Details
#pointer ⇒ Spotify::ManagedPointer (readonly)
12 13 14 |
# File 'lib/hallon/enumerator.rb', line 12 def pointer @pointer end |
Class Method Details
.item(method) {|item, index, pointer| ... } ⇒ Object
block passed is used to modify return value from Spotify#item_method
42 43 44 45 46 47 48 |
# File 'lib/hallon/enumerator.rb', line 42 def self.item(method, &block) define_method(:at) do |index| item = Spotify.public_send(method, pointer, index) item = instance_exec(item, index, pointer, &block) if block_given? item end end |
.size(method) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/hallon/enumerator.rb', line 19 def self.size(method) # this method is about twice as fast as define_method/public_send class_eval <<-SIZE, __FILE__, __LINE__ + 1 def size Spotify.#{method}(pointer) end SIZE end |
Instance Method Details
#[](index) ⇒ Object? #[](start, length) ⇒ Array? #[](range) ⇒ Array?
Works exactly the same as Array#[], including the special cases.
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/hallon/enumerator.rb', line 90 def [](*args) # crazy inefficient, but also crazy easy, don’t hate me :( items = [*0...size] result = items[*args] if result.nil? nil elsif result.respond_to?(:map) result.map { |index| at(index) } else at(result) end end |
#each { ... } ⇒ Enumerator
Yield each item out of the enumerator.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/hallon/enumerator.rb', line 61 def each index = 0 # check size on each iteration, in case it changes while index < size yield self[index] index += 1 end self end |
#empty? ⇒ Boolean
Returns true if the size is zero.
74 75 76 |
# File 'lib/hallon/enumerator.rb', line 74 def empty? size.zero? end |
#to_s ⇒ String
Returns String representation of the Enumerator.
105 106 107 |
# File 'lib/hallon/enumerator.rb', line 105 def to_s "<#{self.class.name}:0x#{object_id.to_s(16)} @size=#{size}>" end |