Class: XTF::Search::Element::Clause

Inherits:
Base
  • Object
show all
Defined in:
lib/xtf/search/element/clause.rb

Direct Known Subclasses

And, Near, Not, Or, Phrase, Range

Constant Summary collapse

VALID_TAG_NAMES =
%w{phrase exact and or or_near orNear not near range}

Constants inherited from Base

Base::BASE_ATTRIBUTE_KEYS

Instance Attribute Summary collapse

Attributes inherited from Base

#tag_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attribute_keys, #attributes

Constructor Details

#initialize(*args) ⇒ Clause

Returns a new instance of Clause.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
# File 'lib/xtf/search/element/clause.rb', line 27

def initialize(*args)
  params = args[0] || {}
  self.content = params.delete(:content) || []
  self.term = params.delete(:term) if params.has_key?(:term)
  super(params)

  raise ArgumentError, "need tag_name for XTF::Search::Element::Clause (maybe you should call Clause.create(:tag_name) ? )" unless @tag_name
  raise ArgumentError, "tag_name #{@tag_name} not valid for XTF::Search::Element::Clause. Must be one of: #{VALID_TAG_NAMES.join(', ')}" unless VALID_TAG_NAMES.include?(@tag_name)
end

Instance Attribute Details

#contentObject

an Array that contains any number of clauses and/or terms



5
6
7
# File 'lib/xtf/search/element/clause.rb', line 5

def content
  @content
end

#section_typeObject

available on all elements except <not> and <facet>



10
11
12
# File 'lib/xtf/search/element/clause.rb', line 10

def section_type
  @section_type
end

#termObject

convenience to create a Term from a String and insert it into content



8
9
10
# File 'lib/xtf/search/element/clause.rb', line 8

def term
  @term
end

Class Method Details

.create(*args) ⇒ Object

This is a factory method for creating subclasses directly from Clause. The tag_name may be passed as the first argument or as the value to the key :tag_name or ‘tag_name’

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/xtf/search/element/clause.rb', line 15

def self.create(*args)
  tag_name = args.shift.to_s if args[0].is_a?(String) || args[0].is_a?(Symbol)
  params = (args[0] || {}).symbolize_keys
  tag_name = params.delete(:tag_name) unless tag_name

  raise ArgumentError, "need tag_name for XTF::Search::Element::Clause" unless tag_name
  raise ArgumentError, "tag_name #{tag_name} not valid for XTF::Search::Element::Clause. Must be one of: #{VALID_TAG_NAMES.join(', ')}" unless VALID_TAG_NAMES.include?(tag_name)

  klass = eval("XTF::Search::Element::#{tag_name.to_s.camelize}") # scope the name to avoid conflicts, especially with Range
  klass.new(params)
end

Instance Method Details

#to_xmlObject



54
55
56
# File 'lib/xtf/search/element/clause.rb', line 54

def to_xml
  to_xml_node.to_s
end

#to_xml_nodeObject

TODO add section_type



48
49
50
51
52
53
# File 'lib/xtf/search/element/clause.rb', line 48

def to_xml_node
  xml = XTF::XML::Element.new self.tag_name.camelize(:lower)
  self.attributes.each_pair { |key, value| xml.attributes[key.to_s.camelize(:lower)] = value if value}
  self.content.each {|node| xml.add_element(node.to_xml_node)}
  xml
end