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
The fields as a map keying a field name to the matching instance of a Field
An instance of RecIdentity
.
A hash mapping to RecIndex
from its key. Meaning, RecIndex.key
to the matching RecIndex
The unique key for the record, unique across the Model
.
Full Record
datatype name, including namespace if any. Should be unique in the model.
An array of Reference
A hash mapping to RecUnique
from its key. Meaning, RecUnique.key
to the matching RecUnique
Public Class Methods
Attempts to consume the instance from the given source, returns it if successful, returns nil otherwise.
-
Parameters
-
model
- an instance of aModel
-
src
- an instance ofSourceFile
-
# 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
Creates an instance for the given full data type name, including namespace. Namespace is required.
# 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
Fetches the field by the field key, i.e. Field.name
# File lib/dataMetaDom/record.rb, line 249 def [](fieldKey); @fields[fieldKey] end
Add another field to this Record's definition along with the source reference if any.
-
Parameters:
-
source
- a reference to theSourceFile
where this field has been defined, passnil
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
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
Add another unique index to the record.
-
Parameter:
-
newUq
- an instance ofRecUnique
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
Add several unique indexes to the record.
-
Parameter:
-
uqs
- an array of instances ofRecUnique
to add ot this record.
-
# File lib/dataMetaDom/record.rb, line 344 def addUniques(uqs); uqs.each { |uq| addUnique uq } end
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 thisRecord
.
-
# 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
Set the identity on the Record
.
-
Parameter:
-
newIdy
- an instance ofRecIdentity
-
# 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 the Record
from the given source into this instance.
-
Parameter:
-
source
- an instance ofSourceFile
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
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