Class: Hallon::Enumerator

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject) ⇒ Enumerator

initialize the enumerator with subject.

Parameters:



53
54
55
# File 'lib/hallon/enumerator.rb', line 53

def initialize(subject)
  @pointer = subject.pointer
end

Instance Attribute Details

#pointerSpotify::ManagedPointer (readonly)

Returns:

  • (Spotify::ManagedPointer)


12
13
14
# File 'lib/hallon/enumerator.rb', line 12

def pointer
  @pointer
end

Class Method Details

.item(method) {|item, index, pointer| ... } ⇒ Object

Note:

block passed is used to modify return value from Spotify#item_method

Examples:

modifying result with a block

item :playlist_track do |track|
  Track.from(track)
end

Parameters:

  • method (Symbol, String)

Yields:

  • (item, index, pointer)

    item from calling Spotify#item_method

Yield Parameters:

  • item
  • index (Integer)
  • pointer (Spotify::ManagedPointer)


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

Parameters:

  • method (String, Symbol)


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.

Overloads:

  • #[](index) ⇒ Object?

    Returns:

    • (Object, nil)
  • #[](start, length) ⇒ Array?

    Returns:

    • (Array, nil)
  • #[](range) ⇒ Array?

    Returns:

    • (Array, nil)

See Also:



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.

Yields:

  • obj

Returns:



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.

Returns:

  • (Boolean)

    true if the size is zero.



74
75
76
# File 'lib/hallon/enumerator.rb', line 74

def empty?
  size.zero?
end

#to_sString

Returns String representation of the Enumerator.

Returns:

  • (String)

    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