Module: CircuitApi::Utils::Attributes
- Defined in:
- lib/circuit_api/utils/attributes.rb
Class Method Summary collapse
- .camelize(str, uppercase_first_letter = true) ⇒ Object
- .camelize_keys(value) ⇒ Object
- .snake_hash_keys(hash) ⇒ Object
- .snake_keys(value) ⇒ Object
- .underscore(string) ⇒ Object
- .underscore_key(key) ⇒ Object
Class Method Details
.camelize(str, uppercase_first_letter = true) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/circuit_api/utils/attributes.rb', line 48 def self.camelize(str, uppercase_first_letter = true) if uppercase_first_letter str = str.sub(/^[a-z\d]*/) { $&.capitalize } else str = str.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase } end str.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::') end |
.camelize_keys(value) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/circuit_api/utils/attributes.rb', line 37 def self.camelize_keys(value) return value unless value.is_a?(Hash) value.keys.each do |k| new_key = camelize(k.to_s, false) new_key = new_key.to_sym if k.is_a? Symbol value[k].map { |current_array| self.camelize_keys(current_array) } if value[k].is_a?(Array) value[new_key] = value.delete(k) end value end |
.snake_hash_keys(hash) ⇒ Object
15 16 17 |
# File 'lib/circuit_api/utils/attributes.rb', line 15 def self.snake_hash_keys(hash) Hash[hash.map { |k, v| [underscore_key(k).to_sym, snake_keys(v)] }] end |
.snake_keys(value) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/circuit_api/utils/attributes.rb', line 4 def self.snake_keys(value) case value when Hash snake_hash_keys(value) when Array value.map { |v| snake_keys(v) } else value end end |
.underscore(string) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/circuit_api/utils/attributes.rb', line 29 def self.underscore(string) string.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr("-", "_") .downcase end |
.underscore_key(key) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/circuit_api/utils/attributes.rb', line 19 def self.underscore_key(key) if key.is_a?(Symbol) underscore(key.to_s).to_sym elsif key.is_a?(String) underscore(key) else key end end |