Method: ActiveDocument::MarkLogicSearchOptions#to_s

Defined in:
lib/ActiveDocument/mark_logic_search_options.rb

#to_sObject

outputs the object in correctly formatted XML suitable for use in a search



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/ActiveDocument/mark_logic_search_options.rb', line 50

def to_s
  constraints = String.new
  additional_query = String.new
  searchable_path = String.new

  @value_constraints.each do |key, value|
    constraints << <<-XML
    <constraint name="#{key.gsub(/\s/, '_')}">
      <value>
        <element ns="#{value["namespace"]}" name="#{value["element"]}"/>
      </value>
    </constraint>
    XML
  end

  @word_constraints.each do |key, value|
    constraints << <<-XML
    <constraint name="#{key.gsub(/\s/, '_')}">
      <word>
        <element ns="#{value["namespace"]}" name="#{value["element"]}"/>
      </word>
    </constraint>
    XML
  end

  @attribute_constraints.each do |constraint_name, attribute_constraint|
    constraints << <<-XML
    <constraint name="#{constraint_name.gsub(/\s/, '_')}">
      <word>
        <attribute ns="#{attribute_constraint.attribute_namespace unless attribute_constraint.attribute_namespace.nil?}" name="#{attribute_constraint.attribute}"/>
      <element ns="#{attribute_constraint.element_namespace unless attribute_constraint.element_namespace.nil?}" name="#{attribute_constraint.element}"/>
      </word>
    </constraint>
    XML
  end

  @range_constraints.each do |key, value|
    constraints << <<-XML
    <constraint name="#{key.gsub(/\s/, '_')}">
      <range type="#{value["type"]}"
    XML
    if value.has_key?("collation")
      constraints << "collation=\"#{value["collation"]}\""
    end

    constraints << <<-XML
        >
        <element ns="#{value["namespace"]}" name="#{value["element"]}"/>
    XML

    if value.has_key?("computed_buckets")
      value["computed_buckets"].each do |computed_bucket|
        constraints << computed_bucket.to_s if computed_bucket.instance_of?(ActiveDocument::MarkLogicSearchOptions::ComputedBucket)
      end
    end
    constraints << "</range></constraint>"
  end

  # serialize the additional query information
  if @directory_constraint
    additional_query = "<additional-query>{cts:directory-query(\"#{directory_constraint}\", \"#{directory_depth}\")}</additional-query>"
  end

  # serialize the searchable_expression
  if @searchable_expression.size > 0
    searchable_path << "<searchable-expression"
    searchable_path << "  xmlns:a=\"#{@searchable_expression.keys[0]}\"" unless @searchable_expression.keys[0].nil? or @searchable_expression.keys[0].empty?
    searchable_path << '>/'
    searchable_path << "a:" unless @searchable_expression.keys[0].nil? or @searchable_expression.keys[0].empty?
    searchable_path << "#{@searchable_expression.values[0]}</searchable-expression>"
  end

  value = "<options xmlns=\"http://marklogic.com/appservices/search\">"
  unless additional_query.empty?
    value << additional_query
  end

  # add in constraints
  unless constraints.empty?
    value << constraints
  end
  value << "<return-facets>#{@return_facets}</return-facets>"

  # output the searchable expression
  unless searchable_path.empty?
    value << searchable_path
  end
  # close the options node
  value << "</options>"

end