Class: String

Inherits:
Object show all
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

Binary, Gorillib::Model::Name, Guid, Hostname, IpAddress

Instance Method Summary collapse

Instance Method Details

#blank?TrueClass, FalseClass

Strips out whitespace then tests if the string is empty.

"".blank? #=> true " ".blank? #=> true " hey ho ".blank? #=> false

Returns:



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

#constantizeModule, Class

Find a declared constant with the name specified in the string, or raise.

Examples:

"Module".constantize  # => Module
"Class".constantize   # => Class
"blargle".constantize # => NameError: wrong constant name blargle

Returns:

Raises:

  • (NameError)

    when the name is not in CamelCase or is not initialized.

See Also:



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

#humanizeObject

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.

Examples:

"employee_salary" #=> "Employee salary"
"author_id" #=> "Author"


12
13
14
# File 'lib/gorillib/string/human.rb', line 12

def humanize
  self.gsub(/_id$/, '').tr('_', ' ').capitalize
end

#safe_constantizeModule, Class

Find a declared constant with the name specified in the string, or return nil.

CamelCase or is not initialized.

Examples:

"Module".safe_constantize  # => Module
"Class".safe_constantize   # => Class
"blargle".safe_constantize # => nil

Returns:

  • (Module, Class)

    the specified class, or nil when the name is not in

  • (Module, Class)

    the specified constant, or nil when the name is not in CamelCase or is not initialized.

See Also:

  • Gorillib::Model::Inflector.safe_constantize


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

#titleizeObject

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, options = {})
  text = self.dup
  chars        = text.respond_to?(:mb_chars)      ? text.mb_chars            : text
  omission     = options[:omission] || "..."
  omission_len = omission.respond_to?(:mb_chars)  ? omission.mb_chars.length : omission.length
  length_with_room_for_omission = length - omission_len

  if (separator = options[: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