module Geos::Helper
Constants
- JS_ESCAPE_MAP
Public Class Methods
array_wrap(object)
click to toggle source
Makes sure an object is wrapped in an Array.
# File lib/geos/geos_helper.rb, line 109 def array_wrap(object) if object.nil? [] elsif object.respond_to?(:to_ary) object.to_ary || [ object ] else [ object ] end end
camelize(lower_case_and_underscored_word, first_letter_in_uppercase = false)
click to toggle source
# File lib/geos/geos_helper.rb, line 41 def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = false) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else lower_case_and_underscored_word.to_s[0..0].downcase + camelize(lower_case_and_underscored_word, true)[1..-1] end end
camelize_keys(hash, first_letter_in_uppercase = false)
click to toggle source
Return a new Hash with all keys converted to camelized Strings.
# File lib/geos/geos_helper.rb, line 66 def camelize_keys(hash, first_letter_in_uppercase = false) hash.inject({}) do |options, (key, value)| options[camelize(key, first_letter_in_uppercase)] = value options end end
camelize_keys!(hash, first_letter_in_uppercase = false)
click to toggle source
Destructively convert all keys to camelized Strings.
# File lib/geos/geos_helper.rb, line 74 def camelize_keys!(hash, first_letter_in_uppercase = false) hash.tap { hash.keys.each do |key| unless key.class.to_s == 'String' hash[camelize(key, first_letter_in_uppercase)] = hash[key] hash.delete(key) end end } end
deep_camelize_keys(hash, first_letter_in_uppercase = false)
click to toggle source
Deeply camelize a Hash.
# File lib/geos/geos_helper.rb, line 86 def deep_camelize_keys(hash, first_letter_in_uppercase = false) camelize_keys(hash, first_letter_in_upppcase).inject({}) do |memo, (k, v)| memo.tap do if v.is_a? Hash memo[k] = deep_camelize_keys(v, first_letter_in_uppercase) else memo[k] = v end end end end
deep_camelize_keys!(hash, first_letter_in_uppercase = false)
click to toggle source
Destructively deeply camelize a Hash.
# File lib/geos/geos_helper.rb, line 99 def deep_camelize_keys!(hash, first_letter_in_uppercase = false) hash.replace(deep_camelize_keys(hash, first_letter_in_uppercase)) end
escape_json(hash, ignore_keys = [])
click to toggle source
# File lib/geos/geos_helper.rb, line 26 def escape_json(hash, ignore_keys = []) json = hash.inject([]) do |memo, (k, v)| memo.tap { k = k.to_s memo << if ignore_keys.include?(k) "#{k.to_json}: #{v}" else "#{k.to_json}: #{v.to_json}" end } end "{#{json.join(', ')}}" end
number_with_precision(number, precision = 6)
click to toggle source
# File lib/geos/geos_helper.rb, line 103 def number_with_precision(number, precision = 6) rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision "%01.#{precision}f" % rounded_number end