Module: Artifactory::Util
Instance Method Summary collapse
-
#camelize(string, lowercase = false) ⇒ 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.
- #to_type(string) ⇒ Object
-
#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.
-
#xml_to_hash(element, child_with_children = '', unique_children = true) ⇒ Object
Flatten an xml element with at most one child node with children into a hash.
Instance Method Details
#camelize(string, lowercase = false) ⇒ String
Convert an underscored string to it’s camelcase equivalent constant.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/artifactory/util.rb', line 48 def camelize(string, lowercase = false) result = string .to_s .split('_') .map { |e| e.capitalize } .join if lowercase result[0,1].downcase + result[1..-1] else result end end |
#rename_keys(options, map = {}) ⇒ Hash
Rename a list of keys to the given map.
93 94 95 |
# File 'lib/artifactory/util.rb', line 93 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.
108 109 110 111 112 113 |
# File 'lib/artifactory/util.rb', line 108 def slice(, *keys) keys.inject({}) do |hash, key| hash[key] = [key] if [key] hash end end |
#to_type(string) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/artifactory/util.rb', line 143 def to_type(string) return true if string.eql?('true') return false if string.eql?('false') return string.to_i if numeric?(string) return string end |
#truncate(string, options = {}) ⇒ Object
Truncate the given string to a certain number of characters.
70 71 72 73 74 75 76 77 78 |
# File 'lib/artifactory/util.rb', line 70 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.
30 31 32 33 34 35 36 37 38 |
# File 'lib/artifactory/util.rb', line 30 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 |
#xml_to_hash(element, child_with_children = '', unique_children = true) ⇒ Object
Flatten an xml element with at most one child node with children into a hash.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/artifactory/util.rb', line 122 def xml_to_hash(element, child_with_children = '', unique_children = true) properties = {} element.each_element_with_text do |e| if e.name.eql?(child_with_children) if unique_children e.each_element_with_text do |t| properties[t.name] = to_type(t.text) end else children = [] e.each_element_with_text do |t| properties[t.name] = children.push(to_type(t.text)) end end else properties[e.name] = to_type(e.text) end end properties end |