Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun/ess/extensions.rb

Overview

:stopdoc:

Instance Method Summary collapse

Instance Method Details

#camelizeObject



14
15
16
17
18
19
# File 'lib/aliyun/ess/extensions.rb', line 14

def camelize
  string = to_s.sub(/^[a-z\d]*/) { $&.capitalize }
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
  string.gsub!(/\//, '::')
  string
end

#remove_extendedObject



36
37
38
# File 'lib/aliyun/ess/extensions.rb', line 36

def remove_extended
  dup.remove_extended!
end

#remove_extended!Object

All paths in in ESS have to be valid unicode so this takes care of cleaning up any strings that aren’t valid utf-8 according to String#valid_utf8?



27
28
29
30
31
32
33
34
# File 'lib/aliyun/ess/extensions.rb', line 27

def remove_extended!
  sanitized_string = ''
  each_byte do |byte|
    character = byte.chr
    sanitized_string << character if character.ascii_only?
  end
  sanitized_string
end

#underscoreObject

ActiveSupport adds an underscore method to String so let’s just use that one if we find that the method is already defined



7
8
9
10
11
12
# File 'lib/aliyun/ess/extensions.rb', line 7

def underscore
  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:

  • (Boolean)


21
22
23
# File 'lib/aliyun/ess/extensions.rb', line 21

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