nokogiri-plist 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -34,15 +34,23 @@ There is already a plist gem (http://github.com/bleything/plist) that has simila
34
34
 
35
35
  > require 'nokogiri-plist'
36
36
 
37
+ Parse the plist XML
38
+
37
39
  > plist = Nokogiri::PList(open('test.plist'))
38
40
  => { "Beers" => ["Black Butte", "Steel Reserve", "Bass Pale Ale"], "Beers Drank" => 4123, "Beer Drinker" => "Casey"}
39
41
 
42
+ Read a dict property thats value is an array
43
+
40
44
  > plist["Beers"]
41
45
  => ["Black Butte", "Steel Reserve", "Bass Pale Ale"]
42
46
 
47
+ Read another value
48
+
43
49
  > plist["Beer Drinker"]
44
50
  => "Casey"
45
51
 
52
+ Turn it into a string containing XML
53
+
46
54
  > puts plist.to_plist_xml
47
55
  <dict>
48
56
  <key>Beers</key>
@@ -55,9 +63,13 @@ There is already a plist gem (http://github.com/bleything/plist) that has simila
55
63
  <key>Beer Drinker</key><string>Casey</string>
56
64
  </dict>
57
65
 
66
+ Convert a string into a plist XML string
67
+
58
68
  > "beer".to_plist_xml
59
69
  => "<string>beer</string>"
60
70
 
71
+ Convert an array to a plist XML string
72
+
61
73
  > puts (1..3).to_a.to_plist_xml
62
74
  <array>
63
75
  <integer>1</integer>
@@ -65,16 +77,15 @@ There is already a plist gem (http://github.com/bleything/plist) that has simila
65
77
  <integer>3</integer>
66
78
  </array>
67
79
 
80
+
68
81
  == REQUIREMENTS:
69
82
 
70
- * ruby 1.9 (I'll eventually make sure it works in 1.8, but I'm using 1.9 at the moment)
83
+ * Ruby (tested with 1.9.1 and 1.8.7)
71
84
  * nokogiri
72
85
  * shoulda and mocha (only if you want to run the tests)
73
86
 
74
87
 
75
88
  == INSTALL:
76
89
 
77
- I haven't published this gem yet, but once I write tests and more documentation the following will work:
78
-
79
- * gem install nokogiri-plist
90
+ * [sudo] gem install nokogiri-plist
80
91
 
@@ -9,6 +9,7 @@ module Nokogiri
9
9
  end
10
10
 
11
11
  def self.to_s(value, current_indent = 0)
12
+ # Todo: make these procs and allow custom procs (for data for example)
12
13
  case value
13
14
  when Array
14
15
  tag("array", nil, current_indent) do
@@ -4,53 +4,59 @@ module Nokogiri
4
4
 
5
5
  class Parser
6
6
 
7
- def parse(xml)
8
- @converters = {
9
- 'integer' => Proc.new { |node| node.content.to_i },
10
- 'real' => Proc.new { |node| node.content.to_f },
11
- 'string' => Proc.new { |node| node.content.to_s },
12
- 'date' => Proc.new { |node| DateTime.parse(node.content) },
13
- 'true' => Proc.new { |node| true },
14
- 'false' => Proc.new { |node| false },
15
- 'dict' => Proc.new { |node| parse_dict node },
16
- 'array' => Proc.new { |node| parse_array node },
17
- 'data' => Proc.new { |node| node.content }
18
- }
19
- plist = xml.xpath('/plist').first
20
- parse_value_node next_valid_sibling(plist.children.first)
21
- end
22
-
23
- def parse_value_node(value_node)
24
- @converters[value_node.name].call(value_node)
25
- end
26
-
27
- def valid_type?(type)
28
- not @converters[type].nil?
29
- end
30
-
31
- def valid_node?(node)
32
- valid_type?(node.name) or node.name == "key"
33
- end
34
-
35
- def parse_dict(node)
36
- node.xpath('./key').inject({}) do |return_value, key_node|
37
- return_value[key_node.content] = parse_value_node(next_valid_sibling key_node)
38
- return_value
7
+ class << self
8
+
9
+ def parse(xml, options={})
10
+ @converters = {
11
+ 'integer' => Proc.new { |node| node.content.to_i },
12
+ 'real' => Proc.new { |node| node.content.to_f },
13
+ 'string' => Proc.new { |node| node.content.to_s },
14
+ 'date' => Proc.new { |node| DateTime.parse(node.content) },
15
+ 'true' => Proc.new { |node| true },
16
+ 'false' => Proc.new { |node| false },
17
+ 'dict' => Proc.new { |node| parse_dict node },
18
+ 'array' => Proc.new { |node| parse_array node },
19
+ 'data' => Proc.new { |node| node.content.to_s }
20
+ }.merge(options[:converters] || {})
21
+ @dict_class = options[:dict_class] || Hash
22
+ plist = xml.root
23
+ plist = plist.children.first if plist.name == "plist"
24
+ parse_value_node next_valid_sibling plist
39
25
  end
40
- end
41
-
42
- def parse_array(node)
43
- node.children.inject([]) do |return_value, node|
44
- return_value << parse_value_node(node) if valid_node? node
45
- return_value
26
+
27
+ def parse_value_node(value_node)
28
+ @converters[value_node.name].call(value_node)
46
29
  end
47
- end
48
-
49
- def next_valid_sibling(node)
50
- until node.nil? or valid_type? node.name
51
- node = node.next_sibling
30
+
31
+ def valid_type?(type)
32
+ not @converters[type].nil?
33
+ end
34
+
35
+ def valid_node?(node)
36
+ valid_type?(node.name) or node.name == "key"
37
+ end
38
+
39
+ def parse_dict(node)
40
+ node.xpath('./key').inject(@dict_class.new) do |return_value, key_node|
41
+ return_value[key_node.content] = parse_value_node(next_valid_sibling key_node)
42
+ return_value
43
+ end
44
+ end
45
+
46
+ def parse_array(node)
47
+ node.children.inject([]) do |return_value, node|
48
+ return_value << parse_value_node(node) if valid_node? node
49
+ return_value
50
+ end
51
+ end
52
+
53
+ def next_valid_sibling(node)
54
+ until node.nil? or valid_type? node.name
55
+ node = node.next_sibling
56
+ end
57
+ node
52
58
  end
53
- node
59
+
54
60
  end
55
61
 
56
62
  end
@@ -0,0 +1,66 @@
1
+ module Nokogiri
2
+
3
+ module PList
4
+
5
+ class Parser
6
+
7
+ class << self
8
+
9
+ def parse(xml, options={})
10
+ @converters = {
11
+ 'integer' => Proc.new { |node| node.content.to_i },
12
+ 'real' => Proc.new { |node| node.content.to_f },
13
+ 'string' => Proc.new { |node| node.content.to_s },
14
+ 'date' => Proc.new { |node| DateTime.parse(node.content) },
15
+ 'true' => Proc.new { |node| true },
16
+ 'false' => Proc.new { |node| false },
17
+ 'dict' => Proc.new { |node| parse_dict node },
18
+ 'array' => Proc.new { |node| parse_array node },
19
+ 'data' => Proc.new { |node| node.content.to_s }
20
+ }.merge(options[:converters] || {})
21
+ @dict_class = options[:dict_class] || Hash
22
+ plist = xml.root
23
+ plist = plist.children.first if plist.name == "plist"
24
+ parse_value_node next_valid_sibling plist
25
+ end
26
+
27
+ def parse_value_node(value_node)
28
+ @converters[value_node.name].call(value_node)
29
+ end
30
+
31
+ def valid_type?(type)
32
+ not @converters[type].nil?
33
+ end
34
+
35
+ def valid_node?(node)
36
+ valid_type?(node.name) or node.name == "key"
37
+ end
38
+
39
+ def parse_dict(node)
40
+ node.xpath('./key').inject(@dict_class.new) do |return_value, key_node|
41
+ return_value[key_node.content] = parse_value_node(next_valid_sibling key_node)
42
+ return_value
43
+ end
44
+ end
45
+
46
+ def parse_array(node)
47
+ node.children.inject([]) do |return_value, node|
48
+ return_value << parse_value_node(node) if valid_node? node
49
+ return_value
50
+ end
51
+ end
52
+
53
+ def next_valid_sibling(node)
54
+ until node.nil? or valid_type? node.name
55
+ node = node.next_sibling
56
+ end
57
+ node
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -3,7 +3,7 @@ module Nokogiri
3
3
  class << self
4
4
 
5
5
  def PList(xml)
6
- Nokogiri::PList::Parser.new.parse(Nokogiri::XML(xml))
6
+ Nokogiri::PList::Parser.parse(Nokogiri::XML(xml))
7
7
  end
8
8
 
9
9
  end
@@ -0,0 +1,11 @@
1
+ module Nokogiri
2
+
3
+ class << self
4
+
5
+ def PList(xml)
6
+ Nokogiri::PList::Parser.parsea(Nokogiri::XML(xml))
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -5,7 +5,7 @@ module Nokogiri
5
5
  class Node
6
6
 
7
7
  def to_plist
8
- Nokogiri::PList::Parser.new.parse(self)
8
+ Nokogiri::PList::Parser.parse(self)
9
9
  end
10
10
 
11
11
  end
@@ -0,0 +1,15 @@
1
+ module Nokogiri
2
+
3
+ module XML
4
+
5
+ class Node
6
+
7
+ def to_plist
8
+ Nokogiri::PList::Parser.new.parse(self)
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -1,5 +1,9 @@
1
-
2
- require 'nokogiri'
1
+ begin
2
+ require 'nokogiri'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ retry
6
+ end
3
7
  require 'date'
4
8
 
5
9
  # TODO: Do this a not so stupid way
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri-plist
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 2
7
+ - 3
9
8
  - 0
10
- version: 0.2.0
9
+ version: 0.3.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Casey Howard
@@ -15,23 +14,10 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-05-27 00:00:00 -07:00
17
+ date: 2010-05-30 00:00:00 -07:00
19
18
  default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- name: rspec
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
33
- type: :development
34
- version_requirements: *id001
19
+ dependencies: []
20
+
35
21
  description: Allows Nokogiri objects to be converted to PLists
36
22
  email:
37
23
  - caseyhoward@caseyhoward.com
@@ -45,7 +31,10 @@ files:
45
31
  - lib/nokogiri-plist.rb
46
32
  - lib/string.rb
47
33
  - lib/nokogiri/plist/parser.rb
34
+ - lib/nokogiri/plist/parser.rb~
48
35
  - lib/nokogiri/plist/generator.rb
36
+ - lib/nokogiri/plist.rb~
37
+ - lib/nokogiri/xml/node.rb~
49
38
  - lib/nokogiri/xml/node.rb
50
39
  - lib/nokogiri/plist.rb
51
40
  - LICENSE
@@ -60,20 +49,16 @@ rdoc_options: []
60
49
  require_paths:
61
50
  - lib
62
51
  required_ruby_version: !ruby/object:Gem::Requirement
63
- none: false
64
52
  requirements:
65
53
  - - ">="
66
54
  - !ruby/object:Gem::Version
67
- hash: 3
68
55
  segments:
69
56
  - 0
70
57
  version: "0"
71
58
  required_rubygems_version: !ruby/object:Gem::Requirement
72
- none: false
73
59
  requirements:
74
60
  - - ">="
75
61
  - !ruby/object:Gem::Version
76
- hash: 23
77
62
  segments:
78
63
  - 1
79
64
  - 3
@@ -81,8 +66,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
66
  version: 1.3.6
82
67
  requirements: []
83
68
 
84
- rubyforge_project: nokogiri-plist
85
- rubygems_version: 1.3.7
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.6
86
71
  signing_key:
87
72
  specification_version: 3
88
73
  summary: PList parsing capabilities built using Nokogiri