Class: Hugger::String
Overview
String
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
- 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
/\((.*)\)/- 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
- 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
'_'.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
Constants included from Object
Class Method Summary collapse
-
.camelize(input) ⇒ ::String
Return a camelize version of the string.
-
.classify(input) ⇒ ::String
Return a CamelCase version of the string.
-
.dasherize(input) ⇒ ::String
Return a downcased and dash separated version of the string.
-
.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
Return a pluralized version of self.
-
.rsub(input, pattern, replacement) ⇒ ::String
Replace the rightmost match of
patternwithreplacement. -
.singularize(input) ⇒ ::String
Return a singularized version of self.
-
.titleize(input) ⇒ ::String
Return a titleized version of the string.
-
.underscore(input) ⇒ ::String
Return a downcased and underscore separated version of the string.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
(also: #eql?)
Equality.
-
#camelize ⇒ ::String
Return a camelize version of the string.
-
#classify ⇒ Hugger::String
Return a CamelCase version of the string.
-
#dasherize ⇒ Hugger::String
Return a downcased and dash separated version of the string.
-
#demodulize ⇒ Hugger::String
Return the string without the Ruby namespace of the class.
-
#gsub(pattern, replacement = nil, &blk) ⇒ Hugger::String
Replace the given pattern with the given replacement.
-
#hash ⇒ Integer
Returns the hash of the internal string.
-
#initialize(string) ⇒ Hugger::String
constructor
Initialize the string.
-
#inspect ⇒ Object
private
inspect.
-
#method_missing(method_name, *args, &blk) ⇒ Object
private
Override Ruby's method_missing in order to provide ::String interface.
-
#namespace ⇒ Hugger::String
Return the top level namespace name.
-
#object ⇒ Object
private
Private internal interface for Hugger::Object.
-
#pluralize ⇒ Hugger::String
private
Return a pluralized version of self.
-
#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) ⇒ Hugger::String
Replace the rightmost match of
patternwithreplacement. -
#scan(pattern, &blk) ⇒ Array<Hugger::String>
Iterate through the string, matching the pattern.
-
#singularize ⇒ Hugger::String
private
Return a singularized version of self.
-
#split(pattern, limit = 0) ⇒ Array<Hugger::String>
Split the string with the given pattern.
-
#titleize ⇒ Hugger::String
Return a titleized version of the string.
-
#to_s ⇒ ::String
(also: #to_str)
Returns a string representation.
-
#tokenize { ... } ⇒ void
It iterates through the tokens and calls the given block.
-
#underscore ⇒ Hugger::String
Return a downcased and underscore separated version of the string.
Methods included from Object
Constructor Details
#initialize(string) ⇒ Hugger::String
Initialize the string
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
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
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
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
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
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
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.
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.
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.
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
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
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
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
306 307 308 |
# File 'lib/hugger/string.rb', line 306 def camelize self.class.new(Hugger.inflector.camelize(@string)) end |
#classify ⇒ Hugger::String
Return a CamelCase version of the string
321 322 323 |
# File 'lib/hugger/string.rb', line 321 def classify self.class.new(Hugger.inflector.classify(@string)) end |
#dasherize ⇒ Hugger::String
Return a downcased and dash separated version of the string
360 361 362 |
# File 'lib/hugger/string.rb', line 360 def dasherize self.class.new(Hugger.inflector.dasherize(@string)) end |
#demodulize ⇒ Hugger::String
Return the string without the Ruby namespace of the class
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 |
#hash ⇒ Integer
Returns the hash of the internal string
460 461 462 |
# File 'lib/hugger/string.rb', line 460 def hash @string.hash end |
#inspect ⇒ 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.
inspect
602 603 604 |
# File 'lib/hugger/string.rb', line 602 def inspect object.inspect end |
#namespace ⇒ Hugger::String
Return the top level namespace name
396 397 398 |
# File 'lib/hugger/string.rb', line 396 def namespace self.class.new split(NAMESPACE_SEPARATOR).first end |
#object ⇒ 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.
Private internal interface for Hugger::Object
594 595 596 |
# File 'lib/hugger/string.rb', line 594 def object @string end |
#pluralize ⇒ Hugger::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.
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
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.
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 |
#singularize ⇒ Hugger::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.
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 |
#titleize ⇒ Hugger::String
Return a titleized version of the string
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
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 |.
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 |
#underscore ⇒ Hugger::String
Return a downcased and underscore separated version of the string
Revised version of ActiveSupport::Inflector.underscore implementation
339 340 341 |
# File 'lib/hugger/string.rb', line 339 def underscore self.class.new Hugger.inflector.underscore(@string) end |