activedocument 0.4 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,10 +13,10 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- require 'ActiveDocument/mark_logic_http'
17
16
  require 'rubygems'
18
17
  require 'nokogiri'
19
18
  require 'yaml'
19
+ require 'ActiveDocument/mark_logic_http'
20
20
  require 'ActiveDocument/mark_logic_query_builder'
21
21
  require 'ActiveDocument/search_results'
22
22
  require 'ActiveDocument/finder'
@@ -113,6 +113,10 @@ module ActiveDocument
113
113
  end
114
114
  end
115
115
 
116
+ def []=(key, value)
117
+ set_attribute(key, value)
118
+ end
119
+
116
120
  # enables the dynamic finders
117
121
  def method_missing(method_id, * arguments, & block)
118
122
  @@log.debug("ActiveDocument::Base at line #{__LINE__}: method called is #{method_id} with arguments #{arguments}")
@@ -124,6 +128,8 @@ module ActiveDocument
124
128
  access_element $1
125
129
  elsif method =~ /^(\w*)=$/ && arguments.length == 1 # methods with no '.' in them and ending in '='
126
130
  set_element($1, arguments)
131
+ else
132
+ super
127
133
  end
128
134
  end
129
135
 
@@ -205,10 +211,16 @@ module ActiveDocument
205
211
 
206
212
 
207
213
  class PartialResult < self
214
+ include Enumerable
208
215
  # todo should this contain a reference to its parent?
209
216
  def initialize(nodeset, parent)
210
217
  @document = nodeset
211
- @root = nodeset[0].name
218
+ @root =
219
+ if nodeset.instance_of? Nokogiri::XML::Element then
220
+ nodeset.name
221
+ else
222
+ nodeset[0].name
223
+ end
212
224
  @my_namespaces = parent.class.my_namespaces
213
225
  @my_default_namespace = parent.class.my_default_namespace
214
226
  end
@@ -228,6 +240,14 @@ module ActiveDocument
228
240
  @document[index]
229
241
  end
230
242
 
243
+ def text
244
+ @document.text
245
+ end
246
+
247
+ def each(&block)
248
+ @document.each(&block)
249
+ end
250
+
231
251
  end
232
252
 
233
253
  private
@@ -253,26 +273,14 @@ module ActiveDocument
253
273
  end
254
274
 
255
275
  def evaluate_nodeset(result_nodeset)
256
- if result_nodeset.length == 1 # found one match
257
- if result_nodeset[0].children.length == 1 and result_nodeset[0].children[0].type == Nokogiri::XML::Node::TEXT_NODE
258
- result_nodeset[0]
259
- #elsif result_nodeset[0].children.length >1 # we are now dealing with complex nodes
260
- else
261
- PartialResult.new(result_nodeset, self)
262
- end
263
- elsif result_nodeset.length >1 # multiple matches
264
- if result_nodeset.all? { |node| node.children.length == 1 } and result_nodeset.all? { |node| node.children[0].type == Nokogiri::XML::Node::TEXT_NODE }
265
- # we have multiple simple text nodes
266
- result_nodeset.collect { |node| node.text }
267
- else
268
- # we have multiple complex elements
269
- PartialResult.new(result_nodeset, self)
270
- end
276
+ if result_nodeset.length == 1 and result_nodeset[0].children.length == 1 and result_nodeset[0].children[0].type == Nokogiri::XML::Node::TEXT_NODE
277
+ PartialResult.new(result_nodeset[0], self)
278
+ else
279
+ PartialResult.new(result_nodeset, self)
271
280
  end
272
281
  end
273
282
 
274
283
  def set_element(element, value)
275
- # element.chop!
276
284
  xpath, namespace = xpath_for_element(element)
277
285
  if namespace.nil?
278
286
  node = @document.xpath(xpath)
@@ -286,6 +294,17 @@ module ActiveDocument
286
294
  end
287
295
  end
288
296
 
297
+ def set_attribute(attribute, value)
298
+ namespace = namespace_for_element(attribute)
299
+ node = if namespace.empty?
300
+ @document.xpath("@#{attribute}")
301
+ else
302
+ @document.xpath("@ns:#{attribute}", {'ns' => namespace})
303
+ end
304
+ node[0].child.content = value
305
+
306
+ end
307
+
289
308
  def access_element(element)
290
309
  xpath, namespace = xpath_for_element(element)
291
310
  if namespace.nil?
@@ -293,6 +312,7 @@ module ActiveDocument
293
312
  else
294
313
  nodes = @document.xpath(xpath, {'ns' => namespace})
295
314
  end
315
+ # PartialResult.new(nodes, self)
296
316
  evaluate_nodeset(nodes)
297
317
 
298
318
  end
@@ -311,4 +331,4 @@ module ActiveDocument
311
331
  end
312
332
 
313
333
 
314
- end # end module
334
+ end # end module
@@ -12,10 +12,10 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require 'ActiveDocument/mark_logic_http'
16
- require 'ActiveDocument/mark_logic_query_builder'
17
15
  require 'rubygems'
18
16
  require 'nokogiri'
17
+ require 'ActiveDocument/mark_logic_query_builder'
18
+ require 'ActiveDocument/mark_logic_http'
19
19
  require 'ActiveDocument/search_results'
20
20
  require 'logger'
21
21
 
@@ -41,7 +41,7 @@ module ActiveDocument
41
41
  namespace = arguments[1] if arguments.length == 2
42
42
  execute_finder($1.to_sym, arguments[0], nil, namespace)
43
43
  else
44
- puts "missed"
44
+ super
45
45
  end
46
46
  end
47
47
 
@@ -55,9 +55,6 @@ module ActiveDocument
55
55
  else
56
56
  @node.xpath("./search:snippet/search:match").each {|node| yield SearchMatch.new(node)}
57
57
  end
58
-
59
-
60
- # @node.xpath("./search:snippet/search:match").each {|node| yield SearchMatch.new(node)}
61
58
  end
62
59
 
63
60
  def root_type
metadata CHANGED
@@ -5,7 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- version: "0.4"
8
+ - 1
9
+ version: 0.4.1
9
10
  platform: ruby
10
11
  authors:
11
12
  - Clark D. Richey, Jr.
@@ -13,7 +14,7 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2010-06-25 00:00:00 -07:00
17
+ date: 2010-07-19 00:00:00 -04:00
17
18
  default_executable:
18
19
  dependencies:
19
20
  - !ruby/object:Gem::Dependency
@@ -58,7 +59,7 @@ post_install_message:
58
59
  rdoc_options: []
59
60
 
60
61
  require_paths:
61
- - .
62
+ - lib
62
63
  required_ruby_version: !ruby/object:Gem::Requirement
63
64
  requirements:
64
65
  - - ">="