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



16
17
18
19
# File 'lib/trick_serial/serializer/simple.rb', line 16

def _decode! x
  @traverse_mode = :_decode!
  _traverse! x
end

#_encode!(x) ⇒ Object



11
12
13
14
# File 'lib/trick_serial/serializer/simple.rb', line 11

def _encode! x
  @traverse_mode = :_encode!
  _traverse! x
end

#_traverse!(x) ⇒ Object



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
69
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
# File 'lib/trick_serial/serializer/simple.rb', line 21

def _traverse! x
  # pp [ :_traverse!, @traverse_mode, x.class, x ] if @debug >= 1

  case x
  when *@do_not_traverse
    # NOTHING

  when ObjectProxy
    if @traverse_mode == :_decode!
      x = x.object
    end

  when Struct
    if o = @visited[x.object_id]
      return o.first
    end
    o = x
    x = _copy_with_extensions(x)
    @visited[o.object_id] = [ x, o ]
    x.class.members.each do | m |
      v = x.send(m)
      v = _traverse! v
      x.send(:"#{m}=", v)
    end

  when OpenStruct
    if o = @visited[x.object_id]
      return o.first
    end
    o = x
    x = _copy_with_extensions(x)
    @visited[o.object_id] = [ x, o ]
    t = x.instance_variable_get("@table")
    t.keys.to_a.each do | k |
      v = t._get_without_trick_serial(k)
      v = _traverse! v
      x.send(:"#{k}=", v)
    end

  when Array
    if o = @visited[x.object_id]
      return o.first
    end
    o = x
    x = _copy_with_extensions(x)
    @visited[o.object_id] = [ x, o ]
    x.map! do | v |
      _traverse! v
    end

  when Hash
    if o = @visited[x.object_id]
      return o.first
    end
    o = x
    x = _copy_with_extensions(x)
    @visited[o.object_id] = [ x, o ]
    x.keys.to_a.each do | k |
      x[k] = _traverse!(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 = _copy_with_extensions(x)
        @object_to_proxy_map[o.object_id] = [ x, o ]
        ivs.each do | ivar |
          v = x.instance_variable_get(ivar)
          v = _traverse!(v)
          x.instance_variable_set(ivar, v)
        end
      end
    end
    if @traverse_mode == :_decode!
      @object_to_proxy_map[o.object_id] ||= [ x, o ]
    else
      x = _make_proxy o, x, proxy_cls
    end
  end # case

  # pp [ :_traverse!, @traverse_mode, :RESULT, x.class, x ] if @debug >= 2

  x
end