Skip to content

Commit

Permalink
Added a handy convenience method for directly modifying an existing p…
Browse files Browse the repository at this point in the history
…list.
  • Loading branch information
lukeredpath committed Feb 8, 2010
1 parent 5b3e98e commit 5aa9e1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/plist/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ def Plist::parse_xml( filename_or_xml )
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
Expand Down
20 changes: 19 additions & 1 deletion test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
##############################################################

require 'test/unit'

require 'plist'

class TestParser < Test::Unit::TestCase
Expand Down Expand Up @@ -94,7 +93,26 @@ def test_filename_or_xml_is_stringio

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__

0 comments on commit 5aa9e1c

Please sign in to comment.