Class: XTF::Search::Element::Term

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

Overview

Models a single XTF Term. However, if the term is surrounded by double-quotes, then a Phrase will be emitted for to_xml_node().

Constant Summary

Constants inherited from Base

Base::BASE_ATTRIBUTE_KEYS

Instance Attribute Summary collapse

Attributes inherited from Base

#tag_name

Instance Method Summary collapse

Methods inherited from Base

attribute_keys, #attributes

Constructor Details

#initialize(*args) ⇒ Term

new accepts an optional first argument for value as well as an optional first or second argument Hash of the +Term+'s attributes. value may be passed as the first argument or in attributes Hash with key :value. section_type may be passed in the attributes Hash with key :section_type.



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

def initialize(*args)
  @tag_name = "term"
  @value = args.shift if args[0].kind_of?(String)
  params = args[0] || {}
  @value = params.delete(:value) unless @value
  @section_type = params.delete(:section_type)
  @parse_phrase = params.key?(:parse_phrase) ? params.delete(:parse_phrase) : false
  super
  @value.strip! unless @value.nil?
end

Instance Attribute Details

#parse_phrase ⇒ Object

Should a phrase be parsed? Defaults to true



9
10
11
# File 'lib/xtf/search/element/term.rb', line 9

def parse_phrase
  @parse_phrase
end

#section_type ⇒ Object

Returns the value of attribute section_type.



6
7
8
# File 'lib/xtf/search/element/term.rb', line 6

def section_type
  @section_type
end

#value ⇒ Object

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#to_xml ⇒ Object



48
49
50
# File 'lib/xtf/search/element/term.rb', line 48

def to_xml
  to_xml_node.to_s
end

#to_xml_node ⇒ Object

For convenience, if the Term's value matches /[-\s\/.,;:]+/, it will be parsed as a Phrase. Double quotes on either end will be removed.

"this phrase" woud yield:

thisphrase


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/xtf/search/element/term.rb', line 36

def to_xml_node
  if self.parse_phrase && self.value =~ XTF::Search::Constants.phrase_delimiters
    phrase = XTF::Search::Element::Phrase.new(self.attributes)
    phrase.phrase = self.value
    phrase.to_xml_node
  else
    xml = XTF::XML::Element.new(self.tag_name)
    self.attributes.each_pair { |key, value| xml.attributes[key.to_s.camelize(:lower)] = value if value}
    xml.text = self.value
    xml
  end
end