Class: Hallon::PlaylistContainer::Folder

Inherits:
Object
  • Object
show all
Defined in:
lib/hallon/playlist_container.rb

Overview

Folders are parts of playlist containers in that they surround playlists with a beginning marker and an ending marker. The playlists between these markers are considered "inside the folder".

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container_pointer, indices) ⇒ Folder

Returns a new instance of Folder.

Parameters:



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hallon/playlist_container.rb', line 101

def initialize(container_pointer, indices)
  @container_ptr = container_pointer

  @begin = indices.begin
  @end   = indices.end
  @id    = Spotify.playlistcontainer_playlist_folder_id(container_ptr, @begin)
  FFI::Buffer.alloc_out(256) do |buffer|
    error = Spotify.playlistcontainer_playlist_folder_name(container_ptr, @begin, buffer, buffer.size)
    Error.maybe_raise(error) # should not fail, but just to be safe!

    @name = buffer.get_string(0)
  end
end

Instance Attribute Details

#beginInteger (readonly)

Returns index this folder starts at in the container.

Returns:

  • (Integer)

    index this folder starts at in the container.



64
65
66
# File 'lib/hallon/playlist_container.rb', line 64

def begin
  @begin
end

#container_ptrSpotify::Container (readonly, private)

Returns:

  • (Spotify::Container)


76
77
78
# File 'lib/hallon/playlist_container.rb', line 76

def container_ptr
  @container_ptr
end

#endInteger (readonly)

Returns index this folder ends at in the container.

Returns:

  • (Integer)

    index this folder ends at in the container.



67
68
69
# File 'lib/hallon/playlist_container.rb', line 67

def end
  @end
end

#idInteger (readonly)

Returns:

  • (Integer)


70
71
72
# File 'lib/hallon/playlist_container.rb', line 70

def id
  @id
end

#nameString (readonly)

Returns:

  • (String)


73
74
75
# File 'lib/hallon/playlist_container.rb', line 73

def name
  @name
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the two folders are the same (same indices, same id).

Parameters:

Returns:

  • (Boolean)

    true if the two folders are the same (same indices, same id).



117
118
119
120
121
# File 'lib/hallon/playlist_container.rb', line 117

def ==(other)
  !! [:id, :container_ptr, :begin, :end].all? do |attr|
    send(attr) == other.send(attr)
  end if other.is_a?(Folder)
end

#containerPlaylistContainer

Returns playlistcontainer this folder was created from.

Returns:



80
81
82
# File 'lib/hallon/playlist_container.rb', line 80

def container
  PlaylistContainer.new(container_ptr)
end

#contentsArray<Playlist, Folder>

Returns contents of this folder.

Returns:



124
125
126
127
# File 'lib/hallon/playlist_container.rb', line 124

def contents
  container = OpenStruct.new(:pointer => container_ptr)
  Contents.new(container)[(@begin + 1)..(@end - 1)]
end

#moved?Boolean

Returns true if the folder has moved.

Returns:

  • (Boolean)

    true if the folder has moved.



130
131
132
133
# File 'lib/hallon/playlist_container.rb', line 130

def moved?
  Spotify.playlistcontainer_playlist_folder_id(container_ptr, @begin) != id or
  Spotify.playlistcontainer_playlist_folder_id(container_ptr, @end) != id
end

#rename(new_name) ⇒ Folder

Note:

libspotify has no actual folder rename; what happens is that the folder is removed and then re-created at the same position.

Rename the folder.

Parameters:

Returns:

Raises:

  • (IndexError)


90
91
92
93
94
95
96
97
# File 'lib/hallon/playlist_container.rb', line 90

def rename(new_name)
  raise IndexError, "folder has moved from #{@begin}..#{@end}" if moved?

  insert_at = @begin
  container.remove(@begin)
  container.insert_folder(insert_at, new_name)
  container.move(insert_at + 1, @end)
end