Class: HMachine::POSH::DefinitionList

Inherits:
Base
  • Object
show all
Defined in:
lib/hmachine/posh/definition_list.rb

Direct Known Subclasses

Microformat::XMDP

Constant Summary

Constants included from HMachine

HMachine::PRODID, VERSION

Instance Attribute Summary

Attributes inherited from Base

#node, #parent, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], [], add_property, #each_pair, #empty?, extract, get_properties, has_many, has_many!, has_one, has_one!, #has_property?, inherited, #initialize, #inspect, many, name, one, parent, properties, property_of?, remove_nested, search, selector, #to_html, #to_s, validate

Methods included from HMachine

#extract, #extract_from, find, #find_in, #found_in?, get, get_document, get_url, map, normalize, #parse, #parse_first, #search, #valid?, #validate

Constructor Details

This class inherits a constructor from HMachine::POSH::Base

Class Method Details

.build_dictionary(dl) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hmachine/posh/definition_list.rb', line 9

def self.build_dictionary(dl)
  dictionary = {}
  terms = dl.children.select {|dt| dt.elem? && dt.node_name.eql?('dt') }
  terms.each do |term|
    definition = term.next_element if term.next_element.node_name.eql?('dd')
    nested_dls = definition.children.select {|dd| dd.elem? && dd.node_name.eql?('dl') } 
    if nested_dls.empty?
      value = definition.content.strip
    elsif nested_dls.length.eql?(1)
      value = build_dictionary(nested_dls.first)
    else
      # if there are multiple nested <dl> elements, merge them together
      value = {}
      nested_dls.each do |dict| 
        value.merge! build_dictionary(dict) 
      end
    end
    dictionary[term.content.strip.intern] = value
  end
  dictionary
end

Instance Method Details

#propertiesObject



31
32
33
# File 'lib/hmachine/posh/definition_list.rb', line 31

def properties
  to_h
end

#to_hObject



35
36
37
# File 'lib/hmachine/posh/definition_list.rb', line 35

def to_h
  @to_h ||= self.class.build_dictionary(node)
end