class Hugger::String
String
@since 0.0.1
Constants
- CAPITALIZE_SEPARATOR
Separator for capitalize
@since 0.0.1 @api private
- CLASSIFY_SEPARATOR
Separator for Ruby file name
@since 0.0.1 @api private
- DASHERIZE_SEPARATOR
Separator for
dasherize
@since 0.0.1 @api private
- EMPTY_STRING
Empty string for
classify
@since 0.0.1 @api private
- NAMESPACE_SEPARATOR
Separator between Ruby namespaces
@since 0.0.1 @api private
- TITLEIZE_SEPARATOR
Separator for
titleize
@since 0.0.1 @api private
- TOKENIZE_REGEXP
Regexp for
tokenize
@since 0.0.1 @api private
- TOKENIZE_SEPARATOR
Separator for
tokenize
@since 0.0.1 @api private
- UNDERSCORE_DIVISION_TARGET
gsub second parameter used in
underscore
@since 0.0.1 @api private
- UNDERSCORE_SEPARATOR
Separator for
underscore
@since 0.0.1 @api private
Public Class Methods
Return a camelize version of the string
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.camelize('hugger utils') # => "Hugger Utils"
# File lib/hugger/string.rb, line 98 def camelize(input) string = ::String.new(input.to_s) new(string).camelize end
Return a CamelCase version of the string
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.classify('hugger_utils') # => 'HuggerUtils'
# File lib/hugger/string.rb, line 115 def classify(input) string = ::String.new(input.to_s) new(string).classify end
Return a downcased and dash separated version of the string
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.dasherize('Hugger Utils') # => 'hugger-utils' Hugger::String.dasherize('hugger_utils') # => 'hugger-utils' Hugger::String.dasherize('HuggerUtils') # => "hugger-utils"
# File lib/hugger/string.rb, line 156 def dasherize(input) string = ::String.new(input.to_s) new(string).dasherize end
Return the string without the Ruby namespace of the class
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.demodulize('Hugger::String') # => 'String' Hugger::String.demodulize('String') # => 'String'
# File lib/hugger/string.rb, line 175 def demodulize(input) string = ::String.new(input.to_s) new(string).demodulize end
Return the top level namespace name
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.namespace('Hugger::String') # => 'Hugger' Hugger::String.namespace('String') # => 'String'
# File lib/hugger/string.rb, line 194 def namespace(input) ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).first end
Initialize the string
@param string [::String, Symbol] the value we want to initialize
@return [Hugger::String] self
@since 0.0.1
# File lib/hugger/string.rb, line 274 def initialize(string) @string = string.to_s end
Return a pluralized version of self.
@param input [::String] the input
@return [::String] the pluralized string.
@since 0.0.1
@see Hugger::Inflector
@example
require 'hugger/string' Hugger::String.pluralize('book') # => 'books'
# File lib/hugger/string.rb, line 212 def pluralize(input) string = ::String.new(input.to_s) new(string).pluralize end
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.
@param input [::String] the input @param pattern [Regexp, ::String] the pattern to find @param replacement [String] the string to replace
@return [::String] the replaced string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.rsub('authors/books/index', %r{/}, '#') # => 'authors/books#index'
# File lib/hugger/string.rb, line 255 def 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
Return a singularized version of self.
@param input [::String] the input
@return [::String] the singularized string.
@since 0.0.1
@see Hugger::Inflector
@example
require 'hugger/string' Hugger::String.singularize('books') # => 'book'
# File lib/hugger/string.rb, line 231 def singularize(input) string = ::String.new(input.to_s) new(string).singularize end
Return a titleized version of the string
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.titleize('hugger utils') # => "Hugger Utils"
# File lib/hugger/string.rb, line 81 def titleize(input) string = new(input.to_s).titlize new(string) end
Return a downcased and underscore separated version of the string
Revised version of `ActiveSupport::Inflector.underscore` implementation @see github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.underscore('HuggerUtils') # => 'hugger_utils'
# File lib/hugger/string.rb, line 135 def underscore(input) string = ::String.new(input.to_s) new(string).underscore end
Public Instance Methods
Equality
@return [TrueClass,FalseClass]
@since 0.0.1
# File lib/hugger/string.rb, line 480 def ==(other) to_s == other end
Return a camelize version of the string
@param input [::String] the input
@return [::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' Hugger::String.new('hugger utils').camelize # => "Hugger Utils"
# File lib/hugger/string.rb, line 306 def camelize self.class.new(Hugger.inflector.camelize(@string)) end
Return a CamelCase version of the string
@return [Hugger::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new 'hugger_utils' string.classify # => 'HuggerUtils'
# File lib/hugger/string.rb, line 321 def classify self.class.new(Hugger.inflector.classify(@string)) end
Return a downcased and dash separated version of the string
@return [Hugger::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new 'Hugger Utils' string.dasherize # => 'hugger-utils' string = Hugger::String.new 'hugger_utils' string.dasherize # => 'hugger-utils' string = Hugger::String.new 'HuggerUtils' string.dasherize # => "hugger-utils"
# File lib/hugger/string.rb, line 360 def dasherize self.class.new(Hugger.inflector.dasherize(@string)) end
Return the string without the Ruby namespace of the class
@return [Hugger::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new 'Hugger::String' string.demodulize # => 'String' string = Hugger::String.new 'String' string.demodulize # => 'String'
# File lib/hugger/string.rb, line 378 def demodulize self.class.new(Hugger.inflector.demodulize(@string)) end
Replace the given pattern with the given replacement
@return [Hugger::String]
@see www.ruby-doc.org/core/String.html#method-i-gsub
@since 0.0.1
# File lib/hugger/string.rb, line 504 def gsub(pattern, replacement = nil, &blk) s = if block_given? @string.gsub(pattern, &blk) else @string.gsub(pattern, replacement) end self.class.new(s) end
Returns the hash of the internal string
@return [Integer]
@since 0.0.1
# File lib/hugger/string.rb, line 460 def hash @string.hash end
inspect
@api private @since 0.0.1
# File lib/hugger/string.rb, line 602 def inspect object.inspect end
Override Ruby's method_missing
in order to provide ::String interface
@api private @since 0.0.1
@raise [NoMethodError] If doesn't respond to the given method rubocop:disable Style/MethodMissing
# File lib/hugger/string.rb, line 566 def method_missing(method_name, *args, &blk) raise NoMethodError, %(undefined method `#{method_name}' for "#{@string}":#{self.class}) unless respond_to_missing?(method_name) s = @string.__send__(method_name, *args, &blk) case s when ::String self.class.new(s) when Array s.map { |p| self.class.new(p) } else s end end
Return the top level namespace name
@return [Hugger::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new 'Hugger::String' string.namespace # => 'Hugger' string = Hugger::String.new 'String' string.namespace # => 'String'
# File lib/hugger/string.rb, line 396 def namespace self.class.new split(NAMESPACE_SEPARATOR).first end
Private internal interface for Hugger::Object
@api private @since 0.0.1
# File lib/hugger/string.rb, line 594 def object @string end
Return a pluralized version of self.
@return [Hugger::String] the pluralized string.
@api private @since 0.0.1
# File lib/hugger/string.rb, line 441 def pluralize self.class.new Hugger.inflector.pluralize(self) end
Override Ruby's respond_to_missing? in order to support ::String interface
@api private @since 0.0.1
# File lib/hugger/string.rb, line 586 def respond_to_missing?(method_name, include_private = false) @string.respond_to?(method_name, include_private) end
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.
@param pattern [Regexp, String] the pattern to find @param replacement [String, Hugger::String] the string to replace
@return [Hugger::String] the replaced string
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new('authors/books/index') result = string.rsub(/\//, '#') puts string # => #<Hugger::String:0x007fdb41233ad8 @string="authors/books/index"> puts result # => #<Hugger::String:0x007fdb41232ed0 @string="authors/books#index">
# File lib/hugger/string.rb, line 549 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
Iterate through the string, matching the pattern. Either return all those patterns, or pass them to the block.
@return [Array<Hugger::String>]
@see www.ruby-doc.org/core/String.html#method-i-scan
@since 0.0.1
# File lib/hugger/string.rb, line 521 def scan(pattern, &blk) @string.scan(pattern, &blk).map { |s| self.class.new(s) } end
Return a singularized version of self.
@return [Hugger::String] the singularized string.
@api private @since 0.0.1
# File lib/hugger/string.rb, line 451 def singularize self.class.new Hugger.inflector.singularize(self) end
Split the string with the given pattern
@return [Array<Hugger::String>]
@see www.ruby-doc.org/core/String.html#method-i-split
@since 0.0.1
# File lib/hugger/string.rb, line 493 def split(pattern, limit = 0) @string.split(pattern, limit).map { |s| self.class.new(s) } end
Return a titleized version of the string
@return [Hugger::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new 'hugger utils' string.titleize # => "Hugger Utils"
# File lib/hugger/string.rb, line 289 def titleize self.class.new underscore.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR) end
Returns a string representation
@return [::String]
@since 0.0.1
# File lib/hugger/string.rb, line 469 def to_s @string end
It iterates through the tokens and calls the given block. A token is a substring wrapped by `()` and separated by `|`.
@yield the block that is called for each token.
@return [void]
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new 'Hugger::(Utils|App)' string.tokenize do |token| puts token end # => 'Hugger' 'Hugger::App'
# File lib/hugger/string.rb, line 420 def tokenize # rubocop:disable Metrics/MethodLength 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
Return a downcased and underscore separated version of the string
Revised version of `ActiveSupport::Inflector.underscore` implementation @see github.com/rails/rails/blob/feaa6e2048fe86bcf07e967d6e47b865e42e055b/activesupport/lib/active_support/inflector/methods.rb#L90
@return [Hugger::String] the transformed string
@since 0.0.1
@example
require 'hugger/string' string = Hugger::String.new 'HuggerUtils' string.underscore # => 'hugger_utils'
# File lib/hugger/string.rb, line 339 def underscore self.class.new Hugger.inflector.underscore(@string) end