Class: Spotify::Struct
- Inherits:
-
FFI::Struct
- Object
- FFI::Struct
- Spotify::Struct
- Extended by:
- TypeSafety
- Defined in:
- lib/spotify/structs.rb
Overview
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.
Direct Known Subclasses
AudioBufferStats, AudioFormat, OfflineSyncStatus, PlaylistCallbacks, PlaylistContainerCallbacks, SessionCallbacks, SessionConfig, Subscribers
Class Method Summary collapse
-
.enclosing_module ⇒ Spotify::API
This is used by FFI to do type lookups when creating the struct layout.
Instance Method Summary collapse
-
#initialize(pointer = nil, *layout, &block) ⇒ Struct
constructor
When initialized with a hash, assigns each value of the hash to the newly created struct before returning.
-
#to_h ⇒ Hash
Convert the struct to a hash.
-
#to_s ⇒ String
String representation of the struct.
Methods included from TypeSafety
Constructor Details
#initialize(pointer = nil, *layout, &block) ⇒ Struct
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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/spotify/structs.rb', line 24 def initialize(pointer = nil, *layout, &block) if pointer.respond_to?(:each_pair) = pointer pointer = nil else = {} end super(pointer, *layout, &block) if defined?(self.class::DEFAULTS) = self.class::DEFAULTS.merge() end .each_pair do |key, value| self[key] = value end end |
Class Method Details
.enclosing_module ⇒ Spotify::API
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.
13 14 15 |
# File 'lib/spotify/structs.rb', line 13 def self.enclosing_module Spotify::API end |
Instance Method Details
#to_h ⇒ Hash
Convert the struct to a hash.
46 47 48 |
# File 'lib/spotify/structs.rb', line 46 def to_h Hash[members.zip(values)] end |
#to_s ⇒ String
String representation of the struct. Looks like a Hash.
53 54 55 |
# File 'lib/spotify/structs.rb', line 53 def to_s "<#{self.class.name} #{to_h}>" end |