lib/rating_chgk_v2/utils/string_utils.rb
# frozen_string_literal: true
module RatingChgkV2
module Utils
module StringUtils
refine String do
# Underscore a string such that camelcase, dashes and spaces are
# replaced by underscores.
# Initial code taken from Facets gem by Rubyworks
# https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
def snakecase
base_class_name.
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z\d])([A-Z])/, '\1_\2').
tr('-', '_').
gsub(/\s/, '_').
gsub(/__+/, '_').
downcase
end
# Turn `Module::Nested::ClassName` to just `ClassName`
def base_class_name
split('::').last
end
end
end
end
end