Class: RGen::MetamodelBuilder::DataTypes::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/rgen/metamodel_builder/data_types.rb

Overview

An enum object is used to describe possible attribute values within a MetamodelBuilder attribute definition. An attribute defined this way can only take the values specified when creating the Enum object. Literal values can only be symbols or true or false. Optionally a name may be specified for the enum object.

Examples:

Enum.new(:name => “AnimalEnum”, :literals => [:cat, :dog]) Enum.new(:literals => [:cat, :dog]) Enum.new([:cat, :dog])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Enum

Creates a new named enum type object consisting of the elements passed as arguments.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rgen/metamodel_builder/data_types.rb', line 23

def initialize(params)
   MetamodelBuilder::ConstantOrderHelper.enumCreated(self)
	if params.is_a?(Array)
   @literals = params
		@name = "anonymous"
	elsif params.is_a?(Hash)
		raise StandardError.new("Hash entry :literals is missing") unless params[:literals]
		@literals = params[:literals]
		@name = params[:name] || "anonymous"
	else
		raise StandardError.new("Pass an Array or a Hash")
	end
end

Instance Attribute Details

#literalsObject (readonly)

Returns the value of attribute literals.



20
21
22
# File 'lib/rgen/metamodel_builder/data_types.rb', line 20

def literals
  @literals
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/rgen/metamodel_builder/data_types.rb', line 20

def name
  @name
end

Instance Method Details

#literals_as_stringsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rgen/metamodel_builder/data_types.rb', line 43

def literals_as_strings
	literals.collect do |l|
		if l.is_a?(Symbol)
       if l.to_s =~ /^\d|\W/
         ":'"+l.to_s+"'"
       else
         ":"+l.to_s
       end
		elsif l.is_a?(TrueClass) || l.is_a?(FalseClass)
			l.to_s
		else
			raise StandardError.new("Literal values can only be symbols or true/false")
		end
	end
end

#to_sObject

:nodoc:



59
60
61
# File 'lib/rgen/metamodel_builder/data_types.rb', line 59

def to_s # :nodoc:
	name
end

#validLiteral?(l) ⇒ Boolean

This method can be used to check if an object can be used as value for variables having this enum object as type.

Returns:



39
40
41
# File 'lib/rgen/metamodel_builder/data_types.rb', line 39

def validLiteral?(l)
  literals.include?(l)
end