Class: SidekiqAdhocJob::Utils::String
- Inherits:
-
Object
- Object
- SidekiqAdhocJob::Utils::String
- Defined in:
- lib/sidekiq_adhoc_job/utils/string.rb
Class Method Summary collapse
- .camelize(word, first_letter_in_uppercase = true) ⇒ Object
-
.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.
- .constantize(input) ⇒ Object
- .parse(input, symbolize: false) ⇒ Object
- .parse_float(input) ⇒ Object
- .parse_integer(input) ⇒ Object
- .parse_json(input, symbolize: false) ⇒ Object
-
.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 /.
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
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 /
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 |