Class: Boss::Formatter
Overview
Formats ruby object hierarchies with BOSS notation
Constant Summary
Constants included from Boss
DMINUSONE, DONE, DZERO, FMINUSONE, FONE, FZERO, TCOMPRESSED, TDOUBLE, TFALSE, TFLOAT, TFUNCTION, TGLOBREF, TMETHOD, TOBJECT, TTIME, TTRUE, TYPE_BIN, TYPE_CREF, TYPE_DICT, TYPE_EXTRA, TYPE_INT, TYPE_LIST, TYPE_NINT, TYPE_TEXT, VERSION, XT_STREAM_MODE
Instance Method Summary collapse
-
#initialize(dst = nil) ⇒ Formatter
constructor
Construct formatter for a given IO-like object or create StringIO one as output.
-
#put(ob) ⇒ Object
(also: #<<)
Put object tree routed as ob to the output.
-
#put_compressed(ob) ⇒ Object
same as put but automatically select and use proper compression.
-
#stream_mode ⇒ Object
Switch to stream mode.
-
#string ⇒ Object
Get the result as string, may not work if some specific IO instance is passed to constructor.
Methods included from Boss
#checkArg, dump, dump_compressed, load, load_all
Constructor Details
#initialize(dst = nil) ⇒ Formatter
Construct formatter for a given IO-like object or create StringIO one as output
115 116 117 118 119 120 |
# File 'lib/boss-protocol.rb', line 115 def initialize(dst=nil) @io = dst ? dst : StringIO.new('', 'wb') @io.set_encoding 'binary' @cache = { nil => 0 } @stream_mode = false end |
Instance Method Details
#put(ob) ⇒ Object Also known as: <<
Put object tree routed as ob to the output. Alias: <<. It is possible to put more than one object to the same Formatter. Not that formatter has per-instance cache so put(x) put(x) put(x) will store one object and 2 more refs to it, so on load time only one object will be constructed and 2 more refs will be creted.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/boss-protocol.rb', line 161 def put(ob) case ob when Fixnum, Bignum if ob < 0 whdr TYPE_NINT, -ob else whdr TYPE_INT, ob end when String, Symbol ob = ob.to_s unless ob.is_a?(String) if notCached(ob) if ob.encoding == Encoding::BINARY whdr TYPE_BIN, ob.length wbin ob else whdr TYPE_TEXT, ob.bytesize wbin ob.dup.encode(Encoding::UTF_8) end end when Array if notCached(ob) whdr TYPE_LIST, ob.length ob.each { |x| put(x) } end when Hash if notCached(ob) whdr TYPE_DICT, ob.length ob.each { |k, v| self << k << v } end when Float case ob when 0 whdr TYPE_EXTRA, DZERO when -1.0 whdr TYPE_EXTRA, DMINUSONE when +1.0 whdr TYPE_EXTRA, DONE else whdr TYPE_EXTRA, TDOUBLE wdouble ob end when TrueClass, FalseClass whdr TYPE_EXTRA, ob ? TTRUE : TFALSE when nil whdr TYPE_CREF, 0 when Time whdr TYPE_EXTRA, TTIME wrenc ob.to_i else error = "error: not supported object: #{ob}, #{ob.class}" p error raise NotSupportedException, error end self end |
#put_compressed(ob) ⇒ Object
same as put but automatically select and use proper compression. Parser.get will automatically decompress.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/boss-protocol.rb', line 134 def put_compressed ob data = Boss.dump(ob) whdr TYPE_EXTRA, TCOMPRESSED type = case data.length when 0..160 0 when 161..8192 data = Zlib::Deflate.new.deflate(data, Zlib::FINISH) 1 else data = Zlib::Deflate.new.deflate(data, Zlib::FINISH) 1 #data = Bzip2.compress data #2 end whdr type, data.length wbin data end |
#stream_mode ⇒ Object
Switch to stream mode. Stream mode turns off caching.
124 125 126 127 128 |
# File 'lib/boss-protocol.rb', line 124 def stream_mode @stream_mode = true whdr TYPE_EXTRA, XT_STREAM_MODE @cache = { nil => 0 } end |
#string ⇒ Object
Get the result as string, may not work if some specific IO instance is passed to constructor. works well with default constructor or StringIO
223 224 225 |
# File 'lib/boss-protocol.rb', line 223 def string @io.string end |