Class: FMCache::Jsonizer

Inherits:
Object
  • Object
show all
Defined in:
lib/fmcache/jsonizer.rb,
lib/fmcache/jsonizer/default_json_serializer.rb

Defined Under Namespace

Classes: DefaultJsonSerializer

Instance Method Summary collapse

Constructor Details

#initialize(json_serializer) ⇒ Jsonizer

Returns a new instance of Jsonizer.

Parameters:

  • json_serializer (#dump#load | nil)


6
7
8
# File 'lib/fmcache/jsonizer.rb', line 6

def initialize(json_serializer)
  @json_serializer = json_serializer || DefaultJsonSerializer
end

Instance Method Details

#dejsonize(hash) ⇒ { String => { String => <Hash> } }

Parameters:

  • hash ({ String => { String => String } })

Returns:

  • ({ String => { String => <Hash> } })


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fmcache/jsonizer.rb', line 26

def dejsonize(hash)
  r = {}
  hash.each do |k, v|
    h = {}
    v.each do |kk, vv|
      if vv.nil?
        h[kk] = nil
      else
        begin
          h[kk] = @json_serializer.load(vv)
        rescue
          h[kk] = nil
        end
      end
    end
    r[k] = h
  end
  r
end

#jsonize(hash) ⇒ { String => { String => String } }

Parameters:

  • hash ({ String => { String => <Hash> } })

Returns:

  • ({ String => { String => String } })


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fmcache/jsonizer.rb', line 12

def jsonize(hash)
  r = {}
  hash.each do |k, v|
    h = {}
    v.each do |kk, vv|
      h[kk] = @json_serializer.dump(vv)
    end
    r[k] = h
  end
  r
end