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
def get(kind_id)
KIND_MAPS[kind_id]
KIND_MAPS.fetch(kind_id)
end
ALL = KIND_MAPS.values

View File

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