Class: NokogiriPList::Parser
- Inherits:
-
Object
- Object
- NokogiriPList::Parser
- Defined in:
- lib/nokogiri-plist/parser.rb
Class Method Summary collapse
- .next_valid_sibling(node) ⇒ Object
- .parse(xml, options = {}) ⇒ Object
- .parse_array(node) ⇒ Object
- .parse_dict(node) ⇒ Object
- .parse_value_node(value_node) ⇒ Object
- .valid_node?(node) ⇒ Boolean
- .valid_type?(type) ⇒ Boolean
Class Method Details
.next_valid_sibling(node) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/nokogiri-plist/parser.rb', line 51 def next_valid_sibling(node) until node.nil? or valid_type? node.name node = node.next_sibling end node end |
.parse(xml, options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/nokogiri-plist/parser.rb', line 7 def parse(xml, ={}) @converters = { 'integer' => Proc.new { |node| node.content.to_i }, 'real' => Proc.new { |node| node.content.to_f }, 'string' => Proc.new { |node| node.content.to_s }, 'date' => Proc.new { |node| DateTime.parse(node.content) }, 'true' => Proc.new { |node| true }, 'false' => Proc.new { |node| false }, 'dict' => Proc.new { |node| parse_dict node }, 'array' => Proc.new { |node| parse_array node }, 'data' => Proc.new { |node| node.content.to_s } }.merge([:converters] || {}) @dict_class = [:dict_class] || Hash plist = xml.root plist = plist.children.first if plist.name == "plist" parse_value_node next_valid_sibling plist end |
.parse_array(node) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/nokogiri-plist/parser.rb', line 44 def parse_array(node) node.children.inject([]) do |return_value, node| return_value << parse_value_node(node) if valid_node? node return_value end end |
.parse_dict(node) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/nokogiri-plist/parser.rb', line 37 def parse_dict(node) node.xpath('./key').inject(@dict_class.new) do |return_value, key_node| return_value[key_node.content] = parse_value_node(next_valid_sibling key_node) return_value end end |
.parse_value_node(value_node) ⇒ Object
25 26 27 |
# File 'lib/nokogiri-plist/parser.rb', line 25 def parse_value_node(value_node) @converters[value_node.name].call(value_node) end |
.valid_node?(node) ⇒ Boolean
33 34 35 |
# File 'lib/nokogiri-plist/parser.rb', line 33 def valid_node?(node) valid_type?(node.name) or node.name == "key" end |
.valid_type?(type) ⇒ Boolean
29 30 31 |
# File 'lib/nokogiri-plist/parser.rb', line 29 def valid_type?(type) not @converters[type].nil? end |