Class: Hallon::Base

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pointerFFI::Pointer (readonly)

Underlying FFI pointer.

Returns:



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?.

Parameters:

  • pointer (#nil?, #null?)

Returns:

  • (self, nil)

    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


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


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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)

    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

#sessionSession (private)

The current Session instance.

Returns:



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.

Parameters:

  • resource (Spotify::ManagedPointer, String, Link)

Returns:

  • (Spotify::ManagedPointer)

Raises:

  • (TypeError)

    when the pointer is of the wrong type

  • (ArgumentError)

    when pointer could not be created, or null



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_sObject

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