class Spotify::Struct

Spotify::Struct is a regular FFI::Struct, but with type checking that happens in the Spotify::API namespace, and it also allows you to initialize structs with a hash.

Public Class Methods

enclosing_module() click to toggle source

This is used by FFI to do type lookups when creating the struct layout. By overriding this we can trick FFI into looking up types in the right location.

@return [Spotify::API]

# File lib/spotify/structs.rb, line 13
def self.enclosing_module
  Spotify::API
end
new(pointer = nil, *layout, &block) click to toggle source

When initialized with a hash, assigns each value of the hash to the newly created struct before returning.

If not given a hash, it behaves exactly as FFI::Struct.

@param [#each_pair, FFI::Pointer, nil] pointer @param [Array<Symbol, Type>] layout

Calls superclass method
# File lib/spotify/structs.rb, line 24
def initialize(pointer = nil, *layout, &block)
  if pointer.respond_to?(:each_pair)
    options = pointer
    pointer = nil
  else
    options = {}
  end

  super(pointer, *layout, &block)

  if defined?(self.class::DEFAULTS)
    options = self.class::DEFAULTS.merge(options)
  end

  options.each_pair do |key, value|
    self[key] = value
  end
end

Public Instance Methods

to_h() click to toggle source

Convert the struct to a hash.

@return [Hash]

# File lib/spotify/structs.rb, line 46
def to_h
  Hash[members.zip(values)]
end
to_s() click to toggle source

String representation of the struct. Looks like a Hash.

@return [String]

# File lib/spotify/structs.rb, line 53
def to_s
  "<#{self.class.name} #{to_h}>"
end