Class: Reach::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/reach/helper.rb

Class Method Summary collapse

Class Method Details

.convert_keys(value) ⇒ Object

Public: Converts a hash or array’s keys from CamelCase to snake_case

value - the hash or array to have it’s keys converted

Example:

Reach::Helper::to_snake_case({:hashKey => "hashValue"})
#=> {:hash_key => "hashValue"}

Returns the hash/array with snake_case’d keys



38
39
40
41
42
43
44
45
46
47
# File 'lib/reach/helper.rb', line 38

def self.convert_keys(value)
  case value
    when Array
      value.map(&method(:convert_keys))
    when Hash
      Hash[value.map { |k, v| [to_snake_case(k), convert_keys(v)] }]
    else
      value
  end
end

.to_snake_case(value) ⇒ Object

Public: Converts a string or key from CamelCase to snake_case

value - the string or key to be converted

Example:

Reach::Helper::to_snake_case("CamelCase")
#=> "camel_case"

Reach::Helper::to_snake_case(:CamelCase)
#=> :camel_case

Returns the snake_cased string or key



17
18
19
20
21
22
23
24
25
26
# File 'lib/reach/helper.rb', line 17

def self.to_snake_case(value)
  case value
    when Symbol
      lowercase(value.to_s).to_sym
    when String
      lowercase(value)
    else
      false
  end
end