Class: VER::Plist::XML

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/plist.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ XML

Returns a new instance of XML.



32
33
34
35
36
37
# File 'lib/ver/plist.rb', line 32

def initialize(xml)
  @doc = Nokogiri(xml)
  @plist = {}
  @parsed = nil
  @exceptions = []
end

Instance Method Details

#handle_array(array) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ver/plist.rb', line 56

def handle_array(array)
  out = []

  array.children.each do |child|
    child_name = child.name
    next if child_name == 'text'

    out.push(
      case child_name
      when 'key'
        raise 'No key allows in array'
      when 'array'
        handle_array(child)
      when 'dict'
        handle_dict(child)
      when 'false'
        false
      when 'integer', 'real'
        child.inner_text.to_i
      when 'string'
        child.inner_text
      when 'true'
        true
      else
        raise "unhandled %p: %p" % [child_name, child]
      end
    )
  end

  out
end

#handle_dict(dict) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ver/plist.rb', line 88

def handle_dict(dict)
  out = {}
  key = nil

  dict.children.each do |child|
    child_name = child.name

    case child_name
    when 'key'
      case key = child.inner_text
      when /^\d+$/
        key = key.to_i
      else
        key = key.to_sym
      end
    when 'text'
      # ignore
    else
      raise 'No key given' unless key

      out[key] =
        case child_name
        when 'array'
          handle_array(child)
        when 'dict'
          handle_dict(child)
        when 'integer', 'real'
          child.inner_text.to_i
        when 'true'
          true
        when 'false'
          false
        when 'string'
          value = child.inner_text

          case key
          when :begin, :match, :foldingStartMarker, :foldingStopMarker
            sanitize_regexp(value)
          else
            value
          end
        else
          raise "unhandled %p: %p" % [child_name, child]
        end

      key = nil
    end
  end

  out
end

#parseObject



39
40
41
# File 'lib/ver/plist.rb', line 39

def parse
  @parsed || parse!
end

#parse!Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ver/plist.rb', line 43

def parse!
  dict = @doc.at('/plist/dict')

  if dict
    @parsed = handle_dict(dict)
  else
    raise ArgumentError, 'This is no XML plist'
  end

  fail "#{@exceptions.size} errors encountered" if @exceptions.any?
  @parsed
end

#to_hashObject



148
149
150
# File 'lib/ver/plist.rb', line 148

def to_hash
  parse
end

#to_jsonObject



144
145
146
# File 'lib/ver/plist.rb', line 144

def to_json
  parse.to_json
end

#to_yamlObject



140
141
142
# File 'lib/ver/plist.rb', line 140

def to_yaml
  parse.to_yaml
end