Class: Lotus::Utils::String

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/utils/string.rb

Overview

String on steroids

Since:

  • 0.1.0

Direct Known Subclasses

PathPrefix

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

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

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

Since:

  • 0.3.0

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

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

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

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

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

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

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

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

Since:

  • 0.3.4

/#{CLASSIFY_SEPARATOR}|#{NAMESPACE_SEPARATOR}|#{UNDERSCORE_SEPARATOR}|#{DASHERIZE_SEPARATOR}/

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ String

Initialize the string

Parameters:

  • string (::String, Symbol)

    the value we want to initialize

Since:

  • 0.1.0



82
83
84
# File 'lib/lotus/utils/string.rb', line 82

def initialize(string)
  @string = string.to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *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

Raises:

  • (NoMethodError)

    If doesn’t respond to the given method

Since:

  • 0.3.0



400
401
402
403
404
405
406
407
408
# File 'lib/lotus/utils/string.rb', line 400

def method_missing(m, *args, &blk)
  if respond_to?(m)
    s = @string.__send__(m, *args, &blk)
    s = self.class.new(s) if s.is_a?(::String)
    s
  else
    raise NoMethodError.new(%(undefined method `#{ m }' for "#{ @string }":#{ self.class }))
  end
end

Instance Method Details

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

Equality

Returns:

  • (TrueClass, FalseClass)

Since:

  • 0.3.0



317
318
319
# File 'lib/lotus/utils/string.rb', line 317

def ==(other)
  to_s == other
end

#capitalizeLotus::Utils::String

Return a capitalized version of the string

Examples:

require 'lotus/utils/string'

string = Lotus::Utils::String.new 'lotus'
string.capitalize # => "Lotus"

string = Lotus::Utils::String.new 'lotus utils'
string.capitalize # => "Lotus utils"

string = Lotus::Utils::String.new 'Lotus Utils'
string.capitalize # => "Lotus utils"

string = Lotus::Utils::String.new 'lotus_utils'
string.capitalize # => "Lotus utils"

string = Lotus::Utils::String.new 'lotus-utils'
string.capitalize # => "Lotus utils"

Returns:

Since:

  • 0.5.2



124
125
126
127
128
129
130
# File 'lib/lotus/utils/string.rb', line 124

def capitalize
  head, *tail = underscore.split(CLASSIFY_SEPARATOR)

  self.class.new(
    tail.unshift(head.capitalize).join(CAPITALIZE_SEPARATOR)
  )
end

#classifyString

Return a CamelCase version of the string

Examples:

require 'lotus/utils/string'

string = Lotus::Utils::String.new 'lotus_utils'
string.classify # => 'LotusUtils'

Returns:

  • (String)

    the transformed string

Since:

  • 0.1.0



143
144
145
146
147
148
149
150
151
152
# File 'lib/lotus/utils/string.rb', line 143

def classify
  words = split(CLASSIFY_WORD_SEPARATOR).map!(&:capitalize)
  delimiters = scan(CLASSIFY_WORD_SEPARATOR)

  delimiters.map! do |delimiter|
    delimiter == CLASSIFY_SEPARATOR ? EMPTY_STRING : NAMESPACE_SEPARATOR
  end

  self.class.new words.zip(delimiters).join
end

#dasherizeLotus::Utils::String

Return a downcased and dash separated version of the string

Examples:

require 'lotus/utils/string'

string = Lotus::Utils::String.new 'Lotus Utils'
string.dasherize # => 'lotus-utils'

string = Lotus::Utils::String.new 'lotus_utils'
string.dasherize # => 'lotus-utils'

string = Lotus::Utils::String.new 'LotusUtils'
string.dasherize # => "lotus-utils"

Returns:

Since:

  • 0.4.0



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

def dasherize
  self.class.new underscore.split(CLASSIFY_SEPARATOR).join(DASHERIZE_SEPARATOR)
end

#demodulizeString

Return the string without the Ruby namespace of the class

Examples:

require 'lotus/utils/string'

string = Lotus::Utils::String.new 'Lotus::Utils::String'
string.demodulize # => 'String'

string = Lotus::Utils::String.new 'String'
string.demodulize # => 'String'

Returns:

  • (String)

    the transformed string

Since:

  • 0.1.0



212
213
214
# File 'lib/lotus/utils/string.rb', line 212

def demodulize
  self.class.new split(NAMESPACE_SEPARATOR).last
end

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

Replace the given pattern with the given replacement

Returns:

See Also:

Since:

  • 0.3.0



341
342
343
344
345
346
347
# File 'lib/lotus/utils/string.rb', line 341

def gsub(pattern, replacement = nil, &blk)
  if block_given?
    @string.gsub(pattern, &blk)
  else
    @string.gsub(pattern, replacement)
  end
end

#hashFixnum

Returns the hash of the internal string

Returns:

  • (Fixnum)

Since:

  • 0.3.0



297
298
299
# File 'lib/lotus/utils/string.rb', line 297

def hash
  @string.hash
end

#namespaceString

Return the top level namespace name

Examples:

require 'lotus/utils/string'

string = Lotus::Utils::String.new 'Lotus::Utils::String'
string.namespace # => 'Lotus'

string = Lotus::Utils::String.new 'String'
string.namespace # => 'String'

Returns:

  • (String)

    the transformed string

Since:

  • 0.1.2



230
231
232
# File 'lib/lotus/utils/string.rb', line 230

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

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

Returns:

See Also:

Since:

  • 0.4.1



276
277
278
# File 'lib/lotus/utils/string.rb', line 276

def pluralize
  self.class.new Inflector.pluralize(self)
end

#respond_to_missing?(m, 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:

Since:

  • 0.3.0



414
415
416
# File 'lib/lotus/utils/string.rb', line 414

def respond_to_missing?(m, include_private=false)
  @string.respond_to?(m, include_private)
end

#rsub(pattern, replacement) ⇒ Lotus::Utils::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 'lotus/utils/string'

string = Lotus::Utils::String.new('authors/books/index')
result = string.rsub(/\//, '#')

puts string
  # => #<Lotus::Utils::String:0x007fdb41233ad8 @string="authors/books/index">

puts result
  # => #<Lotus::Utils::String:0x007fdb41232ed0 @string="authors/books#index">

Parameters:

Returns:

Since:

  • 0.6.0



384
385
386
387
388
389
390
391
392
# File 'lib/lotus/utils/string.rb', line 384

def rsub(pattern, replacement)
  if i = rindex(pattern)
    s    = @string.dup
    s[i] = replacement
    self.class.new s
  else
    self
  end
end

#scan(pattern, &blk) ⇒ String?

Both forms iterate through str, matching the pattern

Returns:

See Also:

Since:

  • 0.6.0



356
357
358
# File 'lib/lotus/utils/string.rb', line 356

def scan(pattern, &blk)
  @string.scan(pattern, &blk)
end

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

Returns:

See Also:

Since:

  • 0.4.1



288
289
290
# File 'lib/lotus/utils/string.rb', line 288

def singularize
  self.class.new Inflector.singularize(self)
end

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

Split the string with the given pattern

Returns:

See Also:

Since:

  • 0.3.0



330
331
332
# File 'lib/lotus/utils/string.rb', line 330

def split(pattern, limit = 0)
  @string.split(pattern, limit)
end

#titleizeLotus::Utils::String

Return a titleized version of the string

Examples:

require 'lotus/utils/string'

string = Lotus::Utils::String.new 'lotus utils'
string.titleize # => "Lotus Utils"

Returns:

Since:

  • 0.4.0



97
98
99
# File 'lib/lotus/utils/string.rb', line 97

def titleize
  self.class.new underscore.split(CLASSIFY_SEPARATOR).map(&:capitalize).join(TITLEIZE_SEPARATOR)
end

#to_sString Also known as: to_str

Returns a string representation

Returns:

Since:

  • 0.3.0



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

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 'lotus/utils/string'

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

# =>
  'Lotus::Utils'
  'Lotus::App'

Yields:

  • the block that is called for each token.

Since:

  • 0.1.0



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/lotus/utils/string.rb', line 254

def tokenize
  if match = TOKENIZE_REGEXP.match(@string)
    pre, post = match.pre_match, 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

#underscoreString

Return a downcased and underscore separated version of the string

Revised version of ‘ActiveSupport::Inflector.underscore` implementation

Examples:

require 'lotus/utils/string'

string = Lotus::Utils::String.new 'LotusUtils'
string.underscore # => 'lotus_utils'

Returns:

  • (String)

    the transformed string

See Also:

Since:

  • 0.1.0



168
169
170
171
172
173
174
175
# File 'lib/lotus/utils/string.rb', line 168

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