diff options
-rw-r--r-- | lib/plist/parser.rb | 9 | ||||
-rw-r--r-- | test/test_parser.rb | 19 |
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__ |