Class: Hanami::Utils::String
- Inherits:
-
Object
- Object
- Hanami::Utils::String
- Extended by:
- Transproc::Composer, Transproc::Registry
- Defined in:
- lib/hanami/utils/string.rb
Overview
String on steroids
Direct Known Subclasses
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
''.freeze
- 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
'::'.freeze
- 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
'_'.freeze
- TOKENIZE_REGEXP =
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 #tokenize
/\((.*)\)/.freeze
- TOKENIZE_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 #tokenize
'|'.freeze
- 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
'/'.freeze
- 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'.freeze
- 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
' '.freeze
- 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
' '.freeze
- 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
'-'.freeze
- 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}/.freeze
Class Method Summary collapse
-
.bind(value, binding, fun) ⇒ Object
private
Extracted from ‘transproc` source code.
-
.capitalize(input) ⇒ ::String
Return a capitalized version of the string.
-
.classify(input) ⇒ ::String
Return a CamelCase version of the string.
-
.dasherize(input) ⇒ Object
Hanami::Utils::String.dasherize(‘hanami_utils’) # => ‘hanami-utils’.
-
.demodulize(input) ⇒ ::String
Return the string without the Ruby namespace of the class.
-
.namespace(input) ⇒ ::String
Return the top level namespace name.
- .pluralize(input) ⇒ ::String deprecated Deprecated.
-
.rsub(input, pattern, replacement) ⇒ ::String
Replace the rightmost match of ‘pattern` with `replacement`.
- .singularize(input) ⇒ ::String deprecated Deprecated.
-
.titleize(input) ⇒ ::String
Return a titleized version of the string.
-
.transform(input, *transformations) ⇒ ::String
Apply the given transformation(s) to ‘input`.
-
.underscore(input) ⇒ ::String
Return a downcased and underscore separated version of the string.
Instance Method Summary collapse
- #==(other) ⇒ TrueClass, FalseClass (also: #eql?) deprecated Deprecated.
- #capitalize ⇒ Hanami::Utils::String deprecated Deprecated.
-
#classify ⇒ Hanami::Utils::String
deprecated
Deprecated.
Use String.classify
-
#dasherize ⇒ Hanami::Utils::String
deprecated
Deprecated.
Use String.dasherize
- #demodulize ⇒ Hanami::Utils::String deprecated Deprecated.
- #gsub(pattern, replacement = nil, &blk) ⇒ ::String deprecated Deprecated.
- #hash ⇒ Integer deprecated Deprecated.
- #initialize(string) ⇒ Hanami::Utils::String constructor deprecated Deprecated.
-
#method_missing(method_name, *args, &blk) ⇒ Object
private
Override Ruby’s method_missing in order to provide ::String interface.
-
#namespace ⇒ Hanami::Utils::String
deprecated
Deprecated.
Use String.namespace
- #pluralize ⇒ Hanami::Utils::String deprecated private Deprecated.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
private
Override Ruby’s respond_to_missing? in order to support ::String interface.
-
#rsub(pattern, replacement) ⇒ Hanami::Utils::String
deprecated
Deprecated.
Use String.rsub
- #scan(pattern, &blk) ⇒ Array<::String> deprecated Deprecated.
- #singularize ⇒ Hanami::Utils::String deprecated private Deprecated.
- #split(pattern, limit = 0) ⇒ Array<::String> deprecated Deprecated.
-
#titleize ⇒ Hanami::Utils::String
deprecated
Deprecated.
Use String.titleize
- #to_s ⇒ ::String (also: #to_str) deprecated Deprecated.
- #tokenize { ... } ⇒ void deprecated Deprecated.
- #underscore ⇒ Hanami::Utils::String deprecated Deprecated.
Constructor Details
#initialize(string) ⇒ Hanami::Utils::String
Initialize the string
395 396 397 |
# File 'lib/hanami/utils/string.rb', line 395 def initialize(string) @string = string.to_s end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &blk) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override Ruby’s method_missing in order to provide ::String interface
735 736 737 738 739 740 741 |
# File 'lib/hanami/utils/string.rb', line 735 def method_missing(method_name, *args, &blk) raise NoMethodError.new(%(undefined method `#{method_name}' for "#{@string}":#{self.class})) unless respond_to?(method_name) s = @string.__send__(method_name, *args, &blk) s = self.class.new(s) if s.is_a?(::String) s end |
Class Method Details
.bind(value, binding, fun) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extracted from ‘transproc` source code
‘transproc` is Copyright 2014 by Piotr Solnica ([email protected]), released under the MIT License
163 164 165 |
# File 'lib/hanami/utils/string.rb', line 163 def self.bind(value, binding, fun) binding.instance_exec(value, &fun) end |
.capitalize(input) ⇒ ::String
Return a capitalized version of the string
204 205 206 207 208 209 |
# File 'lib/hanami/utils/string.rb', line 204 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
Return a CamelCase version of the string
223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/hanami/utils/string.rb', line 223 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
276 277 278 279 |
# File 'lib/hanami/utils/string.rb', line 276 def self.dasherize(input) string = ::String.new(input.to_s) underscore(string).split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR) end |
.demodulize(input) ⇒ ::String
Return the string without the Ruby namespace of the class
295 296 297 |
# File 'lib/hanami/utils/string.rb', line 295 def self.demodulize(input) ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).last end |
.namespace(input) ⇒ ::String
Return the top level namespace name
313 314 315 |
# File 'lib/hanami/utils/string.rb', line 313 def self.namespace(input) ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).first end |
.pluralize(input) ⇒ ::String
Return a pluralized version of self.
332 333 334 335 |
# File 'lib/hanami/utils/string.rb', line 332 def self.pluralize(input) string = ::String.new(input.to_s) Inflector.pluralize(string) 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.
376 377 378 379 380 381 382 383 384 385 |
# File 'lib/hanami/utils/string.rb', line 376 def self.rsub(input, pattern, replacement) string = ::String.new(input.to_s) if i = string.rindex(pattern) # rubocop:disable Lint/AssignmentInCondition s = string.dup s[i] = replacement s else string end end |
.singularize(input) ⇒ ::String
Return a singularized version of self.
352 353 354 355 |
# File 'lib/hanami/utils/string.rb', line 352 def self.singularize(input) string = ::String.new(input.to_s) Inflector.singularize(string) end |
.titleize(input) ⇒ ::String
Return a titleized version of the string
179 180 181 182 |
# File 'lib/hanami/utils/string.rb', line 179 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
Apply 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`
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/hanami/utils/string.rb', line 134 def self.transform(input, *transformations) fn = @__transformations__.fetch_or_store(transformations.hash) do compose do |fns| transformations.each do |transformation, *args| fns << if transformation.is_a?(Proc) transformation elsif contain?(transformation) self[transformation, *args] elsif input.respond_to?(transformation) t(:bind, input, ->(i) { i.public_send(transformation, *args) }) else raise NoMethodError.new(%(undefined method `#{transformation.inspect}' for #{input.inspect}:#{input.class})) end end end end fn.call(input) end |
.underscore(input) ⇒ ::String
Return a downcased and underscore separated version of the string
Revised version of ‘ActiveSupport::Inflector.underscore` implementation
250 251 252 253 254 255 256 257 258 |
# File 'lib/hanami/utils/string.rb', line 250 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 |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass Also known as: eql?
Equality
647 648 649 |
# File 'lib/hanami/utils/string.rb', line 647 def ==(other) to_s == other end |
#capitalize ⇒ Hanami::Utils::String
Use capitalize
Return a capitalized version of the string
439 440 441 442 443 444 445 |
# File 'lib/hanami/utils/string.rb', line 439 def capitalize head, *tail = underscore.split(CLASSIFY_SEPARATOR) self.class.new( tail.unshift(head.capitalize).join(CAPITALIZE_SEPARATOR) ) end |
#classify ⇒ Hanami::Utils::String
Use classify
Return a CamelCase version of the string
459 460 461 462 463 464 465 466 467 468 |
# File 'lib/hanami/utils/string.rb', line 459 def classify words = underscore.split(CLASSIFY_WORD_SEPARATOR).map!(&:capitalize) delimiters = underscore.scan(CLASSIFY_WORD_SEPARATOR) delimiters.map! do |delimiter| delimiter == CLASSIFY_SEPARATOR ? EMPTY_STRING : NAMESPACE_SEPARATOR end self.class.new words.zip(delimiters).join end |
#dasherize ⇒ Hanami::Utils::String
Use dasherize
Return a downcased and dash separated version of the string
512 513 514 |
# File 'lib/hanami/utils/string.rb', line 512 def dasherize self.class.new underscore.split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR) end |
#demodulize ⇒ Hanami::Utils::String
Use demodulize
Return the string without the Ruby namespace of the class
531 532 533 |
# File 'lib/hanami/utils/string.rb', line 531 def demodulize self.class.new split(NAMESPACE_SEPARATOR).last end |
#gsub(pattern, replacement = nil, &blk) ⇒ ::String
Replace the given pattern with the given replacement
673 674 675 676 677 678 679 |
# File 'lib/hanami/utils/string.rb', line 673 def gsub(pattern, replacement = nil, &blk) if block_given? @string.gsub(pattern, &blk) else @string.gsub(pattern, replacement) end end |
#hash ⇒ Integer
Returns the hash of the internal string
625 626 627 |
# File 'lib/hanami/utils/string.rb', line 625 def hash @string.hash end |
#namespace ⇒ Hanami::Utils::String
Use namespace
Return the top level namespace name
550 551 552 |
# File 'lib/hanami/utils/string.rb', line 550 def namespace self.class.new split(NAMESPACE_SEPARATOR).first end |
#pluralize ⇒ Hanami::Utils::String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a pluralized version of self.
602 603 604 |
# File 'lib/hanami/utils/string.rb', line 602 def pluralize self.class.new Inflector.pluralize(self) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Override Ruby’s respond_to_missing? in order to support ::String interface
747 748 749 |
# File 'lib/hanami/utils/string.rb', line 747 def respond_to_missing?(method_name, include_private = false) @string.respond_to?(method_name, include_private) end |
#rsub(pattern, replacement) ⇒ Hanami::Utils::String
Use rsub
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.
719 720 721 722 723 724 725 726 727 |
# File 'lib/hanami/utils/string.rb', line 719 def rsub(pattern, replacement) if i = rindex(pattern) # rubocop:disable Lint/AssignmentInCondition s = @string.dup s[i] = replacement self.class.new s else self end end |
#scan(pattern, &blk) ⇒ Array<::String>
Iterate through the string, matching the pattern. Either return all those patterns, or pass them to the block.
690 691 692 |
# File 'lib/hanami/utils/string.rb', line 690 def scan(pattern, &blk) @string.scan(pattern, &blk) end |
#singularize ⇒ Hanami::Utils::String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return a singularized version of self.
615 616 617 |
# File 'lib/hanami/utils/string.rb', line 615 def singularize self.class.new Inflector.singularize(self) end |
#split(pattern, limit = 0) ⇒ Array<::String>
Split the string with the given pattern
661 662 663 |
# File 'lib/hanami/utils/string.rb', line 661 def split(pattern, limit = 0) @string.split(pattern, limit) end |
#titleize ⇒ Hanami::Utils::String
Use titleize
Return a titleized version of the string
411 412 413 |
# File 'lib/hanami/utils/string.rb', line 411 def titleize self.class.new underscore.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR) end |
#to_s ⇒ ::String Also known as: to_str
Returns a string representation
635 636 637 |
# File 'lib/hanami/utils/string.rb', line 635 def to_s @string end |
#tokenize { ... } ⇒ void
This method returns an undefined value.
It iterates through the tokens and calls the given block. A token is a substring wrapped by ‘()` and separated by `|`.
rubocop:disable Metrics/MethodLength
577 578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/hanami/utils/string.rb', line 577 def tokenize if match = TOKENIZE_REGEXP.match(@string) # rubocop:disable Lint/AssignmentInCondition pre = match.pre_match post = match.post_match tokens = match[1].split(TOKENIZE_SEPARATOR) tokens.each do |token| yield(self.class.new("#{pre}#{token}#{post}")) end else yield(self.class.new(@string)) end nil end |
#underscore ⇒ Hanami::Utils::String
Use underscore
Return a downcased and underscore separated version of the string
Revised version of ‘ActiveSupport::Inflector.underscore` implementation
485 486 487 488 489 490 491 492 |
# File 'lib/hanami/utils/string.rb', line 485 def underscore new_string = gsub(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR) new_string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET) new_string.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET) new_string.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET) new_string.downcase! self.class.new new_string end |