class DataMetaDom::Record

A data structure comprised of fields with the notion of identity, indexes, unique and not. @!attribute [r] namespace

@return [String] part of the {#name}, the prefix before last dot, "package" in Java and Scala, "namespace" in C

@!attribute [r] baseName

@return [String] part of the {#name}, the suffix after last dot

Attributes

baseName[R]
fields[R]

The fields as a map keying a field name to the matching instance of a Field

identity[R]

An instance of RecIdentity.

indexes[R]

A hash mapping to RecIndex from its key. Meaning, RecIndex.key to the matching RecIndex

key[R]

The unique key for the record, unique across the Model.

name[RW]

Full Record datatype name, including namespace if any. Should be unique in the model.

namespace[R]
refs[R]

An array of Reference

uniques[R]

A hash mapping to RecUnique from its key. Meaning, RecUnique.key to the matching RecUnique

Public Class Methods

consumed?(model, src) click to toggle source

Attempts to consume the instance from the given source, returns it if successful, returns nil otherwise.

  • Parameters

# File lib/dataMetaDom/record.rb, line 219
def self.consumed?(model, src)
    if src.line =~ /^\s*#{RECORD}\s+(\w+)$/
        newRecord = Record.new(DataMetaDom.combineNsBase(DataMetaDom.nsAdjustment(src.namespace, model.options, src).to_sym, $1)).parse(model, src)
        $stderr.puts %<WARN: Record redefinition: "#{newRecord.key}"> if model.records[newRecord.key]
        model.addRecord(newRecord)
    else
        nil
    end
end
new(name) click to toggle source

Creates an instance for the given full data type name, including namespace. Namespace is required.

Calls superclass method
# File lib/dataMetaDom/record.rb, line 235
def initialize(name)
    #noinspection RubyArgCount
    super()
    @namespace, @baseName = DataMetaDom.splitNameSpace(name)

    raise %Q<Record "#{@baseName}": no namespace; namespaces are required!> unless @namespace
    @name = name.to_sym
    @key = @name
    @fields={}; @uniques={}; @identity=nil; @indexes = {}; @refs=[]
end

Public Instance Methods

[](fieldKey) click to toggle source

Fetches the field by the field key, i.e. Field.name

# File lib/dataMetaDom/record.rb, line 249
def [](fieldKey); @fields[fieldKey] end
addField(newField, model, source=nil) click to toggle source

Add another field to this Record's definition along with the source reference if any.

  • Parameters:

    • newField - an instance of Field to add to this Record

    • source - a reference to the SourceFile where this field has been defined, pass nil if

    built from the code.
# File lib/dataMetaDom/record.rb, line 312
def addField(newField, model, source=nil)
    fieldKey = newField.name
    raise "Duplicate field name '#{fieldKey}' in the Record '#{@name}'" if (@fields.key?(fieldKey))
    @fields[fieldKey] = newField
    unless STANDARD_TYPES.member?(newField.dataType.type)
        ns, base = DataMetaDom.splitNameSpace(newField.dataType.type)
        newNs = DataMetaDom.nsAdjustment(ns, model.options, source)
        reRefName = "#{newNs}.#{base}".to_sym
        newField.dataType.type = reRefName # adjust the type for finding the reference again
        @refs << Reference.new(self, newField, reRefName, source ? source.snapshot : nil)
    end
end
addFields(fields, model, source=nil) click to toggle source

Add several Field definitions to this Record.

  • Parameters:

    • fields - an array of the instances of Field to add to this Record

# File lib/dataMetaDom/record.rb, line 330
def addFields(fields, model, source=nil); fields.each { |f| addField f, model, source } end
addIndex(newIx) click to toggle source

Add another non-unique index to the record.

# File lib/dataMetaDom/record.rb, line 299
def addIndex(newIx)
    assertIds(newIx.args)
    raise "Duplicate index declaration #{newIx}" if @indexes.has_key?(newIx.key)
    @indexes[newIx.key] = newIx
end
addIndexes(ixs) click to toggle source

Add several non-unique indexes to the record.

  • Parameter:

# File lib/dataMetaDom/record.rb, line 337
def addIndexes(ixs); ixs.each { |ix| addIndex ix } end
addUnique(newUq) click to toggle source

Add another unique index to the record.

  • Parameter:

    • newUq - an instance of RecUnique to add ot this record.

# File lib/dataMetaDom/record.rb, line 288
def addUnique(newUq)
    assertIds(newUq.args)
    raise "Duplicate unique set declaration #{newUq}" if @uniques.has_key?(newUq.key)
    @uniques[newUq.key] = newUq
end
addUniques(uqs) click to toggle source

Add several unique indexes to the record.

  • Parameter:

    • uqs - an array of instances of RecUnique to add ot this record.

# File lib/dataMetaDom/record.rb, line 344
def addUniques(uqs); uqs.each { |uq| addUnique uq } end
assertIds(ids) click to toggle source

Verifies that the list of ids is valid, meaning that there is a field with the given name on this record.

  • Parameter

    • ids - an array of strings, each should be a valid field name already defined on this Record.

# File lib/dataMetaDom/record.rb, line 256
def assertIds(ids)
    ids.each { |id|
        k = id.to_sym
        raise "Undeclared field '#{id}'" unless @fields.has_key?(k)
    }
end
identity=(newIdy) click to toggle source

Set the identity on the Record.

# File lib/dataMetaDom/record.rb, line 268
def identity=(newIdy)
    raise 'There can be only one identity statement in a record' if @identity
    @identity = newIdy
    assertIds(@identity.args)
    @identity.args.each { |id|
        f = @fields[id.to_sym]

        raise ArgumentError, %|Field "#{
            id}" is made identity; no aggregate types or maps can be identity| if f.aggr? || f.map?

        raise ArgumentError, %|Optional field "#{
            id}" is made identity; only required fields may be identity| unless f.isRequired
    }
end
parse(model, source) click to toggle source

Parse the Record from the given source into this instance.

  • Parameter:

    • source - an instance of SourceFile to parse from

# File lib/dataMetaDom/record.rb, line 351
def parse(model, source)
    while (line = source.nextLine)
        next if docConsumed?(source)
        case line
            when /^\s*#{END_KW}\s*$/
                if source.docs
                    self.docs = source.docs.clone
                    source.docs.clear
                end
                self.ver = source.ver unless self.ver
                raise "Version missing for the Record #{name}" unless self.ver
                return self
            when ''
                next
            else
                isTokenConsumed = false
                RECORD_LEVEL_TOKENS.each { |t|
                    isTokenConsumed = t.consumed? source, self
                    break if isTokenConsumed
                }
                unless isTokenConsumed
                    raise ArgumentError, "Record #{@name}: all field declarations must precede identity" if @identity
                    Field.consume(model, source, self)
                end
                resetEntity
        end # case line
    end # while line
    self # for call chaining
end
to_s() click to toggle source

Textual representation of this instance, with all the fields, attributes and references if any.

# File lib/dataMetaDom/record.rb, line 384
def to_s
    "Record{#{@name}(#{@fields.values.join(';')},uq=[#{@uniques.map { |u| '[' + u.join(',') + ']' }.join('; ')}]"\
   ";idy=[#{@identity ? @identity.args.join(',') : ''}]; refs=[#{@refs.join(',')}]}, #{self.ver}"
end