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

camelize(input) click to toggle source

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
classify(input) click to toggle source

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
dasherize(input) click to toggle source

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
demodulize(input) click to toggle source

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
namespace(input) click to toggle source

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
new(string) click to toggle source

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
pluralize(input) click to toggle source

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
rsub(input, pattern, replacement) click to toggle source

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
singularize(input) click to toggle source

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
titleize(input) click to toggle source

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
underscore(input) click to toggle source

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

==(other) click to toggle source

Equality

@return [TrueClass,FalseClass]

@since 0.0.1

# File lib/hugger/string.rb, line 480
def ==(other)
  to_s == other
end
Also aliased as: eql?
camelize() click to toggle source

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
classify() click to toggle source

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
dasherize() click to toggle source

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
demodulize() click to toggle source

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
eql?(other)
Alias for: ==
gsub(pattern, replacement = nil, &blk) click to toggle source

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
hash() click to toggle source

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() click to toggle source

inspect

@api private @since 0.0.1

# File lib/hugger/string.rb, line 602
def inspect
  object.inspect
end
method_missing(method_name, *args, &blk) click to toggle source

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
namespace() click to toggle source

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
object() click to toggle source

Private internal interface for Hugger::Object

@api private @since 0.0.1

# File lib/hugger/string.rb, line 594
def object
  @string
end
pluralize() click to toggle source

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
respond_to_missing?(method_name, include_private = false) click to toggle source

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
rsub(pattern, replacement) click to toggle source

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
scan(pattern, &blk) click to toggle source

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
singularize() click to toggle source

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(pattern, limit = 0) click to toggle source

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
titleize() click to toggle source

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
to_s() click to toggle source

Returns a string representation

@return [::String]

@since 0.0.1

# File lib/hugger/string.rb, line 469
def to_s
  @string
end
Also aliased as: to_str
to_str()
Alias for: to_s
tokenize() { |class.new("#{pre}#{token}#{post}")| ... } click to toggle source

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
underscore() click to toggle source

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