Class: Lafcadio::ClassDefinitionXmlParser::FieldAttribute

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

Constant Summary collapse

INTEGER =
1
BOOLEAN =
2
ENUM =
3
HASH =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, valueClass, objectFieldClass = nil) ⇒ FieldAttribute

Returns a new instance of FieldAttribute.



19
20
21
22
# File 'lib/lafcadio/domain.rb', line 19

def initialize( name, valueClass, objectFieldClass = nil )
	@name = name; @valueClass = valueClass
	@objectFieldClass = objectFieldClass
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/lafcadio/domain.rb', line 17

def name
  @name
end

#valueClassObject (readonly)

Returns the value of attribute valueClass.



17
18
19
# File 'lib/lafcadio/domain.rb', line 17

def valueClass
  @valueClass
end

Instance Method Details

#maybeSetFieldAttr(field, fieldElt) ⇒ Object



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

def maybeSetFieldAttr( field, fieldElt )
	setterMethod = "#{ name }="
	if field.respond_to?( setterMethod )
		if valueClass != FieldAttribute::HASH
			if ( attrStr = fieldElt.attributes[name] )
				field.send( setterMethod, valueFromString( attrStr ) )
			end
		else
			if ( attrElt = fieldElt.elements[name] )
				field.send( setterMethod, valueFromElt( attrElt ) )
			end
		end
	end
end

#valueFromElt(elt) ⇒ Object



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

def valueFromElt( elt )
	hash = {}
	elt.elements.each( English.singular( @name ) ) { |subElt|
		key = subElt.attributes['key'] == 'true'
		value = subElt.text.to_s
		hash[key] = value
	}
	hash
end

#valueFromString(valueStr) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/lafcadio/domain.rb', line 24

def valueFromString( valueStr )
	if @valueClass == INTEGER
		valueStr.to_i
	elsif @valueClass == BOOLEAN
		valueStr == 'y'
	elsif @valueClass == ENUM
		eval "#{ @objectFieldClass.name }::#{ valueStr }"
	end
end