Class: Lafcadio::ClassDefinitionXmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lafcadio/domain.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: FieldAttribute, InvalidDataError

Instance Method Summary collapse

Constructor Details

#initialize(domain_class, xml) ⇒ ClassDefinitionXmlParser

Returns a new instance of ClassDefinitionXmlParser.



9
10
11
12
13
# File 'lib/lafcadio/domain.rb', line 9

def initialize( domain_class, xml )
	@domain_class = domain_class
	@xmlDocRoot = REXML::Document.new( xml ).root
	@namesProcessed = {}
end

Instance Method Details

#get_class_field(fieldElt) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lafcadio/domain.rb', line 15

def get_class_field( fieldElt )
	className = fieldElt.attributes['class'].to_s
	name = fieldElt.attributes['name']
	if className != ''
		fieldClass = Class.by_name( 'Lafcadio::' + className )
		register_name( name )
		field = fieldClass.create_from_xml( @domain_class, fieldElt )
		set_field_attributes( field, fieldElt )
	else
		msg = "Couldn't find field class '#{ className }' for field " +
		      "'#{ name }'"
		raise( MissingError, msg, caller )
	end
	field
end

#get_class_fieldsObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lafcadio/domain.rb', line 31

def get_class_fields
	namesProcessed = {}
	pk_field = PrimaryKeyField.new( @domain_class )
	if ( spkn = @xmlDocRoot.attributes['sql_primary_key_name'] )
		pk_field.db_field_name = spkn
	end
	fields = [ pk_field ]
	@xmlDocRoot.elements.each('field') { |fieldElt|
		fields << get_class_field( fieldElt )
	}
	fields
end

#possible_field_attributesObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/lafcadio/domain.rb', line 44

def possible_field_attributes
	fieldAttr = []
	fieldAttr << FieldAttribute.new( 'size', :integer )
	fieldAttr << FieldAttribute.new( 'unique', :boolean )
	fieldAttr << FieldAttribute.new( 'not_nil', :boolean )
	fieldAttr << FieldAttribute.new( 'enum_type', :enum, BooleanField )
	fieldAttr << FieldAttribute.new( 'enums', :hash )
	fieldAttr << FieldAttribute.new( 'range', :enum, DateField )
	fieldAttr << FieldAttribute.new( 'large', :boolean )
end

#register_name(name) ⇒ Object

Raises:



55
56
57
58
# File 'lib/lafcadio/domain.rb', line 55

def register_name( name )
	raise InvalidDataError if @namesProcessed[name]
	@namesProcessed[name] = true
end

#set_field_attributes(field, fieldElt) ⇒ Object



60
61
62
63
64
# File 'lib/lafcadio/domain.rb', line 60

def set_field_attributes( field, fieldElt )
	possible_field_attributes.each { |fieldAttr|
		fieldAttr.maybe_set_field_attr( field, fieldElt )
	}
end

#table_nameObject



66
67
68
# File 'lib/lafcadio/domain.rb', line 66

def table_name
	@xmlDocRoot.attributes['table_name']
end