Module: Postmark::HashHelper

Extended by:
HashHelper
Included in:
HashHelper
Defined in:
lib/postmark/helpers/hash_helper.rb

Instance Method Summary collapse

Instance Method Details

#to_postmark(object, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/postmark/helpers/hash_helper.rb', line 6

def to_postmark(object, options = {})
  deep = options.fetch(:deep, false)

  case object
  when Hash
    object.reduce({}) do |m, (k, v)|
      m.tap do |h|
        h[Inflector.to_postmark(k)] = deep ? to_postmark(v, options) : v
      end
    end
  when Array
    deep ? object.map { |v| to_postmark(v, options) } : object
  else
    object
  end
end

#to_ruby(object, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/postmark/helpers/hash_helper.rb', line 23

def to_ruby(object, options = {})
  compatible = options.fetch(:compatible, false)
  deep = options.fetch(:deep, false)

  case object
  when Hash
    object.reduce({}) do |m, (k, v)|
      m.tap do |h|
        h[Inflector.to_ruby(k)] = deep ? to_ruby(v, options) : v
      end
    end.tap do |result|
      if compatible
        result.merge!(object)
        enhance_with_compatibility_warning(result)
      end
    end
  when Array
    deep ? object.map { |v| to_ruby(v, options) } : object
  else
    object
  end
end