Class: Hugger::String

Inherits:
Object
  • Object
show all
Includes:
Object
Defined in:
lib/hugger/string.rb

Overview

String

Since:

  • 0.0.1

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

Since:

  • 0.0.1

''.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

Since:

  • 0.0.1

'::'.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

Since:

  • 0.0.1

/\((.*)\)/
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

Since:

  • 0.0.1

'|'.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

Since:

  • 0.0.1

'/'.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

Since:

  • 0.0.1

'\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

Since:

  • 0.0.1

' '.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 Ruby file name

Since:

  • 0.0.1

'_'.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

Since:

  • 0.0.1

' '.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

Since:

  • 0.0.1

'-'.freeze

Constants included from Object

Object::STRING_MATCHER

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Object

#blank?, #present?

Constructor Details

#initialize(string) ⇒ Hugger::String

Initialize the string

Parameters:

  • string (::String, Symbol)

    the value we want to initialize

Since:

  • 0.0.1



274
275
276
# File 'lib/hugger/string.rb', line 274

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

rubocop:disable Style/MethodMissing

Raises:

  • (NoMethodError)

    If doesn't respond to the given method

Since:

  • 0.0.1



566
567
568
569
570
571
572
573
574
575
576
577
578
# 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

Class Method Details

.camelize(input) ⇒ ::String

Return a camelize version of the string

Examples:

require 'hugger/string'

Hugger::String.camelize('hugger utils') # => "Hugger Utils"

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

Since:

  • 0.0.1



98
99
100
101
# File 'lib/hugger/string.rb', line 98

def camelize(input)
  string = ::String.new(input.to_s)
  new(string).camelize
end

.classify(input) ⇒ ::String

Return a CamelCase version of the string

Examples:

require 'hugger/string'

Hugger::String.classify('hugger_utils') # => 'HuggerUtils'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

Since:

  • 0.0.1



115
116
117
118
# File 'lib/hugger/string.rb', line 115

def classify(input)
  string = ::String.new(input.to_s)
  new(string).classify
end

.dasherize(input) ⇒ ::String

Return a downcased and dash separated version of the string

Examples:

require 'hugger/string'

Hugger::String.dasherize('Hugger Utils') # => 'hugger-utils'

Hugger::String.dasherize('hugger_utils') # => 'hugger-utils'

Hugger::String.dasherize('HuggerUtils') # => "hugger-utils"

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

Since:

  • 0.0.1



156
157
158
159
# File 'lib/hugger/string.rb', line 156

def dasherize(input)
  string = ::String.new(input.to_s)
  new(string).dasherize
end

.demodulize(input) ⇒ ::String

Return the string without the Ruby namespace of the class

Examples:

require 'hugger/string'

Hugger::String.demodulize('Hugger::String') # => 'String'

Hugger::String.demodulize('String') # => 'String'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

Since:

  • 0.0.1



175
176
177
178
# File 'lib/hugger/string.rb', line 175

def demodulize(input)
  string = ::String.new(input.to_s)
  new(string).demodulize
end

.namespace(input) ⇒ ::String

Return the top level namespace name

Examples:

require 'hugger/string'

Hugger::String.namespace('Hugger::String') # => 'Hugger'

Hugger::String.namespace('String') # => 'String'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

Since:

  • 0.0.1



194
195
196
# File 'lib/hugger/string.rb', line 194

def namespace(input)
  ::String.new(input.to_s).split(NAMESPACE_SEPARATOR).first
end

.pluralize(input) ⇒ ::String

Return a pluralized version of self.

Examples:

require 'hugger/string'

Hugger::String.pluralize('book') # => 'books'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the pluralized string.

See Also:

  • Inflector

Since:

  • 0.0.1



212
213
214
215
# File 'lib/hugger/string.rb', line 212

def pluralize(input)
  string = ::String.new(input.to_s)
  new(string).pluralize
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.

Examples:

require 'hugger/string'

Hugger::String.rsub('authors/books/index', %r{/}, '#')
  # => 'authors/books#index'

Parameters:

  • input (::String)

    the input

  • pattern (Regexp, ::String)

    the pattern to find

  • replacement (String)

    the string to replace

Returns:

  • (::String)

    the replaced string

Since:

  • 0.0.1



255
256
257
258
259
260
261
262
263
264
# 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) ⇒ ::String

Return a singularized version of self.

Examples:

require 'hugger/string'

Hugger::String.singularize('books') # => 'book'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the singularized string.

See Also:

  • Inflector

Since:

  • 0.0.1



231
232
233
234
# File 'lib/hugger/string.rb', line 231

def singularize(input)
  string = ::String.new(input.to_s)
  new(string).singularize
end

.titleize(input) ⇒ ::String

Return a titleized version of the string

Examples:

require 'hugger/string'

Hugger::String.titleize('hugger utils') # => "Hugger Utils"

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

Since:

  • 0.0.1



81
82
83
84
# File 'lib/hugger/string.rb', line 81

def titleize(input)
  string = new(input.to_s).titlize
  new(string)
end

.underscore(input) ⇒ ::String

Return a downcased and underscore separated version of the string

Revised version of ActiveSupport::Inflector.underscore implementation

Examples:

require 'hugger/string'

Hugger::String.underscore('HuggerUtils') # => 'hugger_utils'

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

See Also:

Since:

  • 0.0.1



135
136
137
138
# File 'lib/hugger/string.rb', line 135

def underscore(input)
  string = ::String.new(input.to_s)
  new(string).underscore
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass Also known as: eql?

Equality

Returns:

  • (TrueClass, FalseClass)

Since:

  • 0.0.1



480
481
482
# File 'lib/hugger/string.rb', line 480

def ==(other)
  to_s == other
end

#camelize::String

Return a camelize version of the string

Examples:

require 'hugger/string'

Hugger::String.new('hugger utils').camelize # => "Hugger Utils"

Parameters:

  • input (::String)

    the input

Returns:

  • (::String)

    the transformed string

Since:

  • 0.0.1



306
307
308
# File 'lib/hugger/string.rb', line 306

def camelize
  self.class.new(Hugger.inflector.camelize(@string))
end

#classifyHugger::String

Return a CamelCase version of the string

Examples:

require 'hugger/string'

string = Hugger::String.new 'hugger_utils'
string.classify # => 'HuggerUtils'

Returns:

Since:

  • 0.0.1



321
322
323
# File 'lib/hugger/string.rb', line 321

def classify
  self.class.new(Hugger.inflector.classify(@string))
end

#dasherizeHugger::String

Return a downcased and dash separated version of the string

Examples:

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"

Returns:

Since:

  • 0.0.1



360
361
362
# File 'lib/hugger/string.rb', line 360

def dasherize
  self.class.new(Hugger.inflector.dasherize(@string))
end

#demodulizeHugger::String

Return the string without the Ruby namespace of the class

Examples:

require 'hugger/string'

string = Hugger::String.new 'Hugger::String'
string.demodulize # => 'String'

string = Hugger::String.new 'String'
string.demodulize # => 'String'

Returns:

Since:

  • 0.0.1



378
379
380
# File 'lib/hugger/string.rb', line 378

def demodulize
  self.class.new(Hugger.inflector.demodulize(@string))
end

#gsub(pattern, replacement = nil, &blk) ⇒ Hugger::String

Replace the given pattern with the given replacement



504
505
506
507
508
509
510
511
# 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

#hashInteger

Returns the hash of the internal string

Returns:

  • (Integer)

Since:

  • 0.0.1



460
461
462
# File 'lib/hugger/string.rb', line 460

def hash
  @string.hash
end

#inspectObject

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.

inspect

Since:

  • 0.0.1



602
603
604
# File 'lib/hugger/string.rb', line 602

def inspect
  object.inspect
end

#namespaceHugger::String

Return the top level namespace name

Examples:

require 'hugger/string'

string = Hugger::String.new 'Hugger::String'
string.namespace # => 'Hugger'

string = Hugger::String.new 'String'
string.namespace # => 'String'

Returns:

Since:

  • 0.0.1



396
397
398
# File 'lib/hugger/string.rb', line 396

def namespace
  self.class.new split(NAMESPACE_SEPARATOR).first
end

#objectObject

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.

Private internal interface for Hugger::Object

Since:

  • 0.0.1



594
595
596
# File 'lib/hugger/string.rb', line 594

def object
  @string
end

#pluralizeHugger::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.

Returns:

Since:

  • 0.0.1



441
442
443
# 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) ⇒ 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

Returns:

  • (Boolean)

Since:

  • 0.0.1



586
587
588
# 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) ⇒ Hugger::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.

Examples:

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">

Parameters:

Returns:

Since:

  • 0.0.1



549
550
551
552
553
554
555
556
557
# 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) ⇒ Array<Hugger::String>

Iterate through the string, matching the pattern. Either return all those patterns, or pass them to the block.



521
522
523
# File 'lib/hugger/string.rb', line 521

def scan(pattern, &blk)
  @string.scan(pattern, &blk).map { |s| self.class.new(s) }
end

#singularizeHugger::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.

Returns:

Since:

  • 0.0.1



451
452
453
# File 'lib/hugger/string.rb', line 451

def singularize
  self.class.new Hugger.inflector.singularize(self)
end

#split(pattern, limit = 0) ⇒ Array<Hugger::String>

Split the string with the given pattern



493
494
495
# File 'lib/hugger/string.rb', line 493

def split(pattern, limit = 0)
  @string.split(pattern, limit).map { |s| self.class.new(s) }
end

#titleizeHugger::String

Return a titleized version of the string

Examples:

require 'hugger/string'

string = Hugger::String.new 'hugger utils'
string.titleize # => "Hugger Utils"

Returns:

Since:

  • 0.0.1



289
290
291
# File 'lib/hugger/string.rb', line 289

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

Returns:

  • (::String)

Since:

  • 0.0.1



469
470
471
# File 'lib/hugger/string.rb', line 469

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 |.

Examples:

require 'hugger/string'

string = Hugger::String.new 'Hugger::(Utils|App)'
string.tokenize do |token|
  puts token
end

# =>
  'Hugger'
  'Hugger::App'

Yields:

  • the block that is called for each token.

Since:

  • 0.0.1



420
421
422
423
424
425
426
427
428
429
430
431
432
433
# 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

#underscoreHugger::String

Return a downcased and underscore separated version of the string

Revised version of ActiveSupport::Inflector.underscore implementation

Examples:

require 'hugger/string'

string = Hugger::String.new 'HuggerUtils'
string.underscore # => 'hugger_utils'

Returns:

See Also:

Since:

  • 0.0.1



339
340
341
# File 'lib/hugger/string.rb', line 339

def underscore
  self.class.new Hugger.inflector.underscore(@string)
end