activedocument 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -104,6 +104,15 @@ module ActiveDocument
104
104
  @root
105
105
  end
106
106
 
107
+ def [](key)
108
+ namespace = namespace_for_element(key)
109
+ if namespace.empty?
110
+ @document.root.xpath("@#{key}").to_s
111
+ else
112
+ @document.root.xpath("@ns:#{key}", {'ns' => namespace}).to_s
113
+ end
114
+ end
115
+
107
116
  # enables the dynamic finders
108
117
  def method_missing(method_id, * arguments, & block)
109
118
  @@log.debug("ActiveDocument::Base at line #{__LINE__}: method called is #{method_id} with arguments #{arguments}")
@@ -246,7 +255,7 @@ module ActiveDocument
246
255
  def evaluate_nodeset(result_nodeset)
247
256
  if result_nodeset.length == 1 # found one match
248
257
  if result_nodeset[0].children.length == 1 and result_nodeset[0].children[0].type == Nokogiri::XML::Node::TEXT_NODE
249
- result_nodeset[0].text
258
+ result_nodeset[0]
250
259
  #elsif result_nodeset[0].children.length >1 # we are now dealing with complex nodes
251
260
  else
252
261
  PartialResult.new(result_nodeset, self)
@@ -64,7 +64,7 @@ GENERATED
64
64
  xquery << " xmlns:a=\"#{root_namespace}\"" unless root_namespace.nil?
65
65
  xquery << '>/'
66
66
  xquery << "a:" unless root_namespace.nil?
67
- xquery << "#{root}</searchable-expression>)"
67
+ xquery << "#{root}</searchable-expression>"
68
68
  end
69
69
  xquery << <<CONSTRAINT
70
70
  <constraint name="word">
@@ -13,13 +13,13 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
- # SearchOptions allow you to control exactly how the ActiveDocument::Finder#search method behaves and what additional
16
+ # MarkLogicSearchOptions allow you to control exactly how the ActiveDocument::Finder#search method behaves and what additional
17
17
  # information may be returned in the ActiveDocument::SearchResults object
18
18
  # == Attributes
19
19
  # * return_facets - if true then facet information is returned in the resultant ActiveDocument::SearchResults object. Default is true
20
20
  # * value_constraints - this is a #Hash of value constraint names to their options. e.g. search_options_object.value_constraints["Region"] = {"namespace" => "http://wits.nctc.gov", "element" => "Region"}
21
21
  module ActiveDocument
22
- class SearchOptions
22
+ class MarkLogicSearchOptions
23
23
  attr_accessor :return_facets, :value_constraints, :word_constraints, :range_constraints
24
24
 
25
25
  def initialize
@@ -65,9 +65,14 @@ module ActiveDocument
65
65
  constraints << <<-XML
66
66
  >
67
67
  <element ns="#{value["namespace"]}" name="#{value["element"]}"/>
68
- </range>
69
- </constraint>
70
68
  XML
69
+
70
+ if value.has_key?("computed_buckets")
71
+ value["computed_buckets"].each do |computed_bucket|
72
+ constraints << computed_bucket.to_s if computed_bucket.instance_of?(ActiveDocument::MarkLogicSearchOptions::ComputedBucket)
73
+ end
74
+ end
75
+ constraints << "</range></constraint>"
71
76
  end
72
77
 
73
78
  value = <<-XML
@@ -83,5 +88,26 @@ module ActiveDocument
83
88
  value << "</options>"
84
89
 
85
90
  end
91
+
92
+ #end to_s
93
+
94
+ class ComputedBucket
95
+ attr_accessor :name, :ge, :lt, :anchor, :title
96
+
97
+ def initialize (name, ge, lt, anchor, title)
98
+ @name = name
99
+ @ge = ge
100
+ @lt = lt
101
+ @anchor = anchor
102
+ @title = title
103
+ end
104
+
105
+ def to_s
106
+ <<-XML
107
+ <computed-bucket name="#{@name}" ge="#{@ge}" lt="#{@lt}" anchor="#{@anchor}">#{@title}</computed-bucket>
108
+ XML
109
+ end
110
+ end
86
111
  end
112
+
87
113
  end
@@ -45,16 +45,24 @@ module ActiveDocument
45
45
  end
46
46
 
47
47
  def fitness
48
- Integer(@node.xpath("./@fitness").to_s)
48
+ Float(@node.xpath("./@fitness").to_s)
49
49
  end
50
50
 
51
51
  def each(&block)
52
- @node.xpath("./search:snippet/search:match").each {|node| yield SearchMatch.new(node)}
52
+ nodeset = @node.xpath("./search:snippet/search:match")
53
+ if nodeset.length == 1
54
+ yield SearchMatch.new(nodeset[0])
55
+ else
56
+ @node.xpath("./search:snippet/search:match").each {|node| yield SearchMatch.new(node)}
57
+ end
58
+
59
+
60
+ # @node.xpath("./search:snippet/search:match").each {|node| yield SearchMatch.new(node)}
53
61
  end
54
62
 
55
63
  def root_type
56
- full_path = @node.xpath("./search:snippet/search:match")[1].xpath("./@path").to_s
57
- root = full_path.match(/:[[:alpha:]]+\//) # find the first :something/ which should indicate the root
64
+ full_path = @node.xpath("./search:snippet/search:match")[0].xpath("./@path").to_s
65
+ root = full_path.match(/:[[:alpha:]]+\/|:[[:alpha:]]+$/) # find the first :something/ which should indicate the root
58
66
  root.to_s.delete(":/") # remove the : and / to get the root element name
59
67
  end
60
68
 
@@ -62,6 +70,9 @@ module ActiveDocument
62
70
  SearchMatch.new(@node.xpath("./search:snippet/search:match")[index])
63
71
  end
64
72
 
73
+ def length
74
+ @node.xpath("./search:snippet/search:match").length
75
+ end
65
76
  def realize(klass)
66
77
  klass.load(uri)
67
78
  end
@@ -67,11 +67,20 @@ module ActiveDocument
67
67
  end
68
68
 
69
69
  def each(&block)
70
- @results_document.xpath("/search:response/search:result").each {|node| yield SearchResult.new(node)}
70
+ nodeset = @results_document.xpath("/search:response/search:result")
71
+ if nodeset.length == 1
72
+ yield SearchResult.new(nodeset[0])
73
+ else
74
+ @results_document.xpath("/search:response/search:result").each {|node| yield SearchResult.new(node)}
75
+ end
71
76
  end
72
77
 
73
78
  def [](index)
74
- @results_document[index]
79
+ SearchResult.new(@results_document.xpath("/search:response/search:result")[index])
80
+ end
81
+
82
+ def length
83
+ @results_document.xpath("/search:response/search:result").length
75
84
  end
76
85
 
77
86
  end
@@ -4,7 +4,7 @@ require 'ActiveDocument/inheritable'
4
4
  require 'ActiveDocument/mark_logic_http'
5
5
  require 'ActiveDocument/mark_logic_query_builder'
6
6
  require 'ActiveDocument/search_match'
7
- require 'ActiveDocument/search_options'
7
+ require 'ActiveDocument/mark_logic_search_options'
8
8
  require 'ActiveDocument/search_result'
9
9
  require 'ActiveDocument/search_results'
10
10
 
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
8
- version: "0.3"
7
+ - 4
8
+ version: "0.4"
9
9
  platform: ruby
10
10
  authors:
11
11
  - Clark D. Richey, Jr.
@@ -13,7 +13,7 @@ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
 
16
- date: 2010-06-01 00:00:00 -04:00
16
+ date: 2010-06-25 00:00:00 -07:00
17
17
  default_executable:
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
@@ -45,8 +45,8 @@ files:
45
45
  - lib/ActiveDocument/inheritable.rb
46
46
  - lib/ActiveDocument/mark_logic_http.rb
47
47
  - lib/ActiveDocument/mark_logic_query_builder.rb
48
+ - lib/ActiveDocument/mark_logic_search_options.rb
48
49
  - lib/ActiveDocument/search_match.rb
49
- - lib/ActiveDocument/search_options.rb
50
50
  - lib/ActiveDocument/search_result.rb
51
51
  - lib/ActiveDocument/search_results.rb
52
52
  - lib/activedocument.rb