Class: SidekiqAdhocJob::Utils::String

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_adhoc_job/utils/string.rb

Class Method Summary collapse

Class Method Details

.camelize(word, first_letter_in_uppercase = true) ⇒ Object



79
80
81
82
83
84
85
# 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) ⇒ ::String

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

Examples:

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'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

See Also:

Since:

  • 0.1.0



31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



113
114
115
116
117
# 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) ⇒ Object



107
108
109
110
111
# 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) ⇒ Object



119
120
121
122
123
# 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) ⇒ ::String

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 /

Examples:

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'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

See Also:

Since:

  • 0.1.0



59
60
61
62
63
64
65
66
# 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