Module: Hanami::Utils::String
- Extended by:
- Dry::Transformer::Registry
- Defined in:
- gems/gems/hanami-utils-2.2.0.beta1/lib/hanami/utils/string.rb
Overview
String utilities
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
withreplacement
. -
.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
.classify(input) ⇒ ::String
Returns a CamelCase version of the string
Source: | on GitHub
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
Hanami::Utils::String.dasherize(‘hanami_utils’) # => ‘hanami-utils’
Hanami::Utils::String.dasherize(‘HanamiUtils’) # => “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
If the pattern cannot be matched, it returns the original string.
This method does NOT mutate the original string.
.titleize(input) ⇒ ::String
Returns a titleized version of the string
.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
Source: | on GitHub
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
Source: | on GitHub
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 |