Class: String

Inherits:
Object show all
Defined in:
lib/amazon/mws/lib/extensions.rb

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter_in_uppercase = true) ⇒ Object

By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.

camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.

Examples:

"active_record".camelize                # => "ActiveRecord"
"active_record".camelize(:lower)        # => "activeRecord"
"active_record/errors".camelize         # => "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) # => "activeRecord::Errors"


28
29
30
31
32
33
34
# File 'lib/amazon/mws/lib/extensions.rb', line 28

def camelize(first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    self.to_s.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
  end
end

#to_booleanObject



15
16
17
# File 'lib/amazon/mws/lib/extensions.rb', line 15

def to_boolean
  (self == "true") ? true : false
end

#underscoreObject



36
37
38
39
40
41
42
# File 'lib/amazon/mws/lib/extensions.rb', line 36

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

#valid_utf8?Boolean

Returns:



3
4
5
# File 'lib/amazon/mws/lib/extensions.rb', line 3

def valid_utf8?
  dup.force_encoding('UTF-8').valid_encoding?
end