Class: TinyXPathHelper
- Inherits:
-
Object
- Object
- TinyXPathHelper
- Defined in:
- lib/tiny_xpath_helper.rb
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Class Method Summary collapse
- .classes_that_are_xmlish ⇒ Object
- .default_options ⇒ Object
- .find_xpath_from(element, path, options = {}, &blk) ⇒ Object
- .io_stream_classes ⇒ Object
- .xml_node_for_xmlish(xml, options = {}) ⇒ Object
- .xml_node_to_text(node) ⇒ Object
Instance Method Summary collapse
- #all(xpath_expr, options = {}, &blk) ⇒ Object (also: #[])
- #default_options ⇒ Object
- #find_xpath(xpath_expr, options = {}, &blk) ⇒ Object
- #first(xpath_expr, options = {}, &blk) ⇒ Object (also: #at)
-
#initialize(xml, options = {}) ⇒ TinyXPathHelper
constructor
A new instance of TinyXPathHelper.
- #with_options(*options) ⇒ Object
Constructor Details
#initialize(xml, options = {}) ⇒ TinyXPathHelper
Returns a new instance of TinyXPathHelper.
6 7 8 9 10 |
# File 'lib/tiny_xpath_helper.rb', line 6 def initialize(xml, = {}) @xml = xml @node = self.class.xml_node_for_xmlish(xml, ) @options = .freeze end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
4 5 6 |
# File 'lib/tiny_xpath_helper.rb', line 4 def node @node end |
Class Method Details
.classes_that_are_xmlish ⇒ Object
47 48 49 |
# File 'lib/tiny_xpath_helper.rb', line 47 def self.classes_that_are_xmlish io_stream_classes + [ String, REXML::Document ] end |
.default_options ⇒ Object
80 81 82 |
# File 'lib/tiny_xpath_helper.rb', line 80 def self. {:format => nil, :auto_text => true, :find => :first}.freeze end |
.find_xpath_from(element, path, options = {}, &blk) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/tiny_xpath_helper.rb', line 84 def self.find_xpath_from(element, path, = {}, &blk) if [:with_indifferent_access] path = path.to_s end = self..dup.update() format = [:format] auto_text = [:auto_text] count = [:find] if format == :array count = :all format = nil elsif format == :text auto_text = true format = nil elsif format == :xml or format == :rexml auto_text = false format = nil elsif format == :xpath_helper or format == :tiny_xpath_helper auto_text = false format = self.method(:new) end filter1 = auto_text ? self.method(:xml_node_to_text) : nil filter2 = format if count == :all elements = REXML::XPath.match(element, path) elsif count == :first elements = [ REXML::XPath.first(element, path) ].compact # Haskell hacker wishing for the Maybe monad else raise "I don't know how to find #{count.inspect}" end elements = elements.map(&filter1).map(&filter2) if count == :all r = elements elsif count == :first r = elements.first end if(blk and elements.length > 0) return blk.call( r ) end return r end |
.io_stream_classes ⇒ Object
43 44 45 |
# File 'lib/tiny_xpath_helper.rb', line 43 def self.io_stream_classes [ IOStream ] rescue [ IO, StringIO ] # thoughtbot-paperclip fixes the ducktype mess in StringIO end |
.xml_node_for_xmlish(xml, options = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tiny_xpath_helper.rb', line 51 def self.xml_node_for_xmlish( xml, = {} ) if io_stream_classes.any?{|k| xml.is_a?(k) } xml = xml.read end if xml.is_a?(String) xml = REXML::Document.new(xml) end if xml.is_a?(REXML::Document) if [:allow_empty_document] and ! xml.root xml = xml else xml = xml.root end end if not xml.is_a?(REXML::Element) raise TypeError.new("Expected REXML::Element, got #{xml.class}") end return xml end |
.xml_node_to_text(node) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/tiny_xpath_helper.rb', line 71 def self.xml_node_to_text(node) if(node.respond_to? :text) # XML::Elements don't to_s in the way we want val = node.text else val = node.to_s end end |
Instance Method Details
#all(xpath_expr, options = {}, &blk) ⇒ Object Also known as: []
38 39 40 |
# File 'lib/tiny_xpath_helper.rb', line 38 def all(xpath_expr, = {}, &blk) self.find_xpath( xpath_expr, (, :find => :all), &blk) end |
#default_options ⇒ Object
12 13 14 |
# File 'lib/tiny_xpath_helper.rb', line 12 def self.class..dup.update(@options) end |
#find_xpath(xpath_expr, options = {}, &blk) ⇒ Object
29 30 31 |
# File 'lib/tiny_xpath_helper.rb', line 29 def find_xpath(xpath_expr, = {}, &blk) self.class.find_xpath_from( node, xpath_expr, (), &blk) end |
#first(xpath_expr, options = {}, &blk) ⇒ Object Also known as: at
33 34 35 |
# File 'lib/tiny_xpath_helper.rb', line 33 def first(xpath_expr, = {}, &blk) self.find_xpath( xpath_expr, (, :find => :first), &blk) end |
#with_options(*options) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/tiny_xpath_helper.rb', line 16 def (*) r = .dup .reverse.each do |option| if not option.is_a?( Hash ) option = {:format => option} end r.update(option) end return r end |