Class: DataMetaDom::Enum
- Inherits:
-
VerDoccable
- Object
- Documentable
- VerDoccable
- DataMetaDom::Enum
- Defined in:
- lib/dataMetaDom/enum.rb
Overview
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.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The full name of the enum, including namespace if any.
Attributes inherited from VerDoccable
Attributes inherited from Documentable
Class Method Summary collapse
-
.consumed?(model, src) ⇒ Boolean
Attempts to consume/parse an enum from the given source on the given model, returns it if succeeds, returns
nil
otherwise.
Instance Method Summary collapse
-
#[](ord) ⇒ Object
Opposite to ordinal, for the given ordinal returns the word.
-
#addKey(word) ⇒ Object
Adds a word to the given enum - use judiciously, when building an Enum from memory.
-
#getKeys(source) ⇒ Object
Parses the keys from the current line on the given source, yields one key at a time.
-
#initialize(name) ⇒ Enum
constructor
Creates an instance for the given name, initializes internal variables.
-
#isEqOrXtOf(other) ⇒ Object
Determines if this enum is equal or an extention of the other.
-
#keys ⇒ Object
All ordinals on this enum, sorted as integer values.
-
#ordinal(word) ⇒ Object
Returns the ordinal enum value for the given word.
-
#parse(source) ⇒ Object
Parses the given source into current instance.
-
#rawVals ⇒ Object
Raw values, in the order in which they were inserted.
-
#sourceKeyWord ⇒ Object
Keyword for this entity -
enum
literally. -
#to_s ⇒ Object
Textual representation of this enum - a list of words separated by commma.
-
#values ⇒ Object
All words defined on this enum, sorted alphabetically, not by enum order.
Methods inherited from VerDoccable
Methods inherited from Documentable
#addDoc, #all, #clear, #docConsumed?, #getDoc, #has?, #ids
Constructor Details
#initialize(name) ⇒ Enum
Creates an instance for the given name, initializes internal variables.
32 |
# File 'lib/dataMetaDom/enum.rb', line 32 def initialize(name); @name = name.to_sym; @map = {}; @format = nil; @counter = -1 end |
Instance Attribute Details
#name ⇒ Object (readonly)
The full name of the enum, including namespace if any
16 17 18 |
# File 'lib/dataMetaDom/enum.rb', line 16 def name @name end |
Class Method Details
.consumed?(model, src) ⇒ Boolean
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
-
24 25 26 27 |
# 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., src), $1)).parse(src)) : nil end |
Instance Method Details
#[](ord) ⇒ Object
Opposite to ordinal, for the given ordinal returns the word.
72 |
# File 'lib/dataMetaDom/enum.rb', line 72 def [](ord); @map.invert[ord] end |
#addKey(word) ⇒ Object
Adds a word to the given enum - use judiciously, when building an Enum from memory. To parse DataMeta DOM source, use the consumed? method.
41 42 43 44 45 |
# 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) ⇒ Object
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 the SourceFile.
-
53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Object
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.
88 89 90 91 92 93 94 |
# 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 ⇒ Object
All ordinals on this enum, sorted as integer values.
97 |
# File 'lib/dataMetaDom/enum.rb', line 97 def keys; @map.values.sort end |
#ordinal(word) ⇒ Object
Returns the ordinal enum value for the given word.
67 |
# File 'lib/dataMetaDom/enum.rb', line 67 def ordinal(word); @map[word] end |
#parse(source) ⇒ Object
Parses the given source into current instance. See the consumed? method for the details.
-
Parameter:
-
source
- the instance of SourceFile.
-
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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 ⇒ Object
Raw values, in the order in which they were inserted
80 |
# File 'lib/dataMetaDom/enum.rb', line 80 def rawVals; @map.keys end |
#sourceKeyWord ⇒ Object
Keyword for this entity - enum
literally.
35 |
# File 'lib/dataMetaDom/enum.rb', line 35 def sourceKeyWord; ENUM end |
#to_s ⇒ Object
Textual representation of this enum - a list of words separated by commma.
100 |
# File 'lib/dataMetaDom/enum.rb', line 100 def to_s; "Enum #{@name}(#{@map.keys.join(', ')}, ver=#{self.ver})" end |
#values ⇒ Object
All words defined on this enum, sorted alphabetically, not by enum order.
77 |
# File 'lib/dataMetaDom/enum.rb', line 77 def values; @map.keys.sort end |