remove the monkey patching

This commit is contained in:
onyx-and-iris 2023-07-26 18:55:35 +01:00
parent d12a1a5954
commit 57fca646b5
3 changed files with 13 additions and 12 deletions

View File

@ -35,7 +35,7 @@ module OBSWS
end end
module Callbacks module Callbacks
include Util include Util::String
def observers def observers
@observers ||= [] @observers ||= []
@ -52,11 +52,11 @@ module OBSWS
def notify_observers(event, data) def notify_observers(event, data)
observers.each do |o| observers.each do |o|
if o.respond_to? "on_#{event.to_snake}" if o.respond_to? "on_#{snakecase(event)}"
if data.empty? if data.empty?
o.send("on_#{event.to_snake}") o.send("on_#{snakecase(event)}")
else else
o.send("on_#{event.to_snake}", data) o.send("on_#{snakecase(event)}", data)
end end
end end
end end

View File

@ -3,11 +3,11 @@ require_relative "util"
module OBSWS module OBSWS
module Mixin module Mixin
module Meta module Meta
include Util include Util::String
def make_field_methods(*params) def make_field_methods(*params)
params.each do |param| params.each do |param|
define_singleton_method(param.to_s.to_snake) { @resp[param] } define_singleton_method(snakecase(param.to_s)) { @resp[param] }
end end
end end
end end
@ -23,7 +23,7 @@ module OBSWS
def empty? = @fields.empty? def empty? = @fields.empty?
def attrs = @fields.map { |f| f.to_s.to_snake } def attrs = @fields.map { |f| snakecase(f.to_s) }
end end
class Response < MetaObject class Response < MetaObject

View File

@ -1,12 +1,13 @@
module OBSWS module OBSWS
module Util module Util
class ::String module String
def to_camel def camelcase(s)
split("_").map(&:capitalize).join s.split("_").map(&:capitalize).join
end end
def to_snake def snakecase(s)
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') s
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2')
.downcase .downcase
end end