Module: OandaAPI::Utils
- Defined in:
- lib/oanda_api/utils/utils.rb
Overview
A few general purpose useful methods. Intentionally not implemented as monkey patches. Some wheel-reinventing to avoid adding extra dependencies.
Class Method Summary collapse
-
.camelize(s) ⇒ String
Puts camelHumps where you expect them.
-
.classify(s) ⇒ String
Converts a string from snake_case to upper camel case (class name like "MyClass").
-
.pluralize(s) ⇒ String
Naively plops an "s" at the end of a string.
-
.rubyize_keys(hash) ⇒ Hash
Returns a deep copy of a hash with its keys downcased, underscored and symbolized into ruby sweetness.
-
.singularize(s) ⇒ String
Returns a string with its trailing "s" vaporized.
-
.stringify_keys(hash) ⇒ Hash
Returns a deep copy of a hash with its keys camelized, underscored and symbolized.
-
.transform_hash_keys(value, &block) {|Object| ... } ⇒ Hash
Yields all keys of a hash, and safely applies whatever transform the block provides.
-
.transform_hash_values(value, key = nil, &block) ⇒ Hash
Yields all key/value pairs of a hash, and safely applies whatever transform the block provides to the values.
-
.underscore(s) ⇒ String
Converts a string from camelCase to snake_case.
Class Method Details
.camelize(s) ⇒ String
Puts camelHumps where you expect them.
9 10 11 |
# File 'lib/oanda_api/utils/utils.rb', line 9 def self.camelize(s) s.to_s.gsub(/(?:_)([a-z\d]*)/i) { "#{$1.capitalize}" }.sub(/^(.)/) { $1.downcase } end |
.classify(s) ⇒ String
Converts a string from snake_case to upper camel case (class name like "MyClass").
16 17 18 |
# File 'lib/oanda_api/utils/utils.rb', line 16 def self.classify(s) s.split('_').collect(&:capitalize).join end |
.pluralize(s) ⇒ String
Naively plops an "s" at the end of a string. If the string is "" or nil, returns "".
24 25 26 27 |
# File 'lib/oanda_api/utils/utils.rb', line 24 def self.pluralize(s) return "" if s.to_s == "" s.to_s =~ /s$/ ? s.to_s : "#{s}s" end |
.rubyize_keys(hash) ⇒ Hash
Returns a deep copy of a hash with its keys downcased, underscored and symbolized into ruby sweetness.
40 41 42 |
# File 'lib/oanda_api/utils/utils.rb', line 40 def self.rubyize_keys(hash) transform_hash_keys(hash) { |key| underscore(key).to_sym } end |
.singularize(s) ⇒ String
Returns a string with its trailing "s" vaporized.
32 33 34 |
# File 'lib/oanda_api/utils/utils.rb', line 32 def self.singularize(s) s.to_s.chomp("s") end |
.stringify_keys(hash) ⇒ Hash
Returns a deep copy of a hash with its keys camelized, underscored and symbolized.
48 49 50 |
# File 'lib/oanda_api/utils/utils.rb', line 48 def self.stringify_keys(hash) transform_hash_keys(hash) { |key| camelize key } end |
.transform_hash_keys(value, &block) {|Object| ... } ⇒ Hash
Yields all keys of a hash, and safely applies whatever transform the block provides. Supports nested hashes.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/oanda_api/utils/utils.rb', line 63 def self.transform_hash_keys(value, &block) case when value.is_a?(Array) value.map { |v| transform_hash_keys(v, &block) } when value.is_a?(Hash) Hash[value.map { |k, v| [yield(k), transform_hash_keys(v, &block)] }] else value end end |
.transform_hash_values(value, key = nil, &block) ⇒ Hash
Yields all key/value pairs of a hash, and safely applies whatever transform the block provides to the values. Supports nested hashes and arrays.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/oanda_api/utils/utils.rb', line 86 def self.transform_hash_values(value, key = nil, &block) case when value.is_a?(Array) value.map { |v| transform_hash_values(v, key, &block) } when value.is_a?(Hash) Hash[value.map { |k, v| [k, transform_hash_values(v, k, &block)] }] else yield key, value end end |
.underscore(s) ⇒ String
Converts a string from camelCase to snake_case.
100 101 102 103 104 105 106 |
# File 'lib/oanda_api/utils/utils.rb', line 100 def self.underscore(s) s.to_s .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr("-", "_") .downcase end |