From 57fca646b5114d5cd14fb9638de6704ef07e1567 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 26 Jul 2023 18:55:35 +0100 Subject: [PATCH] remove the monkey patching --- lib/obsws/event.rb | 8 ++++---- lib/obsws/mixin.rb | 6 +++--- lib/obsws/util.rb | 11 ++++++----- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/obsws/event.rb b/lib/obsws/event.rb index 3cbddb6..0525aa1 100644 --- a/lib/obsws/event.rb +++ b/lib/obsws/event.rb @@ -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 diff --git a/lib/obsws/mixin.rb b/lib/obsws/mixin.rb index 27712dc..3f547bb 100644 --- a/lib/obsws/mixin.rb +++ b/lib/obsws/mixin.rb @@ -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 diff --git a/lib/obsws/util.rb b/lib/obsws/util.rb index 0b5a64e..74fb1f1 100644 --- a/lib/obsws/util.rb +++ b/lib/obsws/util.rb @@ -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