Class: Babushka::XMLString

Inherits:
String show all
Defined in:
lib/babushka/xml_string.rb

Instance Method Summary collapse

Methods inherited from String

#/, #blank?, #colourise, #end_with, #ends_with?, #start_with, #starts_with?, #to_utf8, #to_version, #with, #xml_val_for

Methods included from Fancypath::Helpers

#to_expanded_fancypath, #to_fancypath, #to_tilde_expanded_path

Instance Method Details

#val_for(key) ⇒ Object

This extracts values from XML, like that found in .plist files. For example,

%Q{
  <key>SVNPath</key>
  <string>/trunk/src</string>
  <key>SVNRevision</key>
  <string>37609</string>
}.val_for('SVNRevision') #=> "37609"

It doesn't work for arrays, and probably doesn't work for boolean values. Patches welcome :)


14
15
16
17
18
19
20
# File 'lib/babushka/xml_string.rb', line 14

def val_for key
  split(/<\/[^>]+>\n\s*<key>/m).select {|i|
    i[/#{Regexp.escape(key)}<\/key>/]
  }.map {|i|
    i.scan(/<[^\/>]+>(.*)$/m)
  }.flatten.first
end