Module: Hanami::Utils::String
- Extended by:
- Dry::Transformer::Registry
- Defined in:
- lib/hanami/utils/string.rb
Overview
String utilities
Constant Summary collapse
- EMPTY_STRING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Empty string for #classify
""
- NAMESPACE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator between Ruby namespaces
"::"
- CLASSIFY_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator for #classify
"_"
- UNDERSCORE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator for #underscore
"/"
- UNDERSCORE_DIVISION_TARGET =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
gsub second parameter used in #underscore
'\1_\2'
- TITLEIZE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator for #titleize
" "
- CAPITALIZE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator for #capitalize
" "
- DASHERIZE_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Separator for #dasherize
"-"
- CLASSIFY_WORD_SEPARATOR =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regexp for #classify
/#{CLASSIFY_SEPARATOR}|#{NAMESPACE_SEPARATOR}|#{UNDERSCORE_SEPARATOR}|#{DASHERIZE_SEPARATOR}/
Class Method Summary collapse
-
.capitalize(input) ⇒ ::String
Returns a capitalized version of the string.
-
.classify(input) ⇒ ::String
Returns a CamelCase version of the string.
-
.dasherize(input) ⇒ Object
Hanami::Utils::String.dasherize(‘hanami_utils’) # => ‘hanami-utils’.
-
.demodulize(input) ⇒ ::String
Returns the string without the Ruby namespace of the class.
-
.namespace(input) ⇒ ::String
Returns the top level namespace name.
-
.rsub(input, pattern, replacement) ⇒ ::String
Replace the rightmost match of ‘pattern` with `replacement`.
-
.titleize(input) ⇒ ::String
Returns a titleized version of the string.
-
.transform(input, *transformations) ⇒ ::String
Applies the given transformation(s) to ‘input`.
-
.underscore(input) ⇒ ::String
Returns a downcased and underscore separated version of the string.
Class Method Details
.capitalize(input) ⇒ ::String
Returns a capitalized version of the string
182 183 184 185 186 187 |
# File 'lib/hanami/utils/string.rb', line 182 def self.capitalize(input) string = ::String.new(input.to_s) head, *tail = underscore(string).split(CLASSIFY_SEPARATOR) tail.unshift(head.capitalize).join(CAPITALIZE_SEPARATOR) end |
.classify(input) ⇒ ::String
Returns a CamelCase version of the string
201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/hanami/utils/string.rb', line 201 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 |
.dasherize(input) ⇒ Object
254 255 256 257 |
# File 'lib/hanami/utils/string.rb', line 254 def self.dasherize(input) string = ::String.new(input.to_s) underscore(string).split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR) end |
.demodulize(input) ⇒ ::String
Returns the string without the Ruby namespace of the class
273 274 275 |
# File 'lib/hanami/utils/string.rb', line 273 def self.demodulize(input) ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).last end |
.namespace(input) ⇒ ::String
Returns the top level namespace name
291 292 293 |
# File 'lib/hanami/utils/string.rb', line 291 def self.namespace(input) ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).first end |
.rsub(input, pattern, replacement) ⇒ ::String
Replace the rightmost match of ‘pattern` with `replacement`
If the pattern cannot be matched, it returns the original string.
This method does NOT mutate the original string.
314 315 316 317 318 319 320 321 322 323 |
# File 'lib/hanami/utils/string.rb', line 314 def self.rsub(input, pattern, replacement) string = ::String.new(input.to_s) if i = string.rindex(pattern) s = string.dup s[i] = replacement s else string end end |
.titleize(input) ⇒ ::String
Returns a titleized version of the string
157 158 159 160 |
# File 'lib/hanami/utils/string.rb', line 157 def self.titleize(input) string = ::String.new(input.to_s) underscore(string).split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR) end |
.transform(input, *transformations) ⇒ ::String
Applies the given transformation(s) to ‘input`
It performs a pipeline of transformations, by applying the given functions from ‘Hanami::Utils::String` and `::String`. The transformations are applied in the given order.
It doesn’t mutate the input, unless you use destructive methods from ‘::String`
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hanami/utils/string.rb', line 121 def self.transform(input, *transformations) fn = @__transformations__.fetch_or_store(transformations.hash) do fns = Dry::Transformer::Function.new(->(object) { object }) transformations.each do |transformation, *args| fns = fns.compose( if transformation.is_a?(Proc) transformation elsif contain?(transformation) self[transformation, *args] elsif input.respond_to?(transformation) ->(i) { i.public_send(transformation, *args) } else raise NoMethodError.new(%(undefined method `#{transformation.inspect}' for #{input.inspect}:#{input.class})) # rubocop:disable Layout/LineLength end ) end fns end fn.call(input) end |
.underscore(input) ⇒ ::String
Returns a downcased and underscore separated version of the string
Revised version of ‘ActiveSupport::Inflector.underscore` implementation
228 229 230 231 232 233 234 235 236 |
# File 'lib/hanami/utils/string.rb', line 228 def self.underscore(input) string = ::String.new(input.to_s) string.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR) 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 |