Class: String

Inherits:
Object show all
Defined in:
lib/base/helpers/support.rb

Overview

– Copyright © 2013 RightScale, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Instance Method Summary collapse

Instance Method Details

#_arrayifyArray

Wraps the string into an array.

Examples:

'hahaha'._arrayify #=> ['hahaha']

Returns:



85
86
87
# File 'lib/base/helpers/support.rb', line 85

def _arrayify
  [ self ]
end

#_blank?Boolean

Returns true is the string has zero length or contains spaces only. And it returns false if the string has any meaningful value.

Returns:

  • (Boolean)


94
95
96
# File 'lib/base/helpers/support.rb', line 94

def _blank?
  empty? || strip.empty?
end

#_camelize(lower_case = false) ⇒ String Also known as: _camel_case

Camelizes the string.

Examples:

'hello_world'._camelize       #=> 'HelloWorld'
'hello_world'._camelize(true) #=> 'helloWorld'
'HelloWorld'._camelize        #=> 'HelloWorld'

Parameters:

  • lower_case (Boolean) (defaults to: false)

    When set to true it downcases the very first symbol of the string.

Returns:

  • (String)

    The camelized string value.



54
55
56
57
58
59
60
61
# File 'lib/base/helpers/support.rb', line 54

def _camelize(lower_case = false)
  words = self.gsub(/([A-Z])/, '_\1').
               split(/_|\b/).
               map{ |word| word.capitalize }.
               reject{ |word| word == '' }
  words[0] = words[0].downcase if words[0] && lower_case
  words.join('')
end

#_constantizeClass, Module

Constantizes the string.

Examples:

"Module"._constantize #=> Module
"Class"._constantize  #=> Class

Returns:

  • (Class, Module)

    The constantized class/module.

Raises:

  • (NameError)

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



36
37
38
39
40
41
# File 'lib/base/helpers/support.rb', line 36

def _constantize
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
    fail(::NameError, "#{self.inspect} is not a valid constant name!")
  end
  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end

#_snake_caseString Also known as: _underscore

Underscorizes the string.

Examples:

'HelloWorld'._underscore #=> 'hello_world'

Returns:

  • (String)

    The camelized string value.



71
72
73
74
75
# File 'lib/base/helpers/support.rb', line 71

def _snake_case
  self.split(/\b/).
       map{ |word| word.gsub(/[A-Z]/){ |match| "#{$`=='' ? '' : '_'}#{match.downcase}" } }.
       join('')
end