Class: TrickSerial::Serializer::Simple

Inherits:
TrickSerial::Serializer show all
Defined in:
lib/trick_serial/serializer/simple.rb

Overview

Simple, non-swizzling coder. This requires encode and decode operations. Array and Hash are not extended to support swizzling. Ivar swizzling is not used.

Instance Attribute Summary

Attributes inherited from TrickSerial::Serializer

#class_option_map, #debug, #enabled, #logger, #logger_level, #root, #verbose

Instance Method Summary collapse

Methods inherited from TrickSerial::Serializer

#_class_option, #_copy_with_extensions, #_extended_by, #_log, #_make_proxy, #_prepare, class_option_map, class_option_map=, #decode, #decode!, default, default=, #enabled?, #encode, #encode!, #initialize, #proxyable

Constructor Details

This class inherits a constructor from TrickSerial::Serializer

Instance Method Details

#_decode!(x) ⇒ Object

def



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/trick_serial/serializer/simple.rb', line 70

def _decode! x
  case x
  when ObjectProxy
    x = x.object

  when Array
    if o = @visited[x.object_id]
      return o.first
    end
    extended = false
    o = x
    x = x.dup if @copy
    @visited[o.object_id] = [ x, o ]
    x.map! do | v |
      _decode! v
    end
    
  when Hash
    if o = @visited[x.object_id]
      return o.first
    end
    extended = false
    o = x
    x = x.dup if @copy
    @visited[o.object_id] = [ x, o ]
    x.keys.to_a.each do | k |
      x[k] = _decode!(x[k])
    end
    
  when *@proxyable
    if proxy = @object_to_proxy_map[x.object_id]
      return proxy.first
    end
    # debugger
    o = x
    if class_option = _class_option(x)
      # Deeply encode instance vars?
      if ivs = class_option[:instance_vars]
        ivs = x.instance_variables if ivs == true
        x = x.dup if @copy
        @object_to_proxy_map[o.object_id] = [ x, o ]
        ivs.each do | ivar |
          v = x.instance_variable_get(ivar)
          # $stderr.puts "\n#{x.class} #{x.object_id} ivar #{ivar} #{v.inspect}" if @debug
          v = _decode!(v)
          x.instance_variable_set(ivar, v)
        end
      end
    end
    @object_to_proxy_map[o.object_id] ||= [ x, o ]
  end # case
  
  x
end

#_encode!(x) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/trick_serial/serializer/simple.rb', line 11

def _encode! x
  # pp [ :_encode!, x.class, x ]

  case x
  when ObjectProxy
    x

  when Array
    if o = @visited[x.object_id]
      return o.first
    end
    extended = false
    o = x
    x = x.dup if @copy
    @visited[o.object_id] = [ x, o ]
    x.map! do | v |
      _encode! v
    end

  when Hash
    if o = @visited[x.object_id]
      return o.first
    end
    extended = false
    o = x
    x = x.dup if @copy
    @visited[o.object_id] = [ x, o ]
    x.keys.to_a.each do | k |
      x[k] = _encode!(x[k])
    end

  when *@proxyable
    if proxy = @object_to_proxy_map[x.object_id]
      return proxy.first
    end
    # debugger
    o = x
    proxy_cls = nil
    if class_option = self._class_option(x)
      proxy_cls = class_option[:proxy_class]
      # Deeply encode instance vars?
      if ivs = class_option[:instance_vars]
        ivs = x.instance_variables if ivs == true
        x = x.dup if @copy
        @object_to_proxy_map[o.object_id] = [ x, o ]
        ivs.each do | ivar |
          v = x.instance_variable_get(ivar)
          v = _encode!(v)
          x.instance_variable_set(ivar, v)
        end
      end
    end
    
    x = _make_proxy o, x, proxy_cls
  end

  x
end