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) ⇒ Collection

Returns a new instance of Collection.



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

def initialize data, property=:items
  values = data[property]
  super values
  @total = data[:total] || self.count
  @next = data[:next]
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, data) ⇒ 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, data
  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

  prop = prop.to_sym
  prop = :items unless data.has_key?(prop)

  data[prop].map! {|item| model.new(item) }
  self.new(data)
end

Instance Method Details

#moreObject



44
45
46
47
48
# File 'lib/simplespotify/models/collection.rb', line 44

def more
  return [] unless more?
  req = Request.new({endpoint: @next, private: false})
  SimpleSpotify.dispatch(req)
end

#more?Boolean

Returns:

  • (Boolean)


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

def more?
  !@next.nil?
end