module Artifactory::Util
Public Instance Methods
camelize(string, lowercase = false)
click to toggle source
Convert an underscored string to it's camelcase equivalent constant.
@param [String] string
the string to convert
@return [String]
# File lib/artifactory/util.rb, line 48 def camelize(string, lowercase = false) result = string .to_s .split("_") .map(&:capitalize) .join if lowercase result[0, 1].downcase + result[1..-1] else result end end
rename_keys(options, map = {})
click to toggle source
Rename a list of keys to the given map.
@example Rename the given keys
rename_keys(hash, foo: :bar, zip: :zap)
@param [Hash] options
the options to map
@param [Hash] map
the map of keys to map
@return [Hash]
# File lib/artifactory/util.rb, line 93 def rename_keys(options, map = {}) Hash[options.map { |k, v| [map[k] || k, v] }] end
slice(options, *keys)
click to toggle source
Slice the given list of options with the given keys.
@param [Hash] options
the list of options to slice
@param [Array<Object>] keys
the keys to slice
@return [Hash]
the sliced hash
# File lib/artifactory/util.rb, line 108 def slice(options, *keys) keys.inject({}) do |hash, key| hash[key] = options[key] if options[key] hash end end
to_type(string)
click to toggle source
# 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) string end
truncate(string, options = {})
click to toggle source
Truncate the given string to a certain number of characters.
@param [String] string
the string to truncate
@param [Hash] options
the list of options (such as +length+)
# File lib/artifactory/util.rb, line 70 def truncate(string, options = {}) length = options[:length] || 30 if string.length > length string[0..length - 3] + "..." else string end end
underscore(string)
click to toggle source
Covert the given CaMelCaSeD string to under_score. Graciously borrowed from stackoverflow.com/questions/1509915.
@param [String] string
the string to use for transformation
@return [String]
# 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)
click to toggle source
Flatten an xml element with at most one child node with children into a hash.
@param [REXML] element
xml element
# 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
Private Instance Methods
numeric?(string)
click to toggle source
# File lib/artifactory/util.rb, line 153 def numeric?(string) string.to_i.to_s == string || string.to_f.to_s == string end