rework factory method.

each kind Class separately defined.

Kinds.get now uses fetch() (throws error if invalid key)
This commit is contained in:
onyx-and-iris 2023-07-16 22:33:02 +01:00
parent ebd41bbc91
commit d387503fa2
2 changed files with 23 additions and 8 deletions

View File

@ -40,7 +40,7 @@ module Voicemeeter
public public
def get(kind_id) def get(kind_id)
KIND_MAPS[kind_id] KIND_MAPS.fetch(kind_id)
end end
ALL = KIND_MAPS.values ALL = KIND_MAPS.values

View File

@ -15,8 +15,6 @@ module Voicemeeter
private private
class Remote < Base class Remote < Base
attr_reader :strip, :bus, :button, :vban, :command, :recorder, :device
def initialize(kind, **kwargs) def initialize(kind, **kwargs)
super super
@strip = (0...kind.num_strip).map { |i| Strip::Strip.make(self, i) } @strip = (0...kind.num_strip).map { |i| Strip::Strip.make(self, i) }
@ -41,15 +39,32 @@ module Voicemeeter
end end
end end
class RemoteBasic < Remote
attr_reader :strip, :bus, :button, :vban, :command, :device
end
class RemoteBanana < Remote
attr_reader :strip, :bus, :button, :vban, :command, :device, :recorder
end
class RemotePotato < Remote
attr_reader :strip, :bus, :button, :vban, :command, :device, :recorder
end
public public
def self.new(kind_id, **kwargs) def self.new(kind_id, **kwargs)
remotes = kind = Kinds.get(kind_id)
Kinds::ALL.to_h { |kind| [kind.name, Remote.new(kind, **kwargs)] } rescue KeyError
unless remotes.key? kind_id raise Errors::VMError.new "unknown Voicemeeter kind #{kind_id}"
raise Errors::VMError.new("unknown Voicemeeter kind #{kind_id}") else
if kind_id == :basic
RemoteBasic.new(kind, **kwargs)
elsif kind_id == :banana
RemoteBanana.new(kind, **kwargs)
elsif kind_id == :potato
RemotePotato.new(kind, **kwargs)
end end
remotes[kind_id]
end end
end end
end end