class DataMetaDom::Enum
Worded enum, enumeration expressed as a list of words, each word consisting of alphanumerical symbols only.
For command line details either check the new method's source or the README.rdoc file, the usage section.
Attributes
The full name of the enum, including namespace if any
Public Class Methods
Attempts to consume/parse an enum from the given source on the given model, returns it if succeeds, returns nil
otherwise.
-
Parameters
-
model - an instance of
Model
-
src - an instance of
SourceFile
-
# File lib/dataMetaDom/enum.rb, line 24 def self.consumed?(model, src) src.line =~ /^\s*#{ENUM}\s+(\w+)$/ ? model.addEnum(Enum.new(DataMetaDom.combineNsBase( DataMetaDom.nsAdjustment(src.namespace, model.options, src), $1)).parse(src)) : nil end
Creates an instance for the given name, initializes internal variables.
# File lib/dataMetaDom/enum.rb, line 32 def initialize(name); @name = name.to_sym; @map = {}; @format = nil; @counter = -1 end
Public Instance Methods
Opposite to ordinal, for the given ordinal returns the word.
# File lib/dataMetaDom/enum.rb, line 72 def [](ord); @map.invert[ord] end
Adds a word to the given enum - use judiciously, when building an Enum
from memory. To parse DataMeta DOM source, use the consumed? method.
# File lib/dataMetaDom/enum.rb, line 41 def addKey(word) raise "Duplicate value '#{word}' in the enum '#@name'" if (@map.key?(word)) @counter += 1 @map[word] = @counter # ordinal end
Parses the keys from the current line on the given source, yields one key at a time. Used by the parse method.
-
Parameter:
-
source
- the instance of theSourceFile
.
-
# File lib/dataMetaDom/enum.rb, line 53 def getKeys(source) newVals = source.line.split(/[\s,]+/) puts newVals.join("|") if $DEBUG newVals.each { |v| raise "Invalid worded enum '#{v}', must start with #{ID_START}" \ ", line ##{source.lineNum}" unless v =~ /^#{ID_START}\w*$/ yield v.to_sym } end
Determines if this enum is equal or an extention of the other. Extension means, it contains the whole other enum plus more members at the tail.
Returns: :ne
if neither equal nor extension, :eq
if equal, :xt
if extension.
# File lib/dataMetaDom/enum.rb, line 88 def isEqOrXtOf(other) return :eq if @map.keys == other.rawVals other.rawVals.each_index{ |x| return :ne if @map.keys[x] != other.rawVals[x] } :xt end
All ordinals on this enum, sorted as integer values.
# File lib/dataMetaDom/enum.rb, line 97 def keys; @map.values.sort end
Returns the ordinal enum value for the given word.
# File lib/dataMetaDom/enum.rb, line 67 def ordinal(word); @map[word] end
Parses the given source into current instance. See the consumed? method for the details.
-
Parameter:
-
source
- the instance ofSourceFile
.
-
# File lib/dataMetaDom/enum.rb, line 107 def parse(source) while (line = source.nextLine) case line when /^\s*#{END_KW}\s*$/ self.ver = source.ver unless self.ver raise "Version missing for the enum #{name}" unless self.ver self.docs = source.docs.clone if source.docs source.docs.clear return self else getKeys(source) { |k| addKey k } end # case end # while line self end
Raw values, in the order in which they were inserted
# File lib/dataMetaDom/enum.rb, line 80 def rawVals; @map.keys end
Keyword for this entity - enum
literally.
# File lib/dataMetaDom/enum.rb, line 35 def sourceKeyWord; ENUM end
Textual representation of this enum - a list of words separated by commma.
# File lib/dataMetaDom/enum.rb, line 100 def to_s; "Enum #{@name}(#{@map.keys.join(', ')}, ver=#{self.ver})" end
All words defined on this enum, sorted alphabetically, not by enum order.
# File lib/dataMetaDom/enum.rb, line 77 def values; @map.keys.sort end