Class: NokogiriNotepad::NodeFinder
- Inherits:
-
Object
- Object
- NokogiriNotepad::NodeFinder
- Defined in:
- lib/nokogiri-notepad/node_finder.rb
Instance Method Summary collapse
- #find_grouped_nodes_at(path, code = nil, tag_name = nil) ⇒ Object
- #find_node_at(path, code = nil, tag_name = nil) ⇒ Object
-
#initialize(doc) ⇒ NodeFinder
constructor
A new instance of NodeFinder.
Constructor Details
#initialize(doc) ⇒ NodeFinder
Returns a new instance of NodeFinder.
3 4 5 |
# File 'lib/nokogiri-notepad/node_finder.rb', line 3 def initialize(doc) @doc = doc end |
Instance Method Details
#find_grouped_nodes_at(path, code = nil, tag_name = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/nokogiri-notepad/node_finder.rb', line 29 def find_grouped_nodes_at(path, code=nil, tag_name=nil) begin nodes = @doc.remove_namespaces!.xpath(render(path)) rescue NoMethodError return nil end return nil if nodes.empty? if !(code || tag_name) node = nodes.first || nil else matching_nodes = nodes.select do |node| return nil if node.nil? node.children.to_s.try(:strip) == code end return nil if matching_nodes.first.nil? return nil if matching_nodes.first.parent.at(".//#{tag_name}").nil? nodes = matching_nodes.map do |node| node.parent.at(".//#{tag_name}") end end nodes end |
#find_node_at(path, code = nil, tag_name = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/nokogiri-notepad/node_finder.rb', line 7 def find_node_at(path, code=nil, tag_name=nil) begin nodes = @doc.remove_namespaces!.xpath(render(path)) rescue NoMethodError return nil end if !(code || tag_name) node = nodes.first || nil else matching_nodes = nodes.select do |node| return nil if node.nil? node.children.to_s.try(:strip) == code end return nil if matching_nodes.first.nil? return nil if matching_nodes.first.parent.at(".//#{tag_name}").nil? node = matching_nodes.first.parent.at(".//#{tag_name}") end node end |