Class: Hallon::Base
- Inherits:
-
Object
- Object
- Hallon::Base
- Defined in:
- lib/hallon/base.rb
Overview
All objects in Hallon are mere representations of Spotify objects. Hallon::Base covers basic functionality shared by all of these.
Direct Known Subclasses
Album, AlbumBrowse, Artist, ArtistBrowse, Image, Link, Player, Playlist, PlaylistContainer, Search, Session, Toplist, Track, User, User::Post
Instance Attribute Summary collapse
-
#pointer ⇒ FFI::Pointer
readonly
Underlying FFI pointer.
Class Method Summary collapse
-
.from(pointer, *args, &block) ⇒ self?
A new instance of self, unless given pointer is #null?.
- .from_link(as_object, &block) ⇒ Object private
- .to_link(cmethod) ⇒ Object private
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if both objects represent the same object.
-
#is_linkable? ⇒ Boolean
private
True if the object can convert links to pointers.
-
#session ⇒ Session
private
The current Session instance.
-
#to_pointer(resource, type, *args) ⇒ Spotify::ManagedPointer
private
Convert a given object to a pointer by best of ability.
-
#to_s ⇒ Object
Default string representation of self.
Instance Attribute Details
#pointer ⇒ FFI::Pointer (readonly)
Underlying FFI pointer.
20 21 22 |
# File 'lib/hallon/base.rb', line 20 def pointer @pointer end |
Class Method Details
.from(pointer, *args, &block) ⇒ self?
Returns a new instance of self, unless given pointer is #null?.
8 9 10 11 12 13 14 15 |
# File 'lib/hallon/base.rb', line 8 def self.from(pointer, *args, &block) is_nil = pointer.nil? is_null = pointer.null? if pointer.respond_to?(:null?) unless is_nil or is_null new(pointer, *args, &block) end end |
.from_link(as_object, &block) ⇒ Object (private)
62 63 64 65 |
# File 'lib/hallon/base.rb', line 62 def self.from_link(as_object, &block) # this is here to work around a YARD limitation, see # {Linkable} for the actual source end |
.to_link(cmethod) ⇒ Object (private)
49 50 51 52 |
# File 'lib/hallon/base.rb', line 49 def self.to_link(cmethod) # this is here to work around a YARD limitation, see # {Linkable} for the actual source end |
Instance Method Details
#==(other) ⇒ Boolean
True if both objects represent the same object.
26 27 28 29 30 31 32 |
# File 'lib/hallon/base.rb', line 26 def ==(other) if other.respond_to?(:pointer) pointer == other.pointer else super end end |
#is_linkable? ⇒ Boolean (private)
Returns true if the object can convert links to pointers.
105 106 107 |
# File 'lib/hallon/base.rb', line 105 def is_linkable? respond_to?(:from_link, true) end |
#session ⇒ Session (private)
The current Session instance.
70 71 72 |
# File 'lib/hallon/base.rb', line 70 def session Session.instance end |
#to_pointer(resource, type, *args) ⇒ Spotify::ManagedPointer (private)
Convert a given object to a pointer by best of ability.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/hallon/base.rb', line 80 def to_pointer(resource, type, *args) if resource.is_a?(FFI::Pointer) and not resource.is_a?(Spotify::ManagedPointer) raise TypeError, "Hallon does not support raw FFI::Pointers, wrap it in a Spotify::ManagedPointer" end pointer = if resource.is_a?(type) resource elsif is_linkable? and resource.is_a?(Spotify::Link) from_link(resource, *args) elsif is_linkable? and Link.valid?(resource) from_link(resource, *args) elsif block_given? yield(resource, *args) end if pointer.nil? or pointer.null? raise ArgumentError, "#{resource.inspect} could not be converted to a spotify #{type} pointer" elsif not pointer.is_a?(type) raise TypeError, "“#{resource}” is a #{resource.class}, #{type} expected" else pointer end end |
#to_s ⇒ Object
Default string representation of self.
35 36 37 38 39 |
# File 'lib/hallon/base.rb', line 35 def to_s name = self.class.name address = pointer.address.to_s(16) "<#{name} address=0x#{address}>" end |