Class: C0::Record
Overview
A record: positional or keyed, never mixed. Zero-copy accessors.
A sub-unit is not a unit of the record: field_count, fields, values
and pairs see the units only; unit(n) and unit(key) give a unit with
its subs, units every positional unit so. Enumerates the logical
values of a positional record.
Instance Attribute Summary collapse
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#stop ⇒ Object
readonly
Returns the value of attribute stop.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Logical value under a key (last wins), or nil.
- #each ⇒ Object
-
#field(index_or_key) ⇒ Object
Raw bytes of unit n (its layout included, its sub-units excluded), or of the unit under a key (last wins); nil when there is none.
-
#field_count ⇒ Object
(also: #size)
Number of positional units of the result (0 for a keyed record).
- #fields ⇒ Object
-
#initialize(buf, start, stop) ⇒ Record
constructor
A new instance of Record.
- #key?(key) ⇒ Boolean (also: #has_key?)
-
#keyed? ⇒ Boolean
Whether the record is keyed (it holds a keyed unit, live or dead).
-
#keys ⇒ Object
Keys in written order (duplicates included; dead units skipped).
-
#level(index_or_key) ⇒ Object
Unit n (or the unit under a key) as a nested level.
-
#list(index) ⇒ Object
Unit n as a flat list: a level holding at most one positional record.
-
#pairs ⇒ Object
[key, logical value] pairs in written order.
-
#raw ⇒ Object
The record's bytes (after its RS, trailing layout excluded).
-
#ref(index) ⇒ Object
Unit n as a Reference.
-
#to_h ⇒ Object
The keyed record as a Hash (last wins).
-
#unit(index_or_key) ⇒ Object
Unit n (or the unit under a key, last wins) with its sub-units; nil when there is none.
-
#units ⇒ Object
Every positional unit with its sub-units (empty for a keyed record).
-
#value(index) ⇒ Object
Logical value of unit n, or nil when there is none.
- #values ⇒ Object
Constructor Details
#initialize(buf, start, stop) ⇒ Record
Returns a new instance of Record.
253 254 255 256 257 |
# File 'lib/c0/body.rb', line 253 def initialize(buf, start, stop) @buf = buf @start = start @stop = stop end |
Instance Attribute Details
#start ⇒ Object (readonly)
Returns the value of attribute start.
251 252 253 |
# File 'lib/c0/body.rb', line 251 def start @start end |
#stop ⇒ Object (readonly)
Returns the value of attribute stop.
251 252 253 |
# File 'lib/c0/body.rb', line 251 def stop @stop end |
Instance Method Details
#[](key) ⇒ Object
Logical value under a key (last wins), or nil.
346 347 348 349 |
# File 'lib/c0/body.rb', line 346 def [](key) s, e = keyed_range(key) s && Ext.value(@buf, s, e) end |
#each ⇒ Object
307 |
# File 'lib/c0/body.rb', line 307 def each(&) = values.each(&) |
#field(index_or_key) ⇒ Object
Raw bytes of unit n (its layout included, its sub-units excluded), or of the unit under a key (last wins); nil when there is none.
269 270 271 272 |
# File 'lib/c0/body.rb', line 269 def field(index_or_key) s, e = index_or_key.is_a?(Integer) ? unit_ranges[index_or_key] : keyed_range(index_or_key) s && @buf.byteslice(s, e - s) end |
#field_count ⇒ Object Also known as: size
Number of positional units of the result (0 for a keyed record).
275 |
# File 'lib/c0/body.rb', line 275 def field_count = unit_ranges.size |
#fields ⇒ Object
279 |
# File 'lib/c0/body.rb', line 279 def fields = unit_ranges.map { |s, e| @buf.byteslice(s, e - s) } |
#key?(key) ⇒ Boolean Also known as: has_key?
351 |
# File 'lib/c0/body.rb', line 351 def key?(key) = !keyed_range(key).nil? |
#keyed? ⇒ Boolean
Whether the record is keyed (it holds a keyed unit, live or dead).
263 |
# File 'lib/c0/body.rb', line 263 def keyed? = Ext.record_keyed(@buf, @start, @stop) |
#keys ⇒ Object
Keys in written order (duplicates included; dead units skipped).
340 |
# File 'lib/c0/body.rb', line 340 def keys = pair_ranges.map { |ks, ke, _, _| @buf.byteslice(ks, ke - ks) } |
#level(index_or_key) ⇒ Object
Unit n (or the unit under a key) as a nested level. Raises C0::Error when it is not one; nil for a key with no unit.
311 312 313 314 315 316 317 318 |
# File 'lib/c0/body.rb', line 311 def level(index_or_key) s, e = index_or_key.is_a?(Integer) ? unit_ranges[index_or_key] : keyed_range(index_or_key) return nil if s.nil? && !index_or_key.is_a?(Integer) raise KeyError, "no unit #{index_or_key}" if s.nil? ls, le = Ext.unit_level(@buf, s, e) Body.view(@buf, ls, le) end |
#list(index) ⇒ Object
Unit n as a flat list: a level holding at most one positional record. Raises C0::Error for any other level.
322 323 324 325 326 327 |
# File 'lib/c0/body.rb', line 322 def list(index) s, e = unit_ranges[index] raise KeyError, "no unit #{index}" if s.nil? Ext.unit_list(@buf, s, e).map { |is, ie| Ext.value(@buf, is, ie) } end |
#pairs ⇒ Object
[key, logical value] pairs in written order.
343 |
# File 'lib/c0/body.rb', line 343 def pairs = pair_ranges.map { |ks, ke, vs, ve| [@buf.byteslice(ks, ke - ks), Ext.value(@buf, vs, ve)] } |
#raw ⇒ Object
The record's bytes (after its RS, trailing layout excluded).
260 |
# File 'lib/c0/body.rb', line 260 def raw = @buf.byteslice(@start, @stop - @start) |
#ref(index) ⇒ Object
Unit n as a Reference. Raises C0::Error when it is not one.
330 331 332 333 334 335 |
# File 'lib/c0/body.rb', line 330 def ref(index) s, e = unit_ranges[index] raise KeyError, "no unit #{index}" if s.nil? Reference.from_ext(@buf, Ext.unit_ref(@buf, s, e)) end |
#to_h ⇒ Object
The keyed record as a Hash (last wins).
356 |
# File 'lib/c0/body.rb', line 356 def to_h = pairs.to_h |
#unit(index_or_key) ⇒ Object
Unit n (or the unit under a key, last wins) with its sub-units; nil when there is none.
288 289 290 291 292 293 294 295 296 297 |
# File 'lib/c0/body.rb', line 288 def unit(index_or_key) return units[index_or_key] if index_or_key.is_a?(Integer) needle = C0.to_bin(index_or_key) found = nil pair_trees.each do |ks, ke, vs, ve, subs| found = [vs, ve, subs] if @buf.byteslice(ks, ke - ks) == needle end found && Unit.from_tree(@buf, found) end |
#units ⇒ Object
Every positional unit with its sub-units (empty for a keyed record).
282 283 284 |
# File 'lib/c0/body.rb', line 282 def units @units ||= Ext.record_units_with_subs(@buf, @start, @stop).map { |t| Unit.from_tree(@buf, t) } end |
#value(index) ⇒ Object
Logical value of unit n, or nil when there is none.
300 301 302 303 |
# File 'lib/c0/body.rb', line 300 def value(index) s, e = unit_ranges[index] s && Ext.value(@buf, s, e) end |
#values ⇒ Object
305 |
# File 'lib/c0/body.rb', line 305 def values = unit_ranges.map { |s, e| Ext.value(@buf, s, e) } |