70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/polyrex-objects.rb', line 70
def initialize(schema, id='0', node=nil)
@node = node
@@id = id
if schema then
a = schema.split('/')
a.shift
@class_names = []
a.each do |x|
name, raw_fields = x.split('[')
if raw_fields then
fields = raw_fields.chop.split(',').map &:strip
@class_names << name.capitalize
classx = []
classx << "class #{name.capitalize} < PolyrexObject"
classx << "def initialize(node=nil, id='0')"
classx << "super(node,id)"
classx << "a = node.xpath('summary/*',&:name)"
classx << "yaml_fields = a - (#{fields} + %w(format_mask))"
classx << "yaml_fields.each do |field|"
classx << %q(instance_eval "def #{field}; YAML.load(@node.element('summary/#{field}/text()')); end")
classx << "end"
classx << "@fields = %i(#{fields.join(' ')})"
classx << "@create = PolyrexCreateObject.new('#{schema}', @@id)"
classx << "end"
fields.each do |field|
classx << "def #{field}"
classx << " if @node.element('summary/#{field}').nil? then"
classx << " @node.element('summary').add Rexle::Element.new('#{field}')"
classx << " end"
classx << " @node.element('summary/#{field}/text()').clone"
classx << "end"
classx << "def #{field}=(text)"
classx << " if @node.element('summary/#{field}').nil? then"
classx << " @node.element('summary').add Rexle::Element.new('#{field}', text)"
classx << " else"
classx << " @node.element('summary/#{field}').text = text"
classx << " end"
classx << "end"
end
classx << "end"
eval classx.join("\n")
end
end
if @class_names.length < 2 then
make_def_records(@class_names.first)
else
@class_names[0..-2].reverse.each_with_index do |class_name, k|
i = @class_names.length - (k + 1)
make_def_records(class_name,i)
end
end
@class_names[1..-1].each_with_index do |class_name, k|
eval "#{class_name}.class_eval {
def parent()
#{@class_names[k]}.new(@node.parent.parent, @@id)
end
}"
end
methodx = @class_names.map do |name|
%Q(def #{name.downcase}(); #{name}.new(@node, @@id); end)
end
self.instance_eval(methodx.join("\n"))
end
end
|