Module: Artifactory::Util
Instance Method Summary collapse
-
#camelize(string) ⇒ String
Convert an underscored string to it’s camelcase equivalent constant.
-
#rename_keys(options, map = {}) ⇒ Hash
Rename a list of keys to the given map.
-
#slice(options, *keys) ⇒ Hash
Slice the given list of options with the given keys.
-
#truncate(string, options = {}) ⇒ Object
Truncate the given string to a certain number of characters.
-
#underscore(string) ⇒ String
Covert the given CaMelCaSeD string to under_score.
Instance Method Details
#camelize(string) ⇒ String
Convert an underscored string to it’s camelcase equivalent constant.
32 33 34 35 36 37 38 |
# File 'lib/artifactory/util.rb', line 32 def camelize(string) string .to_s .split('_') .map { |e| e.capitalize } .join end |
#rename_keys(options, map = {}) ⇒ Hash
Rename a list of keys to the given map.
71 72 73 |
# File 'lib/artifactory/util.rb', line 71 def rename_keys(, map = {}) Hash[.map { |k, v| [map[k] || k, v] }] end |
#slice(options, *keys) ⇒ Hash
Slice the given list of options with the given keys.
86 87 88 89 90 91 |
# File 'lib/artifactory/util.rb', line 86 def slice(, *keys) keys.inject({}) do |hash, key| hash[key] = [key] if [key] hash end end |
#truncate(string, options = {}) ⇒ Object
Truncate the given string to a certain number of characters.
48 49 50 51 52 53 54 55 56 |
# File 'lib/artifactory/util.rb', line 48 def truncate(string, = {}) length = [:length] || 30 if string.length > length string[0..length-3] + '...' else string end end |
#underscore(string) ⇒ String
Covert the given CaMelCaSeD string to under_score. Graciously borrowed from stackoverflow.com/questions/1509915.
14 15 16 17 18 19 20 21 22 |
# File 'lib/artifactory/util.rb', line 14 def underscore(string) string .to_s .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr('-', '_') .downcase end |