Module: Geos::Helper

Defined in:
lib/geos/geos_helper.rb

Constant Summary collapse

JS_ESCAPE_MAP =
{
  '\\'    => '\\\\',
  '</'    => '<\/',
  "\r\n"  => '\n',
  "\n"    => '\n',
  "\r"    => '\n',
  '"'     => '\\"',
  "'"     => "\\'"
}

Class Method Summary collapse

Class Method Details

.array_wrap(object) ⇒ Object

Makes sure an object is wrapped in an Array.



107
108
109
110
111
112
113
114
115
# File 'lib/geos/geos_helper.rb', line 107

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) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/geos/geos_helper.rb', line 39

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) ⇒ Object

Return a new Hash with all keys converted to camelized Strings.



64
65
66
67
68
69
# File 'lib/geos/geos_helper.rb', line 64

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) ⇒ Object

Destructively convert all keys to camelized Strings.



72
73
74
75
76
77
78
79
80
81
# File 'lib/geos/geos_helper.rb', line 72

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) ⇒ Object

Deeply camelize a Hash.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/geos/geos_helper.rb', line 84

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) ⇒ Object

Destructively deeply camelize a Hash.



97
98
99
# File 'lib/geos/geos_helper.rb', line 97

def deep_camelize_keys!(hash, first_letter_in_uppercase = false)
  hash.replace(deep_camelize_keys(hash, first_letter_in_uppercase))
end

.escape_javascript(javascript) ⇒ Object

Escape carrier returns and single and double quotes for JavaScript segments. Borrowed from ActiveSupport.



16
17
18
19
20
21
22
# File 'lib/geos/geos_helper.rb', line 16

def escape_javascript(javascript) #:nodoc:
  if javascript
    javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
  else
    ''
  end
end

.escape_json(hash, ignore_keys = []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/geos/geos_helper.rb', line 24

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) ⇒ Object



101
102
103
104
# File 'lib/geos/geos_helper.rb', line 101

def number_with_precision(number, precision = 6)
  rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision
  "%01.#{precision}f" % rounded_number
end

.xml_options(*args) ⇒ Object

:nodoc:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/geos/geos_helper.rb', line 47

def xml_options(*args) #:nodoc:
  xml = if Builder::XmlMarkup === args.first
    args.first
  else
    Builder::XmlMarkup.new(:indent => 4)
  end

  options = if Hash === args.last
    args.last
  else
    Hash.new
  end

  [ xml, options ]
end