Class: SimpleSpotify::Model::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/simplespotify/models/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, property = :items, &refresh) ⇒ Collection

Returns a new instance of Collection.



26
27
28
29
30
31
32
# File 'lib/simplespotify/models/collection.rb', line 26

def initialize data, property=:items, &refresh
  values = data[property]
  super values
  @total = data[:total] || self.count
  @next = data[:next]
  @refresh = refresh if block_given?
end

Instance Attribute Details

#totalObject

Returns the value of attribute total.



6
7
8
# File 'lib/simplespotify/models/collection.rb', line 6

def total
  @total
end

Class Method Details

.of(type, collection, &refresh) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/simplespotify/models/collection.rb', line 8

def self.of type, collection, &refresh
  if type.is_a? Class
    model = type;
    prop = type.to_s.split('::').last.downcase+'s'
  else
    model_name = type.to_s.gsub(/s$/, '')
    model = Model.const_get(model_name.capitalize)
    prop = model_name+'s'
  end

  collection = {items: collection} unless collection.is_a? Hash
  prop = collection.has_key?(prop.to_sym) ? prop.to_sym : :items

  collection[prop].map! {|item| model.new(item) }
  self.new(collection, prop, &refresh)
end

Instance Method Details

#moreObject



45
46
47
48
49
50
51
52
53
# File 'lib/simplespotify/models/collection.rb', line 45

def more
  return [] unless more?
  if @refresh
    @refresh.call @next
  else
    req = Request.new(@next, {private: true})
    SimpleSpotify.dispatch(req)
  end
end

#more?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/simplespotify/models/collection.rb', line 40

def more?
  !@next.nil?
end