Class: C0::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/c0/builder.rb

Overview

Writes C0DATA in canonical compact form. The class and its methods are defined in the C extension (a TypedData object owning a c0_builder); this file adds the Ruby conveniences.

buf = C0::Builder.build do |b|
b.syn(version: "1")
b.group("users", %w[name amount]) do
  b.record("Alice", "1502.30")
  b.record("Bob", "340.00")
end
b.eot
end

Two layers. The primitives are one method per code (fs, gs, rs, us, sub, soh, dle, can, bel, enq, syn, eot, etb, em); each writes its code and, when given, what grammatically follows it, so a chain reads like the bytes. sub is bare: the element after the next separator is subordinate to the one before it, at that grain (before GS a subsection, before US a sub-unit). enq takes a group name, or a block writing a pattern (␅␐ … ␘):

b.rs("Alice").us("1502.30")                              # ␞Alice␟1502.30
b.rs.soh("host").us("localhost").soh("port").us("5432")  # ␞␁host␟localhost␁port␟5432
b.gs("users").soh.rs("name").us("amount")                # ␝users␁␞name␟amount
b.bel.gs("old")                                          # ␇␝old, a dead group
b.rs("old").sub.us("new")                                # ␞old␚␟new, new a sub-unit of old
b.gs("intro", depth: 2)                                  # ␚␝intro, a subsection
b.rs.enq("tags").us.enq { b.gs("tags").rs("001") }       # ␞␅tags␟␅␐␝tags␞001␘

The composed methods (file, group, header, record, field, sub_unit, map, key, level, list, ref, note) are built on the primitives and cannot write ill-formed output. The primitives write what you spell; well-formedness is then yours to keep. Both layers share the record state, so they mix: b.rs("a").field("b") writes ␞a␟b.

Values are byte-transparent and written in their one spelling: plain when they can be, text (STX … ETX) otherwise, with only the seven codes escaped; there is no STX/ETX primitive, so everything written stays canonical. Names (file, group, header, key, reference step, ETB payload) must be plain, or a C0::BuildError is raised before anything is written. Every method returns the builder, so calls chain.

A record is opened by record or rs (or implicitly by the first field, sub_unit, key, level, list or ref) and ended by the next structural call. Keyed units written right after syn belong to the heading. Inside level { }, ref { }, enq { } (or between dle and can) the builder is at the top of a fresh body.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build {|b| ... } ⇒ Object

Drive a fresh builder and return its bytes.

Yields:

  • (b)


53
54
55
56
57
# File 'lib/c0/builder.rb', line 53

def self.build
  b = new
  yield b
  b.bytes
end

Instance Method Details

#inspect ⇒ Object



67
# File 'lib/c0/builder.rb', line 67

def inspect = "#<C0::Builder #{bytesize} bytes, depth #{depth}>"

#list_field(items) ⇒ Object

Compatibility spelling of list.



65
# File 'lib/c0/builder.rb', line 65

def list_field(items) = list(items)

#to_s ⇒ Object

Alias of bytes: the compact form as a binary String.



60
# File 'lib/c0/builder.rb', line 60

def to_s = bytes