Class: XTF::Search::Element::Facet

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

Constant Summary

Constants inherited from Base

Base::BASE_ATTRIBUTE_KEYS

Instance Attribute Summary

Attributes inherited from Base

#tag_name

Instance Method Summary collapse

Methods inherited from Base

attribute_keys, #attributes

Constructor Details

#initialize(*args) ⇒ Facet

can take a String or Symbol as first argument for required attribute #field

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/xtf/search/element/facet.rb', line 5

def initialize(*args)
  @tag_name = "facet"
  @field = args.shift.to_s if args[0].is_a?(String) or args[0].is_a?(Symbol)
  params = args[0] || {}
  raise ArgumentError, "supply field as first argument or as attribute of Hash, but not both!" if @field && params.key?(:field)

  @field = params.delete(:field) unless @field
  raise ArgumentError, "field is required." unless @field

  super
end

Instance Method Details

#to_xmlObject



22
23
24
# File 'lib/xtf/search/element/facet.rb', line 22

def to_xml
  to_xml_node.to_s
end

#to_xml_nodeObject



17
18
19
20
21
# File 'lib/xtf/search/element/facet.rb', line 17

def to_xml_node
  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
end