Class: WeakXml::Fragment
- Inherits:
-
Object
- Object
- WeakXml::Fragment
- Defined in:
- lib/weak_xml/fragment.rb
Constant Summary collapse
- ATTRIBUTES_REGEXP =
Compiling regular expressions is expensive, specially if one uses variable parts. So, in order to achive the best performance, these should be compiled upfront without any “runtime dependencies”.
Regexp.compile(/\A<\w+\s+([^>]+)/m)
- CONTENT_REGEXP =
Regexp.compile(/.*?>(.*?)<[^>]+>\Z/m)
Instance Method Summary collapse
- #attr(key) ⇒ Object
- #content ⇒ Object
-
#initialize(tag, xml) ⇒ Fragment
constructor
A new instance of Fragment.
Constructor Details
#initialize(tag, xml) ⇒ Fragment
Returns a new instance of Fragment.
10 11 12 13 |
# File 'lib/weak_xml/fragment.rb', line 10 def initialize(tag, xml) @tag = tag @xml = xml end |
Instance Method Details
#attr(key) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/weak_xml/fragment.rb', line 15 def attr(key) if match_data = @xml.match(ATTRIBUTES_REGEXP) if value_match_data = match_data.captures.first.match(/#{key}="(.+?)"/) value_match_data.captures.first end end end |
#content ⇒ Object
23 24 25 26 27 |
# File 'lib/weak_xml/fragment.rb', line 23 def content if match_data = @xml.match(CONTENT_REGEXP) match_data.captures.first.tap(&:strip!) end end |