Class: WeakXml
- Inherits:
-
Object
- Object
- WeakXml
- Defined in:
- lib/weak_xml.rb,
lib/weak_xml/version.rb
Defined Under Namespace
Classes: Fragment
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .find(tag, xml, options = {}) ⇒ Object
- .find_all(tag, xml, options = {}) ⇒ Object
- .regex_factory(tag, options = {}) ⇒ Object
Instance Method Summary collapse
- #find(tag, options = nil) ⇒ Object
- #find_all(tag, options = nil) ⇒ Object
-
#initialize(xml, options = {}) ⇒ WeakXml
constructor
A new instance of WeakXml.
Constructor Details
#initialize(xml, options = {}) ⇒ WeakXml
Returns a new instance of WeakXml.
31 32 33 34 |
# File 'lib/weak_xml.rb', line 31 def initialize(xml, = {}) @options = @xml = xml end |
Class Method Details
.find(tag, xml, options = {}) ⇒ Object
6 7 8 9 10 |
# File 'lib/weak_xml.rb', line 6 def self.find(tag, xml, = {}) if matched_string = xml[regex_factory(tag, )] Fragment.new(tag, matched_string) end end |
.find_all(tag, xml, options = {}) ⇒ Object
12 13 14 15 16 |
# File 'lib/weak_xml.rb', line 12 def self.find_all(tag, xml, = {}) xml.scan(regex_factory(tag, )).map! do |match| Fragment.new(tag, match) end end |
.regex_factory(tag, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/weak_xml.rb', line 18 def self.regex_factory(tag, = {}) [:multiline] = [:multiline].nil? ? true : !![:multiline] regexp_base = if tag.start_with?("<") && tag.end_with?(">") "#{tag}.*?<\/#{tag[1..-2]}>" else "<#{tag}[>\s].*?<\/#{tag}>" end Regexp.new(regexp_base, ([:multiline] ? Regexp::MULTILINE : 0)) end |
Instance Method Details
#find(tag, options = nil) ⇒ Object
36 37 38 |
# File 'lib/weak_xml.rb', line 36 def find(tag, = nil) self.class.find(tag, @xml, ( || @options)) end |
#find_all(tag, options = nil) ⇒ Object
40 41 42 |
# File 'lib/weak_xml.rb', line 40 def find_all(tag, = nil) self.class.find_all(tag, @xml, ( || @options)) end |