Class: C0::Reference

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

Overview

A reference (DESIGN.md "References"): a group written plain, ␅tags, or a pattern, ␅␐␝tags␞001␟label␘: the level that is one branch of the target with its values left out, reported as the Body it is written as. path linearises either form into its steps (file, group names, record id, field name, then an inner pattern's steps). Resolution is not implemented.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pattern, path, group_name = nil) ⇒ Reference

Returns a new instance of Reference.



390
391
392
393
394
395
# File 'lib/c0/body.rb', line 390

def initialize(name, pattern, path, group_name = nil)
  @name = name
  @pattern = pattern
  @path = path
  @group_name = group_name
end

Instance Attribute Details

#name ⇒ Object (readonly)

The group name of a plain reference, or nil for a pattern.



384
385
386
# File 'lib/c0/body.rb', line 384

def name
  @name
end

#path ⇒ Object (readonly)

The steps in walking order.



388
389
390
# File 'lib/c0/body.rb', line 388

def path
  @path
end

#pattern ⇒ Object (readonly)

The pattern's body (the inside of its level), or nil for a plain reference.



386
387
388
# File 'lib/c0/body.rb', line 386

def pattern
  @pattern
end

Class Method Details

.from_ext(buf, desc) ⇒ Object

A reference from the extension's description of a unit of buf.



404
405
406
407
408
409
410
# File 'lib/c0/body.rb', line 404

def self.from_ext(buf, desc)
  name_range, pattern_range, steps, group_range = desc
  new(name_range && buf.byteslice(name_range[0], name_range[1] - name_range[0]),
      pattern_range && Body.view(buf, pattern_range[0], pattern_range[1]),
      steps.map { |s, e| buf.byteslice(s, e - s) },
      group_range && buf.byteslice(group_range[0], group_range[1] - group_range[0]))
end

.parse(raw) ⇒ Object

Parse a raw unit (ENQ …). Raises C0::Error when it is not a reference.



398
399
400
401
# File 'lib/c0/body.rb', line 398

def self.parse(raw)
  b = C0.to_bin(raw)
  from_ext(b, Ext.unit_ref(b, 0, b.bytesize))
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



424
# File 'lib/c0/body.rb', line 424

def ==(other) = other.is_a?(Reference) && other.name == @name && other.pattern&.raw == @pattern&.raw

#group ⇒ Object

The group named. Raises C0::Error when the reference is a pattern into a group.



422
# File 'lib/c0/body.rb', line 422

def group = @group_name || raise(Error, "reference is a pattern, not a group")

#group? ⇒ Boolean

Whether the reference names a group as a whole: a plain name, or a pattern that is a bare group line (␅␐␝tags␘).

Returns:

  • (Boolean)


416
# File 'lib/c0/body.rb', line 416

def group? = !@group_name.nil?

#group_name ⇒ Object

The group named when group?, else nil.



419
# File 'lib/c0/body.rb', line 419

def group_name = @group_name

#hash ⇒ Object



428
# File 'lib/c0/body.rb', line 428

def hash = [@name, @pattern&.raw].hash

#inspect ⇒ Object



432
# File 'lib/c0/body.rb', line 432

def inspect = "#<C0::Reference #{@name ? @name.inspect : "pattern #{to_s.inspect}"}>"

#pattern? ⇒ Boolean

Returns:

  • (Boolean)


412
# File 'lib/c0/body.rb', line 412

def pattern? = !@pattern.nil?

#to_s ⇒ Object



430
# File 'lib/c0/body.rb', line 430

def to_s = @path.join("/")