Class: C0::Builder
- Inherits:
-
Object
- Object
- C0::Builder
- Defined in:
- lib/c0.rb
Overview
Builds C0DATA compact bytes. Byte-identical to the C builder.
Names (file/group/header) reject control bytes; record field values are byte-transparent and DLE-escaped automatically.
Instance Method Summary collapse
-
#block(text) ⇒ Object
Document-mode content block: RS + escaped text.
- #bytes ⇒ Object
- #eot ⇒ Object
-
#etb(payload = nil) ⇒ Object
ETB commit marker (stream mode) with an optional integrity payload, which may not contain control bytes.
-
#field(value) ⇒ Object
Single field: US + escaped value (for building records field by field).
- #file(name) ⇒ Object
- #group(name, headers = nil) ⇒ Object
- #header(names) ⇒ Object
-
#initialize ⇒ Builder
constructor
A new instance of Builder.
-
#item(text) ⇒ Object
Document-mode list item: US + escaped text.
-
#list_field(items) ⇒ Object
Field whose value is a flat list: US STX items-joined-by-US ETX.
-
#nested {|_self| ... } ⇒ Object
Nested sub-structure: STX, the block's output, ETX.
- #record(*fields) ⇒ Object
-
#ref(*path) ⇒ Object
Reference: ENQ + name, or with 2+ segments ENQ STX segments-joined-by-US ETX.
-
#section(name, depth = 1) ⇒ Object
Document-mode section: GS x depth + name.
Constructor Details
#initialize ⇒ Builder
Returns a new instance of Builder.
248 249 250 |
# File 'lib/c0.rb', line 248 def initialize @buf = +"".b end |
Instance Method Details
#block(text) ⇒ Object
Document-mode content block: RS + escaped text.
351 352 353 354 355 |
# File 'lib/c0.rb', line 351 def block(text) @buf << RS write_escaped(text) self end |
#bytes ⇒ Object
364 |
# File 'lib/c0.rb', line 364 def bytes = @buf.dup |
#eot ⇒ Object
284 285 286 287 |
# File 'lib/c0.rb', line 284 def eot @buf << EOT self end |
#etb(payload = nil) ⇒ Object
ETB commit marker (stream mode) with an optional integrity payload, which may not contain control bytes.
291 292 293 294 295 296 297 298 299 |
# File 'lib/c0.rb', line 291 def etb(payload = nil) @buf << ETB if payload b = C0.to_bin(payload) b.each_byte { |x| raise ArgumentError, "ETB payload may not contain control bytes" if x < 0x20 } @buf << b end self end |
#field(value) ⇒ Object
Single field: US + escaped value (for building records field by field).
337 338 339 340 341 |
# File 'lib/c0.rb', line 337 def field(value) @buf << US write_escaped(value) self end |
#file(name) ⇒ Object
252 253 254 255 256 |
# File 'lib/c0.rb', line 252 def file(name) @buf << FS write_name(name) self end |
#group(name, headers = nil) ⇒ Object
258 259 260 261 262 263 |
# File 'lib/c0.rb', line 258 def group(name, headers = nil) @buf << GS write_name(name) header(headers) if headers self end |
#header(names) ⇒ Object
265 266 267 268 269 270 271 272 |
# File 'lib/c0.rb', line 265 def header(names) @buf << SOH names.each_with_index do |n, i| @buf << US if i.positive? write_name(n) end self end |
#item(text) ⇒ Object
Document-mode list item: US + escaped text.
358 359 360 361 362 |
# File 'lib/c0.rb', line 358 def item(text) @buf << US write_escaped(text) self end |
#list_field(items) ⇒ Object
Field whose value is a flat list: US STX items-joined-by-US ETX. Read back with Record#list.
326 327 328 329 330 331 332 333 334 |
# File 'lib/c0.rb', line 326 def list_field(items) @buf << US << STX items.each_with_index do |it, i| @buf << US if i.positive? write_escaped(it) end @buf << ETX self end |
#nested {|_self| ... } ⇒ Object
Nested sub-structure: STX, the block's output, ETX.
302 303 304 305 306 307 |
# File 'lib/c0.rb', line 302 def nested @buf << STX yield self @buf << ETX self end |
#record(*fields) ⇒ Object
274 275 276 277 278 279 280 281 282 |
# File 'lib/c0.rb', line 274 def record(*fields) fields = fields[0] if fields.size == 1 && fields[0].is_a?(Array) @buf << RS fields.each_with_index do |f, i| @buf << US if i.positive? write_escaped(f) end self end |
#ref(*path) ⇒ Object
Reference: ENQ + name, or with 2+ segments ENQ STX segments-joined-by-US ETX.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/c0.rb', line 310 def ref(*path) @buf << ENQ if path.size == 1 write_name(path[0]) else @buf << STX path.each_with_index do |seg, i| @buf << US if i.positive? write_name(seg) end @buf << ETX end self end |
#section(name, depth = 1) ⇒ Object
Document-mode section: GS x depth + name.
344 345 346 347 348 |
# File 'lib/c0.rb', line 344 def section(name, depth = 1) depth.times { @buf << GS } write_name(name) self end |