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

View File

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

View File

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