Module: Transit::Marshaler::Base Private

Included in:
BaseJson, MessagePack
Defined in:
lib/transit/marshaler/base.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#emit_array(a, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
# File 'lib/transit/marshaler/base.rb', line 85

def emit_array(a, cache)
  emit_array_start(a.size)
  a.each {|e| marshal(e, false, cache)}
  emit_array_end
end

#emit_boolean(handler, b, as_map_key, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
# File 'lib/transit/marshaler/base.rb', line 77

def emit_boolean(handler, b, as_map_key, cache)
  as_map_key ? emit_string(ESC, "?", handler.string_rep(b), true, cache) : emit_value(b)
end

#emit_double(d, as_map_key, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
# File 'lib/transit/marshaler/base.rb', line 81

def emit_double(d, as_map_key, cache)
  as_map_key ? emit_string(ESC, "d", d, true, cache) : emit_value(d)
end

#emit_encoded(handler, tag, obj, as_map_key, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/transit/marshaler/base.rb', line 107

def emit_encoded(handler, tag, obj, as_map_key, cache)
  if tag.length == 1
    rep = handler.rep(obj)
    if String === rep
      emit_string(ESC, tag, rep, as_map_key, cache)
    elsif as_map_key || @prefer_strings
      if str_rep = handler.string_rep(obj)
        emit_string(ESC, tag, str_rep, as_map_key, cache)
      else
        raise "Cannot be encoded as String: " + {:tag => tag, :rep => rep, :obj => obj}.to_s
      end
    else
      emit_tagged_value(tag, handler.rep(obj), cache)
    end
  elsif as_map_key
    raise "Cannot be used as a map key: " + {:tag => tag, :rep => rep, :obj => obj}.to_s
  else
    emit_tagged_value(tag, handler.rep(obj), cache)
  end
end

#emit_map(m, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
94
95
96
97
98
# File 'lib/transit/marshaler/base.rb', line 91

def emit_map(m, cache)
  emit_map_start(m.size)
  m.each do |k,v|
    marshal(k, true, cache)
    marshal(v, false, cache)
  end
  emit_map_end
end

#emit_nil(as_map_key, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



64
65
66
# File 'lib/transit/marshaler/base.rb', line 64

def emit_nil(as_map_key, cache)
  as_map_key ? emit_string(ESC, "_", nil, true, cache) : emit_value(nil)
end

#emit_string(prefix, tag, value, as_map_key, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
71
72
73
74
75
# File 'lib/transit/marshaler/base.rb', line 68

def emit_string(prefix, tag, value, as_map_key, cache)
  encoded = "#{prefix}#{tag}#{value}"
  if @cache_enabled && cache.cacheable?(encoded, as_map_key)
    emit_value(cache.write(encoded), as_map_key)
  else
    emit_value(encoded, as_map_key)
  end
end

#emit_tagged_value(tag, rep, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



100
101
102
103
104
105
# File 'lib/transit/marshaler/base.rb', line 100

def emit_tagged_value(tag, rep, cache)
  emit_array_start(2)
  emit_string(ESC, "#", tag, false, cache)
  marshal(rep, false, cache)
  emit_array_end
end

#escape(s) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
# File 'lib/transit/marshaler/base.rb', line 56

def escape(s)
  if s.start_with?(SUB,ESC,RES) && s != "#{SUB} "
    "#{ESC}#{s}"
  else
    s
  end
end

#find_handler(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
41
42
43
44
45
# File 'lib/transit/marshaler/base.rb', line 38

def find_handler(obj)
  obj.class.ancestors.each do |a|
    if handler = @handlers[a]
      return handler
    end
  end
  nil
end

#marshal(obj, as_map_key, cache) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/transit/marshaler/base.rb', line 128

def marshal(obj, as_map_key, cache)
  if handler = find_handler(obj)
    tag = handler.tag(obj)
    case tag
    when "_"
      emit_nil(as_map_key, cache)
    when "?"
      emit_boolean(handler, obj, as_map_key, cache)
    when "s"
      emit_string(nil, nil, escape(handler.rep(obj)), as_map_key, cache)
    when "i"
      emit_int(tag, handler.rep(obj), as_map_key, cache)
    when "d"
      emit_double(handler.rep(obj), as_map_key, cache)
    when "'"
      emit_tagged_value(tag, handler.rep(obj), cache)
    when "array"
      emit_array(handler.rep(obj), cache)
    when "map"
      emit_map(handler.rep(obj), cache)
    else
      emit_encoded(handler, tag, obj, as_map_key, cache)
    end
  else
    raise "Can not find a Write Handler for #{obj.inspect}."
  end
end

#marshal_top(obj, cache = RollingCache.new) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/transit/marshaler/base.rb', line 156

def marshal_top(obj, cache=RollingCache.new)
  if handler = find_handler(obj)
    if tag = handler.tag(obj)
      if tag.length == 1
        marshal(TaggedValue.new(QUOTE, obj), false, cache)
      else
        marshal(obj, false, cache)
      end
      flush
    else
      raise "Handler must provide a non-nil tag: #{handler.inspect}"
    end
  else
    raise "Can not find a Write Handler for #{obj.inspect}."
  end
end

#parse_options(opts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/transit/marshaler/base.rb', line 22

def parse_options(opts)
  @cache_enabled  = !opts[:verbose]
  @prefer_strings = opts[:prefer_strings]
  @max_int        = opts[:max_int]
  @min_int        = opts[:min_int]

  handlers = WriteHandlers::DEFAULT_WRITE_HANDLERS.dup
  handlers = handlers.merge!(opts[:handlers]) if opts[:handlers]
  @handlers = (opts[:verbose] ? verbose_handlers(handlers) : handlers)
  @handlers.values.each do |h|
    if h.respond_to?(:handlers=)
      h.handlers=(@handlers)
    end
  end
end

#verbose_handlers(handlers) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
# File 'lib/transit/marshaler/base.rb', line 47

def verbose_handlers(handlers)
  handlers.each do |k, v|
    if v.respond_to?(:verbose_handler) && vh = v.verbose_handler
      handlers.store(k, vh)
    end
  end
  handlers
end