Module: Rockbox::Type

Defined in:
lib/rockbox/types.rb

Overview


Value objects


Each type is a Struct that ignores unknown keys at construction so the firmware can add fields without breaking older SDK builds.


Class Method Summary collapse

Class Method Details

.with(*members) ⇒ Object

Build a Struct that tolerates unknown / missing fields when coming from a Hash. Struct subclasses raise on unknown keys with ‘keyword_init`, so from_hash filters the input.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rockbox/types.rb', line 58

def self.with(*members)
  klass = Struct.new(*members, keyword_init: true)
  klass.define_singleton_method(:from_hash) do |hash|
    return nil if hash.nil?
    attrs = {}
    members.each { |m| attrs[m] = hash[m] }
    new(**attrs)
  end
  klass.define_singleton_method(:known_members) { members.dup }
  klass
end