Class: C0::Heading

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/c0/document.rb

Overview

The heading of a document: the keyed units after SYN (dead units skipped; the heading's values take no sub-units). Last wins for a duplicate key. Enumerates [key, value] pairs.

Constant Summary collapse

KNOWN_KEYS =

The keys the format defines (DESIGN.md "Heading"): shape is diff or stream; schema a name, or a level holding one keyed record of name, url, hash; version; blob the blob table.

%w[shape schema version blob].freeze

Instance Method Summary collapse

Constructor Details

#initialize(buf, start, stop) ⇒ Heading

Returns a new instance of Heading.



14
15
16
17
18
# File 'lib/c0/document.rb', line 14

def initialize(buf, start, stop)
  @buf = buf
  @start = start
  @stop = stop
end

Instance Method Details

#[](key) ⇒ Object

Value for a key (last wins), or nil.



33
34
35
36
37
38
# File 'lib/c0/document.rb', line 33

def [](key)
  needle = C0.to_bin(key)
  found = nil
  pairs.each { |k, v| found = v if k == needle }
  found
end

#blob ⇒ Object

The blob table as a Body, when declared as a level.



69
# File 'lib/c0/document.rb', line 69

def blob = level("blob")

#each ⇒ Object



28
# File 'lib/c0/document.rb', line 28

def each(&) = pairs.each(&)

#empty? ⇒ Boolean

Returns:

  • (Boolean)


42
# File 'lib/c0/document.rb', line 42

def empty? = raw_pairs.empty?

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


40
# File 'lib/c0/document.rb', line 40

def key?(key) = !self[key].nil?

#keys ⇒ Object



30
# File 'lib/c0/document.rb', line 30

def keys = raw_pairs.map(&:first)

#level(key) ⇒ Object

The value under a key as a nested level, or nil when absent or not one.



60
61
62
63
64
65
66
# File 'lib/c0/document.rb', line 60

def level(key)
  range = raw_range(key)
  return nil unless range && Ext.unit_type(@buf, *range) == UnitType::LEVEL

  ls, le = Ext.unit_level(@buf, *range)
  Body.view(@buf, ls, le)
end

#pairs ⇒ Object

[key, value] in written order: the logical value, or for a nested level (the blob table, a schema record) its raw bytes.



22
23
24
25
26
# File 'lib/c0/document.rb', line 22

def pairs
  @pairs ||= raw_pairs.map do |k, s, e|
    [k, Ext.unit_type(@buf, s, e) == UnitType::LEVEL ? @buf.byteslice(s, e - s) : Ext.value(@buf, s, e)]
  end
end

#raw ⇒ Object

The heading's bytes.



72
# File 'lib/c0/document.rb', line 72

def raw = @buf.byteslice(@start, @stop - @start)

#schema ⇒ Object

The schema the body claims: a plain name (bytes), or for the keyed form the one keyed Record of name, url, hash; nil when absent. The format checks the form; a schema layer checks the match. The same shape in every port.



57
# File 'lib/c0/document.rb', line 57

def schema = level("schema")&.record(0) || self["schema"]

#shape ⇒ Object

The shape value as a String, or nil.



45
# File 'lib/c0/document.rb', line 45

def shape = self["shape"]&.dup&.force_encoding(Encoding::UTF_8)

#version ⇒ Object

The version value as an Integer; 1 when absent or not a number.



48
49
50
51
# File 'lib/c0/document.rb', line 48

def version
  v = self["version"]
  v && v.match?(/\A\d+\z/) ? v.to_i : 1
end