Class: Hallon::PlaylistContainer::Folder
- Inherits:
-
Object
- Object
- Hallon::PlaylistContainer::Folder
- 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
-
#begin ⇒ Integer
readonly
Index this folder starts at in the container.
- #container_ptr ⇒ Spotify::Container readonly private
-
#end ⇒ Integer
readonly
Index this folder ends at in the container.
- #id ⇒ Integer readonly
- #name ⇒ String readonly
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if the two folders are the same (same indices, same id).
-
#container ⇒ PlaylistContainer
Playlistcontainer this folder was created from.
-
#contents ⇒ Array<Playlist, Folder>
Contents of this folder.
-
#initialize(container_pointer, indices) ⇒ Folder
constructor
A new instance of Folder.
-
#moved? ⇒ Boolean
True if the folder has moved.
-
#rename(new_name) ⇒ Folder
Rename the folder.
Constructor Details
#initialize(container_pointer, indices) ⇒ Folder
Returns a new instance of Folder.
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
#begin ⇒ Integer (readonly)
Returns index this folder starts at in the container.
64 65 66 |
# File 'lib/hallon/playlist_container.rb', line 64 def begin @begin end |
#container_ptr ⇒ Spotify::Container (readonly, private)
76 77 78 |
# File 'lib/hallon/playlist_container.rb', line 76 def container_ptr @container_ptr end |
#end ⇒ Integer (readonly)
Returns index this folder ends at in the container.
67 68 69 |
# File 'lib/hallon/playlist_container.rb', line 67 def end @end end |
#id ⇒ Integer (readonly)
70 71 72 |
# File 'lib/hallon/playlist_container.rb', line 70 def id @id end |
#name ⇒ String (readonly)
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).
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 |
#container ⇒ PlaylistContainer
Returns playlistcontainer this folder was created from.
80 81 82 |
# File 'lib/hallon/playlist_container.rb', line 80 def container PlaylistContainer.new(container_ptr) end |
#contents ⇒ Array<Playlist, Folder>
Returns contents of this folder.
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.
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
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.
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 |