Class: C0::Document

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

Overview

One C0DATA document: an optional SYN heading, a body, an optional EOT. Document.new(buf) parses the first document in the buffer, applying the whole grammar (a C0::SyntaxError on malformed input). Use Container for a buffer holding several documents or an EM tail. Enumerates the groups of the body's first file.

Constant Summary collapse

SHAPES =

The shapes the format defines; any other is a loud warning.

%w[diff stream].freeze
WARN_NO_SYN =

Warning bits, as the C core reports them (DESIGN.md "Warnings").

0x01
WARN_NO_EOT =
0x02
WARN_UNKNOWN_KEY =
0x04
WARN_DUPLICATE_KEY =
0x08
WARN_UNKNOWN_SHAPE =
0x10
WARN_UNKNOWN_VERSION =
0x20

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf, start = 0) ⇒ Document

Returns a new instance of Document.



109
110
111
112
# File 'lib/c0/document.rb', line 109

def initialize(buf, start = 0)
  @buf = C0.to_bin(buf)
  load(Ext.document(@buf, start))
end

Instance Attribute Details

#body ⇒ Object (readonly)

Returns the value of attribute body.



107
108
109
# File 'lib/c0/document.rb', line 107

def body
  @body
end

#buf ⇒ Object (readonly)

Returns the value of attribute buf.



107
108
109
# File 'lib/c0/document.rb', line 107

def buf
  @buf
end

#dead_elements ⇒ Object (readonly)

Returns the value of attribute dead_elements.



107
108
109
# File 'lib/c0/document.rb', line 107

def dead_elements
  @dead_elements
end

#heading ⇒ Object (readonly)

Returns the value of attribute heading.



107
108
109
# File 'lib/c0/document.rb', line 107

def heading
  @heading
end

#start ⇒ Object (readonly)

Returns the value of attribute start.



107
108
109
# File 'lib/c0/document.rb', line 107

def start
  @start
end

#stop ⇒ Object (readonly)

Returns the value of attribute stop.



107
108
109
# File 'lib/c0/document.rb', line 107

def stop
  @stop
end

#warning_bits ⇒ Object (readonly)

Returns the value of attribute warning_bits.



107
108
109
# File 'lib/c0/document.rb', line 107

def warning_bits
  @warning_bits
end

Class Method Details

.from_ext(buf, desc) ⇒ Object

A document from the C reader's description (see Container).



115
116
117
118
119
120
# File 'lib/c0/document.rb', line 115

def self.from_ext(buf, desc)
  doc = allocate
  doc.instance_variable_set(:@buf, buf)
  doc.send(:load, desc)
  doc
end

Instance Method Details

#dead_elements? ⇒ Boolean

Whether the body held any dead element (a dead record, header row, group or file); they are not in the model.

Returns:

  • (Boolean)


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

def dead_elements? = @dead_elements.positive?

#each_group ⇒ Object Also known as: each



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

def each_group(&) = @body.each_group(&)

#files ⇒ Object



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

def files = @body.files

#group(index_or_name) ⇒ Object Also known as: []



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

def group(index_or_name) = @body.group(index_or_name)

#group_count ⇒ Object



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

def group_count = @body.group_count

#group_names ⇒ Object



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

def group_names = @body.group_names

#groups ⇒ Object



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

def groups = @body.groups

#has_eot? ⇒ Boolean Also known as: eot?

Returns:

  • (Boolean)


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

def has_eot? = @has_eot

#has_syn? ⇒ Boolean Also known as: syn?

Returns:

  • (Boolean)


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

def has_syn? = @has_syn

#name ⇒ Object

---- convenience accessors over the body --------------------------------



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

def name = @body.name

#raw ⇒ Object

The document's bytes (SYN through EOT).



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

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

#records ⇒ Object



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

def records = @body.records

#shape ⇒ Object



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

def shape = heading.shape

#warnings ⇒ Object

Loud warnings (DESIGN.md "Warnings") as strings; empty for a careful writer's file. Warnings never fail a parse.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/c0/document.rb', line 132

def warnings
  @warnings ||= begin
    w = []
    w << "no SYN at the start of the document" if warn?(WARN_NO_SYN)
    w << "no EOT at the end of the document" if warn?(WARN_NO_EOT)
    if warn?(WARN_UNKNOWN_KEY)
      heading.keys.uniq.each { |k| w << "unknown heading key '#{k}'" unless Heading::KNOWN_KEYS.include?(k) }
    end
    if warn?(WARN_DUPLICATE_KEY)
      heading.keys.tally.each { |k, n| w << "duplicate heading key '#{k}'" if n > 1 }
    end
    w << "unknown shape '#{heading.shape}'" if warn?(WARN_UNKNOWN_SHAPE)
    w << "unknown version '#{heading['version']}'" if warn?(WARN_UNKNOWN_VERSION)
    w
  end
end