lib/rocket_chat/util.rb in rocketchat-0.0.2 vs lib/rocket_chat/util.rb in rocketchat-0.0.3

- old
+ new

@@ -7,14 +7,39 @@ # Stringify symbolized hash keys # @param [Hash] hash A string/symbol keyed hash # @return Stringified hash # def stringify_hash_keys(hash) - newhash = {} + new_hash = {} + hash.each { |key, value| new_hash[key.to_s] = value } + new_hash + end + module_function :stringify_hash_keys + + # + # Slice keys from hash + # @param [Hash] hash A hash to slice key/value pairs from + # @param [Array] *keys The keys to be sliced + # @return Hash filtered by keys + # + def slice_hash(hash, *keys) + return {} if keys.length.zero? + + new_hash = {} hash.each do |key, value| - newhash[key.to_s] = value + new_hash[key] = value if keys.include? key end - newhash + new_hash end - module_function :stringify_hash_keys + module_function :slice_hash + + # + # Camelize a string or symbol + # @param [String/Symbol] string A string or symbol + # @return a camelized string + # + def camelize(string) + string.to_s.gsub(/_([a-z])/) { $1.upcase } + end + module_function :camelize end end