Module: KeepYourHead::XmlAccessor

Included in:
Database::BaseItemWithXml
Defined in:
lib/Keepyourhead/database/XmlAccessor.rb

Defined Under Namespace

Classes: XmlAccessorElementList

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.addAttribute(_class, functionName, elementName, notifyEvent, procRead = nil, procWrite = nil) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 83

def self.addAttribute( _class, functionName, elementName, notifyEvent, procRead = nil, procWrite = nil )
  _class.my_define_method(functionName.to_sym) { 
    xmlAccessorAttributeRead elementName, procRead
  }
  _class.my_define_method( (functionName.to_s + "=").to_sym ) { |value| 
    xmlAccessorAttributeWrite elementName, notifyEvent, value, procWrite
  }
end

.addElementCData(_class, functionName, elementName, notifyEvent, procRead = nil, procWrite = nil) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 54

def self.addElementCData( _class, functionName, elementName, notifyEvent, procRead = nil, procWrite = nil )
  _class.my_define_method(functionName.to_sym) { 
    xmlAccessorElementCDataRead elementName, procRead
  }
  _class.my_define_method( (functionName.to_s + "=").to_sym ) { |value| 
    xmlAccessorElementCDataWrite elementName, notifyEvent, value, procWrite
  }
end

.addElementText(_class, functionName, elementName, notifyEvent, procRead = nil, procWrite = nil) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 118

def self.addElementText( _class, functionName, elementName, notifyEvent, procRead = nil, procWrite = nil )
  _class.my_define_method(functionName.to_sym) { 
    xmlAccessorElementTextRead elementName, procRead
  }
  _class.my_define_method( (functionName.to_s + "=").to_sym ) { |value| 
    xmlAccessorElementTextWrite elementName, notifyEvent, value, procWrite
  }
end

Instance Method Details

#xmlAccessorAttributeRead(name, procRead) ⇒ Object



68
69
70
71
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 68

def xmlAccessorAttributeRead( name, procRead )
  ret = @node.attributes[name] 
  (procRead && procRead.call(self, ret) ) || ret
end

#xmlAccessorAttributeWrite(name, notifyEvent, value, procWrite) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 73

def xmlAccessorAttributeWrite( name, notifyEvent, value, procWrite )
  value = (procWrite && procWrite.call(self, value)) || value

  if @node.attributes[name] != value then
    @node.attributes[name] = value

    file.viewersSend notifyEvent, self if notifyEvent
  end
end

#xmlAccessorElementCDataRead(name, procRead) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 21

def xmlAccessorElementCDataRead( name, procRead )
  parent = REXML::XPath.first( node, name )
  ret = parent && parent.children.map{ |c|
    case c
    when REXML::Text
      c.value
    when REXML::CData
      c.value
    else
      nil
    end
  }.compact.join("")
  (procRead && procRead.call(self, ret) ) || ret
end

#xmlAccessorElementCDataWrite(name, notifyEvent, value, procWrite) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 36

def xmlAccessorElementCDataWrite( name, notifyEvent, value, procWrite )
  lastValue = (val = REXML::XPath.first( node, name )) && val.cdatas.map{ |cd| cd.value }.join("")
  
  
  newValue = (procWrite && procWrite.call(self, value)) || value

  node_ = REXML::XPath.first( node, name )
  node_ ||= @node.add REXML::Element.new(name)

  if lastValue != newValue then
    node_.delete_at 0 while node_.length > 0

    node_.add REXML::CData.new(newValue)

    file.viewersSend notifyEvent, self if notifyEvent
  end
end

#xmlAccessorElementListCreate(name, notifyEvent, procCreate) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 240

def xmlAccessorElementListCreate( name, notifyEvent, procCreate )
=begin
  node = REXML::Element.new name

  if self.node[0] then
    self.node.insert_before self.node[0], node
  else
    self.node.add node
  end 
=end

#     node = self.node.add_element name
  node = self.node.add REXML::Element.new(name)

  ret = (procCreate && procCreate.call(self,node)) || node.name

  file.viewersSend notifyEvent, ret if notifyEvent
  
  ret
end

#xmlAccessorElementListInsertAfter(name, notifyEvent, procRead, node, other) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 224

def xmlAccessorElementListInsertAfter( name, notifyEvent, procRead, node, other )
  assert node.kind_of?(REXML::Element)
  assert node.name =~ name
  assert other

  assert other.parent == self
  other.node.parent.insert_after other.node, node
#   other.node.next_sibling = node
  
  ret = (procRead && procRead.call(self,node) ) || node.name
  
  file.viewersSend notifyEvent, ret if notifyEvent

  ret
end

#xmlAccessorElementListInsertBefore(name, notifyEvent, procRead, node, other) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 208

def xmlAccessorElementListInsertBefore( name, notifyEvent, procRead, node, other )
  assert node.kind_of?(REXML::Element)
  assert node.name =~ name
  assert other

  assert other.parent == self
  other.node.parent.insert_before other.node, node
#     other.node.previous_sibling = node
  
  ret = (procRead && procRead.call(self,node) ) || node.name
  
  file.viewersSend notifyEvent, ret if notifyEvent

  ret
end

#xmlAccessorElementListInsertFirst(name, notifyEvent, procRead, node) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 178

def xmlAccessorElementListInsertFirst( name, notifyEvent, procRead, node )
  assert node.kind_of?(REXML::Element)
  assert node.name =~ name

  if self.node[0] then
    self.node.insert_before self.node[0], node
  else
    self.node.add node
  end 
  
  ret = (procRead && procRead.call(self,node) ) || node.name
  
  file.viewersSend notifyEvent, ret if notifyEvent

  ret
end

#xmlAccessorElementListInsertLast(name, notifyEvent, procRead, node) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 195

def xmlAccessorElementListInsertLast( name, notifyEvent, procRead, node )
  assert node.kind_of?(REXML::Element)
  assert node.name =~ name

  self.node.add node
  
  ret = (procRead && procRead.call(self,node) ) || node.name
  
  file.viewersSend notifyEvent, ret if notifyEvent

  ret
end

#xmlAccessorElementListNext(name, procRead, other) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 140

def xmlAccessorElementListNext( name, procRead, other )
  assert other.parent == self

  ret = other.node
  begin 
    ret = ret.next_element
  end while (ret and not ret.name =~ name)

  return nil unless ret

  (procRead && procRead.call(self,ret) ) || ret.name
end

#xmlAccessorElementListPrevious(name, procRead, other) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 153

def xmlAccessorElementListPrevious( name, procRead, other )
  assert other.parent == self

  ret = other.node
  begin 
    ret = ret.previous_element
  end while ret and not ret.name =~ name

  return nil unless ret

  (procRead && procRead.call(self,ret) ) || ret.name
end

#xmlAccessorElementListRead(name, procRead) ⇒ Object



133
134
135
136
137
138
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 133

def xmlAccessorElementListRead( name, procRead )
  ret = @node.elements.select { |node| node.name =~ name }
  ret.map! { |node|
    (procRead && procRead.call(self,node) ) || node.name
  }
end

#xmlAccessorElementListRemove(name, notifyEvent, item) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 166

def xmlAccessorElementListRemove( name, notifyEvent, item )
  assert item.node.name =~ name
  assert item.parent == self
  
  file.viewersSend notifyEvent, item if notifyEvent

  #node.delete_element item.node
  item.node.remove
  
  item.node
end

#xmlAccessorElementTextRead(name, procRead) ⇒ Object



98
99
100
101
102
103
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 98

def xmlAccessorElementTextRead( name, procRead )
  n = REXML::XPath.first( node, name )
  ret = n && n.text

  (procRead && procRead.call(self,ret) ) || ret
end

#xmlAccessorElementTextWrite(name, notifyEvent, value, procWrite) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/Keepyourhead/database/XmlAccessor.rb', line 105

def xmlAccessorElementTextWrite( name, notifyEvent, value, procWrite )
  value = (procWrite && procWrite.call(self,value)) || value
  
  n = REXML::XPath.first( node, name )

  if not n or n.text != value then
    n ||= node.add REXML::Element.new(name)
    n.text=value
  
    file.viewersSend notifyEvent, self if notifyEvent
  end
end