Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/core_ext/string.rb

Overview

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Rails defines this for a number of other classes, including Object see activesupport/lib/active_support/core_ext/object/blank.rb

Returns:

  • (Boolean)


20
21
22
# File 'lib/deltacloud/core_ext/string.rb', line 20

def blank?
    self !~ /\S/
end

#camelize(lowercase_first_letter = nil) ⇒ Object



57
58
59
60
# File 'lib/deltacloud/core_ext/string.rb', line 57

def camelize(lowercase_first_letter=nil)
  s = split('_').map { |w| w.capitalize }.join
  lowercase_first_letter ? s.uncapitalize : s
end

#convert_query_params(params = {}) ⇒ Object



80
81
82
83
# File 'lib/deltacloud/core_ext/string.rb', line 80

def convert_query_params(params={})
  gsub(/:(\w+)/) { |p| params.delete(p[1..-1].to_sym) } +
    params.to_query_params
end

#pluralizeObject



35
36
37
38
39
40
# File 'lib/deltacloud/core_ext/string.rb', line 35

def pluralize
  return self + 'es' if self =~ /ess$/
  return self[0, self.length-1] + "ies" if self =~ /ty$/
  return self if self =~ /data$/
  self + "s"
end

#remove_matrix_paramsObject



76
77
78
# File 'lib/deltacloud/core_ext/string.rb', line 76

def remove_matrix_params
  self.gsub(/;([^\/]*)/, '').gsub(/\?(.*)$/, '')
end

#singularizeObject



42
43
44
45
46
# File 'lib/deltacloud/core_ext/string.rb', line 42

def singularize
  return self.gsub(/ies$/, 'y') if self =~ /ies$/
  return self.gsub(/es$/, '') if self =~ /sses$/
  self.gsub(/s$/, '')
end

#titlecaseObject

Title case.

"this is a string".titlecase
=> "This Is A String"

CREDIT: Eliazar Parra Copied from facets



31
32
33
# File 'lib/deltacloud/core_ext/string.rb', line 31

def titlecase
  gsub(/\b\w/){ $`[-1,1] == "'" ? $& : $&.upcase }
end

#truncate(length = 10) ⇒ Object



70
71
72
73
74
# File 'lib/deltacloud/core_ext/string.rb', line 70

def truncate(length = 10)
  return self if self.length <= length
  end_string = "...#{self[(self.length-(length/2))..self.length]}"
  "#{self[0..(length/2)]}#{end_string}"
end

#uncapitalizeObject



62
63
64
# File 'lib/deltacloud/core_ext/string.rb', line 62

def uncapitalize
  self[0, 1].downcase + self[1..-1]
end

#underscoreObject



48
49
50
51
52
53
54
55
# File 'lib/deltacloud/core_ext/string.rb', line 48

def underscore
  return self.downcase if self =~ /VSPs$/i
  gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end

#upcase_firstObject



66
67
68
# File 'lib/deltacloud/core_ext/string.rb', line 66

def upcase_first
  self[0, 1].upcase + self[1..-1]
end