module BlazingDocs::Utils

Public Instance Methods

to_camel_keys(value = {}) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
15 def to_camel_keys(value = {})
16   case value
17   when Array
18     value.map { |v| to_camel_keys(v) }
19   when Hash
20     camel_hash(value)
21   else
22     value
23   end
24 end
to_hash(obj) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
26 def to_hash(obj)
27   hash = {}
28   obj.instance_variables.each { |var| hash[var.to_s.delete('@')] = obj.instance_variable_get(var) }
29   hash
30 end
to_snake_keys(value = {}) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
 4 def to_snake_keys(value = {})
 5   case value
 6   when Array
 7     value.map { |v| to_snake_keys(v) }
 8   when Hash
 9     snake_hash(value)
10   else
11     value
12   end
13 end

Private Instance Methods

camel_case_key(key) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
67 def camel_case_key(key)
68   case key
69   when Symbol
70     camel_case_lower(key.to_s).to_sym
71   when String
72     camel_case_lower(key)
73   else
74     key
75   end
76 end
camel_case_lower(string) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
78 def camel_case_lower(string)
79   string.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
80 end
camel_hash(value) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
63 def camel_hash(value)
64   value.map { |k, v| [camel_case_key(k), to_camel_keys(v)] }.to_h
65 end
snake_hash(value) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
34 def snake_hash(value)
35   value.map { |k, v| [underscore_key(k), to_snake_keys(v)] }.to_h
36 end
underscore(string) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
49 def underscore(string)
50   @__memoize_underscore ||= {}
51 
52   return @__memoize_underscore[string] if @__memoize_underscore[string]
53 
54   @__memoize_underscore[string] =
55     string.tr("::", "/")
56           .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
57           .gsub(/([a-z\d])([A-Z])/, '\1_\2')
58           .tr("-", "_")
59           .downcase
60   @__memoize_underscore[string]
61 end
underscore_key(key) click to toggle source
   # File lib/blazingdocs/utils/hash_utils.rb
38 def underscore_key(key)
39   case key
40   when Symbol
41     underscore(key.to_s).to_sym
42   when String
43     underscore(key)
44   else
45     key
46   end
47 end