snakecase, camelcase functions moved into

Util::String

module Cache added.
This commit is contained in:
onyx-and-iris 2023-07-17 14:13:08 +01:00
parent eb4920f4a5
commit 2efd07b3d5
4 changed files with 50 additions and 28 deletions

View File

@ -5,12 +5,14 @@ require_relative "midi"
require_relative "event"
require_relative "worker"
require_relative "errors"
require_relative "util"
require_relative "logger"
module Voicemeeter
class Base
include Logging
include Worker
include Util::Cache
attr_reader :kind, :midi, :event, :running, :callback
attr_accessor :cache
@ -106,15 +108,16 @@ module Voicemeeter
end
def get(name, is_string = false)
clear_dirty if @sync
if is_string
c_get = FFI::MemoryPointer.new(:string, 512, true)
CBindings.call(:bind_get_parameter_string_a, name, c_get)
c_get.read_string
else
c_get = FFI::MemoryPointer.new(:float, 1)
CBindings.call(:bind_get_parameter_float, name, c_get)
c_get.read_float.round(1)
polling(:get, name: name) do
if is_string
cget = FFI::MemoryPointer.new(:string, 512, true)
CBindings.call(:bind_get_parameter_string_a, name, cget)
cget.read_string
else
cget = FFI::MemoryPointer.new(:float, 1)
CBindings.call(:bind_get_parameter_float, name, cget)
cget.read_float.round(1)
end
end
end
@ -124,17 +127,20 @@ module Voicemeeter
else
CBindings.call(:bind_set_parameter_float, name, value.to_f)
end
cache.store(name, value)
end
def get_buttonstatus(id, mode)
clear_dirty if @sync
c_get = FFI::MemoryPointer.new(:float, 1)
CBindings.call(:bind_macro_button_get_status, id, c_get, mode)
c_get.read_float.to_i
polling(:get_buttonstatus, id: id, mode: mode) do
cget = FFI::MemoryPointer.new(:float, 1)
CBindings.call(:bind_macro_button_get_status, id, cget, mode)
cget.read_float.to_i
end
end
def set_buttonstatus(id, state, mode)
def set_buttonstatus(id, mode, state)
CBindings.call(:bind_macro_button_set_status, id, state, mode)
cache.store("mb_#{id}_#{mode}", state)
end
def get_level(mode, index)

View File

@ -16,7 +16,7 @@ module Voicemeeter
ffi_convention :stdcall
private_class_method def self.attach_function(c_name, args, returns)
ruby_name = "bind_#{Util.snakecase(c_name.to_s.delete_prefix("VBVMR_"))}".to_sym
ruby_name = "bind_#{Util::String.snakecase(c_name.to_s.delete_prefix("VBVMR_"))}".to_sym
super(ruby_name, c_name, args, returns)
end

View File

@ -19,7 +19,7 @@ module Voicemeeter
end
def fn_name
"VBVMR_#{Util.camelcase(@ruby_name.to_s.delete_prefix("bind_")).sub("macro_button", "macrobutton")}"
"VBVMR_#{Util::String.camelcase(@ruby_name.to_s.delete_prefix("bind_")).sub("macro_button", "macrobutton")}"
end
end
end

View File

@ -1,19 +1,35 @@
module Voicemeeter
module Util
def snakecase(string)
string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr("-", "_")
.gsub(/\s/, "_")
.gsub(/__+/, "_")
.downcase
module String
def snakecase(string)
string.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr("-", "_")
.gsub(/\s/, "_")
.gsub(/__+/, "_")
.downcase
end
def camelcase(string)
string if string !~ /_/ && string =~ /[A-Z]+.*/
string.split("_").map { |e| e.capitalize }.join
end
module_function :snakecase, :camelcase
end
def camelcase(string)
string if string !~ /_/ && string =~ /[A-Z]+.*/
string.split("_").map { |e| e.capitalize }.join
end
module Cache
def polling(func, **kwargs)
params = {
get: kwargs[:name],
get_buttonstatus: "mb_#{kwargs[:id]}_#{kwargs[:mode]}"
}
return cache.delete(params[func]) if cache.key? params[func]
module_function :snakecase, :camelcase
clear_dirty if @sync
yield
end
end
end
end