Class: Zaypay::Util
- Inherits:
-
Object
- Object
- Zaypay::Util
- Defined in:
- lib/zaypay/util.rb
Overview
A class containing some utility methods.
Class Method Summary collapse
-
.arrayify_if_not_an_array(obj) ⇒ Object
Wraps something in an array if it is not an array.
-
.stringify_locale_hash(locale_hash) ⇒ String
Turns a hash with :language and :country keys to a string that represents the locale.
-
.uber_symbolize(data) ⇒ Object
Symbolize keys recursively - Not just for hashes within a Hash, but also for hashes within an Array.
Class Method Details
.arrayify_if_not_an_array(obj) ⇒ Object
Wraps something in an array if it is not an array
46 47 48 |
# File 'lib/zaypay/util.rb', line 46 def self.arrayify_if_not_an_array(obj) obj.is_a?(Array) ? obj : [obj] end |
.stringify_locale_hash(locale_hash) ⇒ String
Turns a hash with :language and :country keys to a string that represents the locale
Example:
Zaypay::Util.stringify_locale_hash( { :country=>"NL", :language=>"nl" } )
=> 'nl-NL'
41 42 43 |
# File 'lib/zaypay/util.rb', line 41 def self.stringify_locale_hash(locale_hash) locale_hash[:language] << '-' << locale_hash[:country] end |
.uber_symbolize(data) ⇒ Object
Symbolize keys recursively - Not just for hashes within a Hash, but also for hashes within an Array.
Example:
hashie = { 'a' => 'A', 'b' => ['B'], 'c' => [ {'cc' => 'CC'} ], 'd' => { 'e' => 'E', :f => { :ff => 'FF' } } }
Zaypay::Util.uber_symbolize(hashie)
=> { :a => "A", :b => ["B"], :c => [ {:cc=>"CC"} ], :d => { :e => "E", :f => { :ff => "FF" } } }
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/zaypay/util.rb', line 17 def self.uber_symbolize(data) if data.is_a?(Hash) data.keys.each do |key| data[(key.to_sym rescue key) || key] = data.delete(key) end data.values.each do |v| Zaypay::Util.uber_symbolize(v) end end if data.is_a?(Array) data.each{|e| Zaypay::Util.uber_symbolize(e)} end data end |