summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Reiter <me@reitermark.us>2017-05-06 20:57:08 +0200
committerMarkus Reiter <me@reitermark.us>2017-05-06 21:45:24 +0200
commit09248f031df32eec34ae4fcf46abff0154cc850f (patch)
treeeed84f373b0e2f72ab4a1ef41d9c2f53e42f50d8
parent2004f36a7f1628afef5b8aa53c9b27ce3310548b (diff)
downloadplist-09248f031df32eec34ae4fcf46abff0154cc850f.tar.gz
Refactor method code style.
-rw-r--r--README.rdoc2
-rw-r--r--lib/plist/generator.rb12
-rwxr-xr-xlib/plist/parser.rb31
-rw-r--r--test/test_data_elements.rb20
-rw-r--r--test/test_generator.rb2
-rwxr-xr-xtest/test_parser.rb46
6 files changed, 57 insertions, 56 deletions
diff --git a/README.rdoc b/README.rdoc
index 1fe9a3f..0df9d97 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -9,7 +9,7 @@ Plist is a library to manipulate Property List files, also known as plists. It
=== Parsing
- result = Plist::parse_xml('path/to/example.plist')
+ result = Plist.parse_xml('path/to/example.plist')
result.class
=> Hash
diff --git a/lib/plist/generator.rb b/lib/plist/generator.rb
index 84bef3a..5c6aefa 100644
--- a/lib/plist/generator.rb
+++ b/lib/plist/generator.rb
@@ -80,7 +80,7 @@ module Plist::Emit
element.keys.sort_by{|k| k.to_s }.each do |k|
v = element[k]
- inner_tags << tag('key', CGI::escapeHTML(k.to_s))
+ inner_tags << tag('key', CGI.escapeHTML(k.to_s))
inner_tags << plist_node(v)
end
@@ -95,7 +95,7 @@ module Plist::Emit
when Date # also catches DateTime
output << tag('date', element.strftime('%Y-%m-%dT%H:%M:%SZ'))
when String, Symbol, Integer, Float
- output << tag(element_type(element), CGI::escapeHTML(element.to_s))
+ output << tag(element_type(element), CGI.escapeHTML(element.to_s))
when IO, StringIO
element.rewind
contents = element.read
@@ -104,13 +104,13 @@ module Plist::Emit
# I used #encode64 instead of #b64encode (which allows a length arg)
# because b64encode is b0rked and ignores the length arg.
data = "\n"
- Base64::encode64(contents).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
+ Base64.encode64(contents).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
output << tag('data', data)
else
- output << comment( 'The <data> element below contains a Ruby object which has been serialized with Marshal.dump.' )
+ output << comment('The <data> element below contains a Ruby object which has been serialized with Marshal.dump.')
data = "\n"
- Base64::encode64(Marshal.dump(element)).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
- output << tag('data', data )
+ Base64.encode64(Marshal.dump(element)).gsub(/\s+/, '').scan(/.{1,68}/o) { data << $& << "\n" }
+ output << tag('data', data)
end
end
diff --git a/lib/plist/parser.rb b/lib/plist/parser.rb
index 396447e..705d823 100755
--- a/lib/plist/parser.rb
+++ b/lib/plist/parser.rb
@@ -11,18 +11,18 @@
# === Load a plist file
# This is the main point of the library:
#
-# r = Plist::parse_xml( filename_or_xml )
+# r = Plist.parse_xml(filename_or_xml)
module Plist
# Note that I don't use these two elements much:
#
# + Date elements are returned as DateTime objects.
# + Data elements are implemented as Tempfiles
#
-# Plist::parse_xml will blow up if it encounters a Date element.
+# Plist.parse_xml will blow up if it encounters a Date element.
# If you encounter such an error, or if you have a Date element which
# can't be parsed into a Time object, please send your plist file to
# plist@hexane.org so that I can implement the proper support.
- def Plist::parse_xml( filename_or_xml )
+ def self.parse_xml(filename_or_xml)
listener = Listener.new
#parser = REXML::Parsers::StreamParser.new(File.new(filename), listener)
parser = StreamParser.new(filename_or_xml, listener)
@@ -41,10 +41,10 @@ module Plist
end
def tag_start(name, attributes)
- @open.push PTag::mappings[name].new
+ @open.push PTag.mappings[name].new
end
- def text( contents )
+ def text(contents)
@open.last.text = contents if @open.last
end
@@ -59,11 +59,11 @@ module Plist
end
class StreamParser
- def initialize( plist_data_or_file, listener )
+ def initialize(plist_data_or_file, listener)
if plist_data_or_file.respond_to? :read
@xml = plist_data_or_file.read
elsif File.exist? plist_data_or_file
- @xml = File.read( plist_data_or_file )
+ @xml = File.read(plist_data_or_file)
else
@xml = plist_data_or_file
end
@@ -78,13 +78,13 @@ module Plist
COMMENT_END = /.*?-->/m
def parse
- plist_tags = PTag::mappings.keys.join('|')
+ plist_tags = PTag.mappings.keys.join('|')
start_tag = /<(#{plist_tags})([^>]*)>/i
end_tag = /<\/(#{plist_tags})[^>]*>/i
require 'strscan'
- @scanner = StringScanner.new( @xml )
+ @scanner = StringScanner.new(@xml)
until @scanner.eos?
if @scanner.scan(COMMENT_START)
@scanner.scan(COMMENT_END)
@@ -130,14 +130,15 @@ module Plist
end
class PTag
- @@mappings = { }
- def PTag::mappings
+ @@mappings = {}
+
+ def self.mappings
@@mappings
end
- def PTag::inherited( sub_class )
+ def self.inherited(sub_class)
key = sub_class.to_s.downcase
- key.gsub!(/^plist::/, '' )
+ key.gsub!(/^plist::/, '')
key.gsub!(/^p/, '') unless key == "plist"
@@mappings[key] = sub_class
@@ -179,13 +180,13 @@ module Plist
class PKey < PTag
def to_ruby
- CGI::unescapeHTML(text || '')
+ CGI.unescapeHTML(text || '')
end
end
class PString < PTag
def to_ruby
- CGI::unescapeHTML(text || '')
+ CGI.unescapeHTML(text || '')
end
end
diff --git a/test/test_data_elements.rb b/test/test_data_elements.rb
index 6d061e8..09af0da 100644
--- a/test/test_data_elements.rb
+++ b/test/test_data_elements.rb
@@ -13,7 +13,7 @@ end
class TestDataElements < Test::Unit::TestCase
def setup
- @result = Plist.parse_xml( 'test/assets/test_data_elements.plist' )
+ @result = Plist.parse_xml('test/assets/test_data_elements.plist')
end
def test_data_object_header
@@ -24,10 +24,10 @@ BAhvOhZNYXJzaGFsYWJsZU9iamVjdAY6CUBmb28iHnRoaXMgb2JqZWN0IHdhcyBtYXJz
aGFsZWQ=
</data>
END
- expected_elements = expected.chomp.split( "\n" )
+ expected_elements = expected.chomp.split("\n")
- actual = Plist::Emit.dump( Object.new, false )
- actual_elements = actual.chomp.split( "\n" )
+ actual = Plist::Emit.dump(Object.new, false)
+ actual_elements = actual.chomp.split("\n")
# check for header
assert_equal expected_elements.shift, actual_elements.shift
@@ -39,7 +39,7 @@ END
def test_marshal_round_trip
expected = MarshalableObject.new('this object was marshaled')
- actual = Plist.parse_xml( Plist::Emit.dump(expected, false) )
+ actual = Plist.parse_xml(Plist::Emit.dump(expected, false))
assert_kind_of expected.class, actual
assert_equal expected.foo, actual.foo
@@ -99,23 +99,23 @@ END
# supplied the test data, and provided the parsing code.
def test_data
# test reading plist <data> elements
- data = Plist::parse_xml("test/assets/example_data.plist");
- assert_equal( File.open("test/assets/example_data.jpg"){|f| f.read }, data['image'].read )
+ data = Plist.parse_xml("test/assets/example_data.plist");
+ assert_equal(File.open("test/assets/example_data.jpg"){|f| f.read }, data['image'].read)
# test writing data elements
expected = File.read("test/assets/example_data.plist")
result = data.to_plist
#File.open('result.plist', 'w') {|f|f.write(result)} # debug
- assert_equal( expected, result )
+ assert_equal(expected, result)
# Test changing the <data> object in the plist to a StringIO and writing.
# This appears extraneous given that plist currently returns a StringIO,
# so the above writing test also flexes StringIO#to_plist_node.
# However, the interface promise is to return an IO, not a particular class.
# plist used to return Tempfiles, which was changed solely for performance reasons.
- data['image'] = StringIO.new( File.read("test/assets/example_data.jpg"))
+ data['image'] = StringIO.new(File.read("test/assets/example_data.jpg"))
- assert_equal(expected, data.to_plist )
+ assert_equal(expected, data.to_plist)
end
diff --git a/test/test_generator.rb b/test/test_generator.rb
index 241ff4f..e03c49d 100644
--- a/test/test_generator.rb
+++ b/test/test_generator.rb
@@ -9,7 +9,7 @@ class SerializableObject
end
def to_plist_node
- return "<string>#{CGI::escapeHTML @foo}</string>"
+ return "<string>#{CGI.escapeHTML(@foo)}</string>"
end
end
diff --git a/test/test_parser.rb b/test/test_parser.rb
index 551135e..0e17765 100755
--- a/test/test_parser.rb
+++ b/test/test_parser.rb
@@ -3,10 +3,10 @@ require 'plist'
class TestParser < Test::Unit::TestCase
def test_Plist_parse_xml
- result = Plist::parse_xml("test/assets/AlbumData.xml")
+ result = Plist.parse_xml("test/assets/AlbumData.xml")
# dict
- assert_kind_of( Hash, result )
+ assert_kind_of(Hash, result)
expected = [
"List of Albums",
@@ -18,65 +18,65 @@ class TestParser < Test::Unit::TestCase
"List of Rolls",
"Application Version"
]
- assert_equal( expected.sort, result.keys.sort )
+ assert_equal(expected.sort, result.keys.sort)
# array
- assert_kind_of( Array, result["List of Rolls"] )
- assert_equal( [ {"PhotoCount"=>1,
+ assert_kind_of(Array, result["List of Rolls"])
+ assert_equal([ {"PhotoCount"=>1,
"KeyList"=>["7"],
"Parent"=>999000,
"Album Type"=>"Regular",
"AlbumName"=>"Roll 1",
"AlbumId"=>6}],
- result["List of Rolls"] )
+ result["List of Rolls"])
# string
- assert_kind_of( String, result["Application Version"] )
- assert_equal( "5.0.4 (263)", result["Application Version"] )
+ assert_kind_of(String, result["Application Version"])
+ assert_equal("5.0.4 (263)", result["Application Version"])
# integer
- assert_kind_of( Integer, result["Major Version"] )
- assert_equal( 2, result["Major Version"] )
+ assert_kind_of(Integer, result["Major Version"])
+ assert_equal(2, result["Major Version"])
# true
- assert_kind_of( TrueClass, result["List of Albums"][0]["Master"] )
- assert( result["List of Albums"][0]["Master"] )
+ assert_kind_of(TrueClass, result["List of Albums"][0]["Master"])
+ assert(result["List of Albums"][0]["Master"])
# false
- assert_kind_of( FalseClass, result["List of Albums"][1]["SlideShowUseTitles"] )
- assert( ! result["List of Albums"][1]["SlideShowUseTitles"] )
+ assert_kind_of(FalseClass, result["List of Albums"][1]["SlideShowUseTitles"])
+ assert(!result["List of Albums"][1]["SlideShowUseTitles"])
end
# uncomment this test to work on speed optimization
#def test_load_something_big
- # plist = Plist::parse_xml( "~/Pictures/iPhoto Library/AlbumData.xml" )
+ # plist = Plist.parse_xml("~/Pictures/iPhoto Library/AlbumData.xml")
#end
# date fields are credited to
def test_date_fields
- result = Plist::parse_xml("test/assets/Cookies.plist")
- assert_kind_of( DateTime, result.first['Expires'] )
- assert_equal DateTime.parse( "2007-10-25T12:36:35Z" ), result.first['Expires']
+ result = Plist.parse_xml("test/assets/Cookies.plist")
+ assert_kind_of(DateTime, result.first['Expires'])
+ assert_equal DateTime.parse("2007-10-25T12:36:35Z"), result.first['Expires']
end
# bug fix for empty <key>
# reported by Matthias Peick <matthias@peick.de>
# reported and fixed by Frederik Seiffert <ego@frederikseiffert.de>
def test_empty_dict_key
- data = Plist::parse_xml("test/assets/test_empty_key.plist");
+ data = Plist.parse_xml("test/assets/test_empty_key.plist");
assert_equal("2", data['key']['subkey'])
end
# bug fix for decoding entities
# reported by Matthias Peick <matthias@peick.de>
def test_decode_entities
- data = Plist::parse_xml('<string>Fish &amp; Chips</string>')
+ data = Plist.parse_xml('<string>Fish &amp; Chips</string>')
assert_equal('Fish & Chips', data)
end
def test_comment_handling_and_empty_plist
assert_nothing_raised do
- assert_nil( Plist::parse_xml( File.read('test/assets/commented.plist') ) )
+ assert_nil(Plist.parse_xml(File.read('test/assets/commented.plist')))
end
end
@@ -84,7 +84,7 @@ class TestParser < Test::Unit::TestCase
require 'stringio'
str = StringIO.new
- data = Plist::parse_xml(str)
+ data = Plist.parse_xml(str)
assert_nil data
end
@@ -97,7 +97,7 @@ class TestParser < Test::Unit::TestCase
xml.force_encoding("ASCII-8BIT")
assert_nothing_raised do
- data = Plist::parse_xml(xml)
+ data = Plist.parse_xml(xml)
assert_equal("\u0099", data["non-ascii-but-utf8-character"])
end
end