class SidekiqAdhocJob::Utils::String
Public Class Methods
camelize(word, first_letter_in_uppercase = true)
click to toggle source
# File lib/sidekiq_adhoc_job/utils/string.rb, line 79 def self.camelize(word, first_letter_in_uppercase = true) if first_letter_in_uppercase word.to_s.gsub(%r{/(.?)}) { '::' + Regexp.last_match[1].upcase }.gsub(/(^|_)(.)/) { Regexp.last_match[2].upcase } else word.first + self.camelize(word)[1..-1] end end
classify(input)
click to toggle source
Return a CamelCase version of the string If the given string is a file name, it will convert each file directory into a CamelCase namespace
@param input [::String] the input
@return [::String] the transformed string
@since 0.1.0
@example
require 'sidekiq_adhoc_job/utils/string' SidekiqAdhocJob::Utils::String.classify('sidekiq_adhoc_job') # => 'SidekiqAdhocJob' SidekiqAdhocJob::Utils::String.classify('sidekiq_adhoc_job/utils/string') # => 'SidekiqAdhocJob::Utils::String'
# File lib/sidekiq_adhoc_job/utils/string.rb, line 31 def self.classify(input) string = ::String.new(input.to_s) words = underscore(string).split(CLASSIFY_WORD_SEPARATOR).map!(&:capitalize) delimiters = underscore(string).scan(CLASSIFY_WORD_SEPARATOR) delimiters.map! do |delimiter| delimiter == CLASSIFY_SEPARATOR ? EMPTY_STRING : NAMESPACE_SEPARATOR end words.zip(delimiters).join end
constantize(input)
click to toggle source
# File lib/sidekiq_adhoc_job/utils/string.rb, line 68 def self.constantize(input) names = input.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end
parse(input, symbolize: false)
click to toggle source
# File lib/sidekiq_adhoc_job/utils/string.rb, line 87 def self.parse(input, symbolize: false) return unless input if input == 'true' true elsif input == 'false' false elsif input == 'nil' nil elsif (i = parse_integer(input)) i elsif (f = parse_float(input)) f elsif (j = parse_json(input, symbolize: symbolize)) j else input end end
parse_float(input)
click to toggle source
# File lib/sidekiq_adhoc_job/utils/string.rb, line 113 def self.parse_float(input) Float(input) rescue ArgumentError => _e nil end
parse_integer(input)
click to toggle source
# File lib/sidekiq_adhoc_job/utils/string.rb, line 107 def self.parse_integer(input) Integer(input) rescue ArgumentError => _e nil end
parse_json(input, symbolize: false)
click to toggle source
# File lib/sidekiq_adhoc_job/utils/string.rb, line 119 def self.parse_json(input, symbolize: false) JSON.parse(input, symbolize_names: symbolize) rescue JSON::ParserError => _e nil end
underscore(input)
click to toggle source
Return a downcased and underscore separated version of the string
- If the given string is a namespaced class name, it will convert the namespace separator
-
into a file path separator /
@param input [::String] the input
@return [::String] the transformed string
@since 0.1.0
@example
require 'sidekiq_adhoc_job/utils/string' SidekiqAdhocJob::Utils::String.underscore('SidekiqAdhocJob') # => 'sidekiq_adhoc_job' SidekiqAdhocJob::Utils::String.underscore('SidekiqAdhocJob::Utils::String') # => 'sidekiq_adhoc_job/utils/string'
# File lib/sidekiq_adhoc_job/utils/string.rb, line 59 def self.underscore(input) string = ::String.new(input.to_s) string.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR) string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET) string.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET) string.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET) string.downcase end