Class: RGen::Instantiator::DefaultXMLInstantiator
Overview
A default XML instantiator. Derive your own instantiator from this class or use it as is.
Defined Under Namespace
Classes: NamespaceDescriptor
Class Method Summary
collapse
Instance Method Summary
collapse
-
#assoc_p2c(parent, child) ⇒ Object
-
#build_attribute(node, attr, value) ⇒ Object
-
#build_class(name, mod) ⇒ Object
-
#build_p2c_assoc(parent, child, method_name) ⇒ Object
-
#class_name(str) ⇒ Object
-
#initialize(env, default_module, create_mm = false) ⇒ DefaultXMLInstantiator
constructor
A new instance of DefaultXMLInstantiator.
-
#method_name(str) ⇒ Object
-
#new_object(node) ⇒ Object
-
#on_ascent(node) ⇒ Object
-
#on_descent(node) ⇒ Object
-
#set_attribute(node, attr, value) ⇒ Object
#camelize, #className, #firstToLower, #firstToUpper, #normalize, #saneClassName, #saneMethodName
#end_element, #instantiate, #instantiate_file, #namespaces, #on_chardata, #parse, prune_level, set_prune_level, #start_element
resolve, resolve_by_id
Constructor Details
#initialize(env, default_module, create_mm = false) ⇒ DefaultXMLInstantiator
Returns a new instance of DefaultXMLInstantiator.
28
29
30
31
32
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 28
def initialize(env, default_module, create_mm=false)
super(env)
@default_module = default_module
@create_mm = create_mm
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class RGen::Instantiator::AbstractInstantiator
Class Method Details
.map_tag_ns(from, to, prefix = "") ⇒ Object
17
18
19
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 17
def map_tag_ns(from, to, prefix="")
tag_ns_map[from] = NamespaceDescriptor.new(prefix, to)
end
|
.tag_ns_map ⇒ Object
21
22
23
24
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 21
def tag_ns_map @tag_ns_map ||={}
@tag_ns_map
end
|
Instance Method Details
#assoc_p2c(parent, child) ⇒ Object
73
74
75
76
77
78
79
80
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 73
def assoc_p2c(parent, child)
return unless parent.object && child.object
method_name = method_name(className(child.object))
build_on_error(NoMethodError, :build_p2c_assoc, parent, child, method_name) do
parent.object.addGeneric(method_name, child.object)
child.object.setGeneric("parent", parent.object)
end
end
|
#build_attribute(node, attr, value) ⇒ Object
94
95
96
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 94
def build_attribute(node, attr, value)
node.object.class.has_attr(method_name(attr))
end
|
#build_class(name, mod) ⇒ Object
65
66
67
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 65
def build_class(name, mod)
mod.const_set(name, Class.new(RGen::MetamodelBuilder::MMBase))
end
|
#build_p2c_assoc(parent, child, method_name) ⇒ Object
82
83
84
85
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 82
def build_p2c_assoc(parent, child, method_name)
parent.object.class.has_many(method_name, child.object.class)
child.object.class.has_one("parent", RGen::MetamodelBuilder::MMBase)
end
|
#class_name(str) ⇒ Object
47
48
49
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 47
def class_name(str)
saneClassName(str)
end
|
#method_name(str) ⇒ Object
69
70
71
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 69
def method_name(str)
saneMethodName(str)
end
|
#new_object(node) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 51
def new_object(node)
ns_desc = self.class.tag_ns_map[node.namespace]
class_name = class_name(ns_desc.nil? ? node.qtag : ns_desc.prefix+node.tag)
mod = (ns_desc && ns_desc.target) || @default_module
build_on_error(NameError, :build_class, class_name, mod) do
begin
mod.const_get(class_name, false).new
rescue ArgumentError
mod.const_get(class_name).new
end
end
end
|
#on_ascent(node) ⇒ Object
41
42
43
44
45
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 41
def on_ascent(node)
node.children.each { |c| assoc_p2c(node, c) }
node.object.class.has_attr 'chardata', Object unless node.object.respond_to?(:chardata)
set_attribute(node, "chardata", node.chardata)
end
|
#on_descent(node) ⇒ Object
34
35
36
37
38
39
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 34
def on_descent(node)
obj = new_object(node)
@env << obj unless obj.nil?
node.object = obj
node.attributes.each_pair { |k,v| set_attribute(node, k, v) }
end
|
#set_attribute(node, attr, value) ⇒ Object
87
88
89
90
91
92
|
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 87
def set_attribute(node, attr, value)
return unless node.object
build_on_error(NoMethodError, :build_attribute, node, attr, value) do
node.object.setGeneric(method_name(attr), value)
end
end
|