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

name[R]

The full name of the enum, including namespace if any

Public Class Methods

consumed?(model, src) click to toggle source

Attempts to consume/parse an enum from the given source on the given model, returns it if succeeds, returns nil otherwise.

# 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
new(name) click to toggle source

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

[](ord) click to toggle source

Opposite to ordinal, for the given ordinal returns the word.

# File lib/dataMetaDom/enum.rb, line 72
def [](ord); @map.invert[ord] end
addKey(word) click to toggle source

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
getKeys(source) { |to_sym| ... } click to toggle source

Parses the keys from the current line on the given source, yields one key at a time. Used by the parse method.

  • Parameter:

# 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
isEqOrXtOf(other) click to toggle source

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
keys() click to toggle source

All ordinals on this enum, sorted as integer values.

# File lib/dataMetaDom/enum.rb, line 97
def keys; @map.values.sort end
ordinal(word) click to toggle source

Returns the ordinal enum value for the given word.

# File lib/dataMetaDom/enum.rb, line 67
def ordinal(word); @map[word] end
parse(source) click to toggle source

Parses the given source into current instance. See the consumed? method for the details.

# 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
rawVals() click to toggle source

Raw values, in the order in which they were inserted

# File lib/dataMetaDom/enum.rb, line 80
def rawVals; @map.keys end
sourceKeyWord() click to toggle source

Keyword for this entity - enum literally.

# File lib/dataMetaDom/enum.rb, line 35
def sourceKeyWord; ENUM end
to_s() click to toggle source

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
values() click to toggle source

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