Module: RocketChat::Util

Defined in:
lib/rocket_chat/util.rb

Overview

Rocket.Chat generic utility functions

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ Object

Camelize a string or symbol

Parameters:

  • string (String/Symbol)

    A string or symbol

Returns:

  • a camelized string



49
50
51
# File 'lib/rocket_chat/util.rb', line 49

def camelize(string)
  string.to_s.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }
end

.slice_hash(hash, *keys) ⇒ Object

Slice keys from hash

Parameters:

  • hash (Hash)

    A hash to slice key/value pairs from

  • *keys (Array)

    The keys to be sliced

Returns:

  • Hash filtered by keys



34
35
36
37
38
39
40
41
42
# File 'lib/rocket_chat/util.rb', line 34

def slice_hash(hash, *keys)
  return {} if keys.length.zero?

  new_hash = {}
  hash.each do |key, value|
    new_hash[key] = value if keys.include? key
  end
  new_hash
end

.stringify_hash_keys(hash) ⇒ Object

Stringify symbolized hash keys

Parameters:

  • hash (Hash)

    A string/symbol keyed hash

Returns:

  • Stringified hash



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rocket_chat/util.rb', line 15

def stringify_hash_keys(hash)
  new_hash = {}
  hash.each do |key, value|
    new_hash[key.to_s] =
      if value.is_a? Hash
        stringify_hash_keys value
      else
        value
      end
  end
  new_hash
end