From 099ef38fadad67be1f9b9fdf7aead3d4cbdc8ec3 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Mon, 17 Jul 2023 06:03:37 +0100 Subject: [PATCH] replace core extensions with util functions --- lib/voicemeeter/cbindings.rb | 4 ++-- .../core_extensions/string/camelcase.rb | 14 -------------- .../core_extensions/string/snakecase.rb | 18 ------------------ lib/voicemeeter/errors.rb | 4 ++-- lib/voicemeeter/util.rb | 19 +++++++++++++++++++ 5 files changed, 23 insertions(+), 36 deletions(-) delete mode 100644 lib/voicemeeter/core_extensions/string/camelcase.rb delete mode 100644 lib/voicemeeter/core_extensions/string/snakecase.rb create mode 100644 lib/voicemeeter/util.rb diff --git a/lib/voicemeeter/cbindings.rb b/lib/voicemeeter/cbindings.rb index f1c3932..a7cb2fd 100644 --- a/lib/voicemeeter/cbindings.rb +++ b/lib/voicemeeter/cbindings.rb @@ -1,6 +1,6 @@ require_relative "install" require_relative "errors" -require_relative "core_extensions/string/snakecase" +require_relative "util" module Voicemeeter module CBindings @@ -16,7 +16,7 @@ module Voicemeeter ffi_convention :stdcall private_class_method def self.attach_function(c_name, args, returns) - ruby_name = "bind_#{c_name.to_s.delete_prefix("VBVMR_").snakecase}".to_sym + ruby_name = "bind_#{Util.snakecase(c_name.to_s.delete_prefix("VBVMR_"))}".to_sym super(ruby_name, c_name, args, returns) end diff --git a/lib/voicemeeter/core_extensions/string/camelcase.rb b/lib/voicemeeter/core_extensions/string/camelcase.rb deleted file mode 100644 index 44004a3..0000000 --- a/lib/voicemeeter/core_extensions/string/camelcase.rb +++ /dev/null @@ -1,14 +0,0 @@ -module Voicemeeter - module Ext - module String - module CamelCase - class ::String - def camelcase - self if self !~ /_/ && self =~ /[A-Z]+.*/ - split("_").map { |e| e.capitalize }.join - end - end - end - end - end -end diff --git a/lib/voicemeeter/core_extensions/string/snakecase.rb b/lib/voicemeeter/core_extensions/string/snakecase.rb deleted file mode 100644 index d55637e..0000000 --- a/lib/voicemeeter/core_extensions/string/snakecase.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Voicemeeter - module Ext - module String - module SnakeCase - class ::String - def snakecase - gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') - .gsub(/([a-z\d])([A-Z])/, '\1_\2') - .tr("-", "_") - .gsub(/\s/, "_") - .gsub(/__+/, "_") - .downcase - end - end - end - end - end -end diff --git a/lib/voicemeeter/errors.rb b/lib/voicemeeter/errors.rb index 61ce1d8..dcb09e9 100644 --- a/lib/voicemeeter/errors.rb +++ b/lib/voicemeeter/errors.rb @@ -1,4 +1,4 @@ -require_relative "core_extensions/string/camelcase" +require_relative "util" module Voicemeeter module Errors @@ -19,7 +19,7 @@ module Voicemeeter end def fn_name - "VBVMR_#{@ruby_name.to_s.delete_prefix("bind_").camelcase.sub("macro_button", "macrobutton")}" + "VBVMR_#{Util.camelcase(@ruby_name.to_s.delete_prefix("bind_")).sub("macro_button", "macrobutton")}" end end end diff --git a/lib/voicemeeter/util.rb b/lib/voicemeeter/util.rb new file mode 100644 index 0000000..10dd97a --- /dev/null +++ b/lib/voicemeeter/util.rb @@ -0,0 +1,19 @@ +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 + end + + def camelcase(string) + string if string !~ /_/ && string =~ /[A-Z]+.*/ + string.split("_").map { |e| e.capitalize }.join + end + + module_function :snakecase, :camelcase + end +end