Class: XTF::Search::Element::Clause
- Defined in:
- lib/xtf/search/element/clause.rb
Constant Summary collapse
- VALID_TAG_NAMES =
%w{phrase exact and or or_near orNear not near range}
Constants inherited from Base
Instance Attribute Summary collapse
-
#content ⇒ Object
an Array that contains any number of clauses and/or terms.
-
#section_type ⇒ Object
available on all elements except <not> and <facet>.
-
#term ⇒ Object
convenience to create a
Termfrom aStringand insert it intocontent.
Attributes inherited from Base
Class Method Summary collapse
-
.create(*args) ⇒ Object
This is a factory method for creating subclasses directly from
Clause.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Clause
constructor
A new instance of Clause.
- #to_xml ⇒ Object
-
#to_xml_node ⇒ Object
TODO add section_type.
Methods inherited from Base
Constructor Details
#initialize(*args) ⇒ Clause
Returns a new instance of Clause.
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
#content ⇒ Object
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_type ⇒ Object
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 |
#term ⇒ Object
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’
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_xml ⇒ Object
54 55 56 |
# File 'lib/xtf/search/element/clause.rb', line 54 def to_xml to_xml_node.to_s end |
#to_xml_node ⇒ Object
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 |