Class: Lotus::Utils::String
- Inherits:
-
String
- Object
- String
- Lotus::Utils::String
- Defined in:
- lib/lotus/utils/string.rb
Overview
String on steroids
Constant Summary collapse
- 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
Instance Method Summary collapse
-
#classify ⇒ String
Return a CamelCase version of the string.
-
#demodulize ⇒ String
Return the string without the Ruby namespace of the class.
-
#initialize(string) ⇒ String
constructor
Initialize the string.
-
#tokenize(&blk) ⇒ void
It iterates thru the tokens and calls the given block.
-
#underscore ⇒ String
Return a downcased and underscore separated version of the string.
Constructor Details
#initialize(string) ⇒ String
Initialize the string
23 24 25 |
# File 'lib/lotus/utils/string.rb', line 23 def initialize(string) super(string.to_s) end |
Instance Method Details
#classify ⇒ String
Return a CamelCase version of the string
38 39 40 |
# File 'lib/lotus/utils/string.rb', line 38 def classify split('_').map {|token| token.slice(0).upcase + token.slice(1..-1) }.join end |
#demodulize ⇒ String
Return the string without the Ruby namespace of the class
77 78 79 |
# File 'lib/lotus/utils/string.rb', line 77 def demodulize split(NAMESPACE_SEPARATOR).last end |
#tokenize(&blk) ⇒ void
This method returns an undefined value.
It iterates thru the tokens and calls the given block. A token is a substring wrapped by ‘()` and separated by `|`.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/lotus/utils/string.rb', line 101 def tokenize(&blk) template = gsub(/\((.*)\)/, "%{token}") tokens = Array(( $1 || self ).split('|')) tokens.each do |token| blk.call(template % {token: token}) end nil end |
#underscore ⇒ String
Return a downcased and underscore separated version of the string
Revised version of ‘ActiveSupport::Inflector.underscore` implementation
56 57 58 59 60 61 |
# File 'lib/lotus/utils/string.rb', line 56 def underscore gsub(NAMESPACE_SEPARATOR, '/'). gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). downcase end |