Class: String
- Defined in:
- lib/gorillib/object/blank.rb,
lib/gorillib/string/human.rb,
lib/gorillib/string/truncate.rb,
lib/gorillib/string/constantize.rb,
lib/gorillib/string/inflections.rb
Overview
class FalseClass
Direct Known Subclasses
Instance Method Summary collapse
-
#blank? ⇒ TrueClass, FalseClass
Strips out whitespace then tests if the string is empty.
- #camelize(*args) ⇒ Object
-
#constantize ⇒ Module, Class
Find a declared constant with the name specified in the string, or raise.
- #demodulize(*args) ⇒ Object
-
#humanize ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", if any.
-
#safe_constantize ⇒ Module, Class
Find a declared constant with the name specified in the string, or return nil.
- #snakeize(*args) ⇒ Object
-
#titleize ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
-
#truncate(length, options = {}) ⇒ Object
Truncates a given +text+ after a given length if +text+ is longer than length:.
- #underscore(*args) ⇒ Object
Instance Method Details
#blank? ⇒ TrueClass, FalseClass
Strips out whitespace then tests if the string is empty.
"".blank? #=> true " ".blank? #=> true " hey ho ".blank? #=> false
93 94 95 |
# File 'lib/gorillib/object/blank.rb', line 93 def blank? strip.empty? end |
#camelize(*args) ⇒ Object
4 |
# File 'lib/gorillib/string/inflections.rb', line 4 def camelize(*args) Gorillib::Inflector.camelize(self, *args) ; end |
#constantize ⇒ Module, Class
Find a declared constant with the name specified in the string, or raise.
14 15 16 |
# File 'lib/gorillib/string/constantize.rb', line 14 def constantize Gorillib::Inflector.constantize(self) end |
#demodulize(*args) ⇒ Object
7 |
# File 'lib/gorillib/string/inflections.rb', line 7 def demodulize(*args) Gorillib::Inflector.demodulize(self, *args) ; end |
#humanize ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips a trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
12 13 14 |
# File 'lib/gorillib/string/human.rb', line 12 def humanize self.gsub(/_id$/, '').tr('_', ' ').capitalize end |
#safe_constantize ⇒ Module, Class
Find a declared constant with the name specified in the string, or return nil.
CamelCase or is not initialized.
31 32 33 |
# File 'lib/gorillib/string/constantize.rb', line 31 def safe_constantize Gorillib::Inflector.safe_constantize(self) end |
#snakeize(*args) ⇒ Object
5 |
# File 'lib/gorillib/string/inflections.rb', line 5 def snakeize(*args) Gorillib::Inflector.snakeize(self, *args) ; end |
#titleize ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. +titleize+ is meant for creating pretty output. It is not used in the Rails internals.
Examples: "man from the boondocks".titleize # => "Man From The Boondocks" "x-men: the last stand".titleize # => "X Men: The Last Stand"
23 24 25 |
# File 'lib/gorillib/string/human.rb', line 23 def titleize self.underscore.humanize.gsub(/\b('?[a-z])/){ $1.capitalize } end |
#truncate(length, options = {}) ⇒ Object
Truncates a given +text+ after a given length if +text+ is longer than length:
"Once upon a time in a world far far away".truncate(27) # => "Once upon a time in a wo..."
The last characters will be replaced with the :omission string (defaults to "...") for a total length not exceeding :length:
"Once upon a time in a world far far away".truncate(27, :separator => ' ') # => "Once upon a time in a..."
Pass a :separator to truncate +text+ at a natural break:
"And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)") # => "And they f... (continued)"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gorillib/string/truncate.rb', line 17 def truncate(length, = {}) text = self.dup chars = text.respond_to?(:mb_chars) ? text.mb_chars : text omission = [:omission] || "..." omission_len = omission.respond_to?(:mb_chars) ? omission.mb_chars.length : omission.length length_with_room_for_omission = length - omission_len if (separator = [:separator]) separator_chars = separator.respond_to?(:mb_chars) ? separator.to_s.mb_chars : separator.to_s stop = chars.rindex(separator_chars, length_with_room_for_omission) || length_with_room_for_omission else stop = length_with_room_for_omission end (chars.length > length ? chars[0...stop] + omission : text).to_s end |
#underscore(*args) ⇒ Object
6 |
# File 'lib/gorillib/string/inflections.rb', line 6 def underscore(*args) Gorillib::Inflector.underscore(self, *args) ; end |