summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Redpath <luke@lukeredpath.co.uk>2010-02-08 12:17:57 +0000
committerBen Bleything <ben@bleything.net>2010-02-16 16:06:15 -0800
commitf605c967868ceec3c66da74d8a20b21fc61a75df (patch)
treef4943864221c423cb6591bcfb387e3c806153a0b
parentf51aa926cf381d9c1dde3c81c00ac00b7eb5c1ee (diff)
downloadplist-integration.tar.gz
Added a handy convenience method for directly modifying an existing plist.integration
-rw-r--r--lib/plist/parser.rb9
-rw-r--r--test/test_parser.rb19
2 files changed, 28 insertions, 0 deletions
diff --git a/lib/plist/parser.rb b/lib/plist/parser.rb
index 6daa5bf..63f9c53 100644
--- a/lib/plist/parser.rb
+++ b/lib/plist/parser.rb
@@ -29,6 +29,15 @@ module Plist
parser.parse
listener.result
end
+
+ # Parses and yields the plist as a hash, and
+ # commits changes back to the original file (destructive)
+ def self.modify(path, &block)
+ if plist = Plist::parse_xml(path)
+ yield plist if block_given?
+ Plist::Emit.save_plist(plist, path)
+ end
+ end
class Listener
#include REXML::StreamListener
diff --git a/test/test_parser.rb b/test/test_parser.rb
index 6116750..4b2742d 100644
--- a/test/test_parser.rb
+++ b/test/test_parser.rb
@@ -91,7 +91,26 @@ class TestParser < Test::Unit::TestCase
assert_nil data
end
+end
+class TestDirectModification < Test::Unit::TestCase
+
+ def setup
+ test_file = File.expand_path("assets/AlbumData.xml", File.dirname(__FILE__))
+ FileUtils.cp(test_file, 'modificationtest.plist')
+ end
+
+ def teardown
+ FileUtils.rm('modificationtest.plist')
+ end
+
+ def test_plist_modification_with_block
+ Plist.modify('modificationtest.plist') do |plist|
+ plist['Application Version'] = '10.1.2'
+ end
+ assert_equal '10.1.2', Plist.parse_xml('modificationtest.plist')['Application Version']
+ end
+
end
__END__