Class: WeakXml

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = {})
  @options = 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, options = {})
  if matched_string = xml[regex_factory(tag, options)]
    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, options = {})
  xml.scan(regex_factory(tag, options)).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, options = {})
  options[:multiline] = options[:multiline].nil? ? true : !!options[:multiline]

  regexp_base =
  if tag.start_with?("<") && tag.end_with?(">")
    "#{tag}.*?<\/#{tag[1..-2]}>"
  else
    "<#{tag}[>\s].*?<\/#{tag}>"
  end

  Regexp.new(regexp_base, (options[: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, options = nil)
  self.class.find(tag, @xml, (options || @options))
end

#find_all(tag, options = nil) ⇒ Object



40
41
42
# File 'lib/weak_xml.rb', line 40

def find_all(tag, options = nil)
  self.class.find_all(tag, @xml, (options || @options))
end