plist4r 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
1
 
2
+ # These methods should all be nemespaced to ::Plist4r
3
+ # we don't want to upset anyone else's code
2
4
 
3
5
  class Object
4
6
  def method_name
@@ -1,88 +1,92 @@
1
1
 
2
2
  require 'plist4r/mixin/ordered_hash'
3
+ require 'plist4r/mixin/ruby_stdlib'
4
+ require 'plist4r/plist_cache'
3
5
  require 'plist4r/plist_type'
6
+ Dir.glob(File.dirname(__FILE__) + "/plist_type/**/*.rb").each {|t| require File.expand_path t}
4
7
  require 'plist4r/backend'
5
8
 
6
-
7
- class Plist4r::Plist
8
- PlistOptionsHash = %w[from_string filename path file_format plist_type unsupported_keys]
9
- FileFormats = %w[binary xml next_step]
9
+ module Plist4r
10
+ class Plist
11
+ PlistOptionsHash = %w[from_string filename path file_format plist_type unsupported_keys backends]
12
+ FileFormats = %w[binary xml next_step]
10
13
 
11
- def initialize *args, &blk
12
- @hash = ::ActiveSupport::OrderedHash.new
13
- @plist_cache = PlistCache.new self
14
- @plist_type = plist_type PlistType::Plist
15
- @unsupported_keys = Config[:unsupported_keys]
16
- @from_string = nil
17
- @filename = nil
18
- @file_format = nil
19
- @path = Config[:default_path]
14
+ def initialize *args, &blk
15
+ @hash = ::ActiveSupport::OrderedHash.new
16
+ # @plist_type = plist_type PlistType::Plist
17
+ @unsupported_keys = Config[:unsupported_keys]
18
+ @backends = Config[:backends]
19
+ @from_string = nil
20
+ @filename = nil
21
+ @file_format = nil
22
+ @path = Config[:default_path]
20
23
 
21
- case args.first
22
- when Hash
23
- parse_opts args.first
24
+ case args.first
25
+ when Hash
26
+ parse_opts args.first
24
27
 
25
- when String, Symbol
26
- @filename = args.first.to_s
27
- when nil
28
- else
29
- raise "Unrecognized first argument: #{args.first.inspect}"
28
+ when String, Symbol
29
+ @filename = args.first.to_s
30
+ when nil
31
+ else
32
+ raise "Unrecognized first argument: #{args.first.inspect}"
33
+ end
34
+
35
+ @plist_cache = PlistCache.new self
30
36
  end
31
- end
32
37
 
33
- def from_string string=nil
34
- case string
35
- when String
36
- plist_format = ::Plist4r.string_detect_format string
37
- if plist_format
38
- eval "@plist_cache.from_#{format}, string"
38
+ def from_string string=nil
39
+ case string
40
+ when String
41
+ plist_format = ::Plist4r.string_detect_format string
42
+ if plist_format
43
+ eval "@plist_cache.from_#{format}, string"
44
+ else
45
+ raise "Unknown plist format for string: #{string}"
46
+ end
47
+ @from_string = string
48
+ when nil
49
+ @from_string
39
50
  else
40
- raise "Unknown plist format for string: #{string}"
51
+ raise "Please specify a string of plist data"
41
52
  end
42
- @from_string = string
43
- when nil
44
- @from_string
45
- else
46
- raise "Please specify a string of plist data"
47
53
  end
48
- end
49
54
 
50
- def filename filename=nil
51
- case filename
52
- when String
53
- @filename = filename
54
- when nil
55
- @filename
56
- else
57
- raise "Please specify a filename"
55
+ def filename filename=nil
56
+ case filename
57
+ when String
58
+ @filename = filename
59
+ when nil
60
+ @filename
61
+ else
62
+ raise "Please specify a filename"
63
+ end
58
64
  end
59
- end
60
65
 
61
- def path path=nil
62
- case path
63
- when String
64
- @path = path
65
- when nil
66
- @path
67
- else
68
- raise "Please specify a directory"
66
+ def path path=nil
67
+ case path
68
+ when String
69
+ @path = path
70
+ when nil
71
+ @path
72
+ else
73
+ raise "Please specify a directory"
74
+ end
69
75
  end
70
- end
71
76
 
72
- def filename_path filename_path=nil
73
- case path
74
- when String
75
- @filename = File.basename filename_path
76
- @path = File.dirname filename_path
77
- when nil
78
- File.expand_path @filename, @path
79
- else
80
- raise "Please specify directory + filename"
77
+ def filename_path filename_path=nil
78
+ case path
79
+ when String
80
+ @filename = File.basename filename_path
81
+ @path = File.dirname filename_path
82
+ when nil
83
+ File.expand_path @filename, @path
84
+ else
85
+ raise "Please specify directory + filename"
86
+ end
81
87
  end
82
- end
83
88
 
84
- def file_format file_format=nil
85
- begin
89
+ def file_format file_format=nil
86
90
  case file_format
87
91
  when Symbol, String
88
92
  if FileFormats.include? file_format.to_s.snake_case
@@ -95,111 +99,114 @@ class Plist4r::Plist
95
99
  else
96
100
  raise "Please specify a valid plist file format, #{FileFormats.inspect}"
97
101
  end
98
- rescue
99
- raise "Please specify a valid plist file format, #{FileFormats.inspect}"
100
102
  end
101
- end
102
103
 
103
- def plist_type plist_type=nil
104
- begin
105
- case plist_type
106
- when Class
107
- @plist_type = PlistType::Plist.new :hash => @hash
108
- when Symbol, String
109
- eval "pt_klass = PlistType::#{plist_type.to_s.camelcase}"
110
- @plist_type = pt_klass.new :hash => @hash
104
+ def plist_type plist_type=nil
105
+ begin
106
+ case plist_type
107
+ when Class
108
+ @plist_type = PlistType::Plist.new :hash => @hash
109
+ when Symbol, String
110
+ eval "pt_klass = PlistType::#{plist_type.to_s.camelcase}"
111
+ @plist_type = pt_klass.new :hash => @hash
112
+ when nil
113
+ @plist_type
114
+ else
115
+ raise "Please specify a valid plist class name, eg ::Plist4r::PlistType::ClassName, \"class_name\" or :class_name"
116
+ end
117
+ rescue
118
+ raise "Please specify a valid plist class name, eg ::Plist4r::PlistType::ClassName, \"class_name\" or :class_name"
119
+ end
120
+ end
121
+
122
+ def unsupported_keys bool
123
+ case bool
124
+ when true,false
125
+ @unsupported_keys = bool
111
126
  when nil
112
- @plist_type
127
+ @unsupported_keys
113
128
  else
114
- raise "Please specify a valid plist class name, eg ::Plist4r::PlistType::ClassName, \"class_name\" or :class_name"
129
+ raise "Please specify true or false to enable / disable this option"
115
130
  end
116
- rescue
117
- raise "Please specify a valid plist class name, eg ::Plist4r::PlistType::ClassName, \"class_name\" or :class_name"
118
131
  end
119
- end
120
132
 
121
- def unsupported_keys bool
122
- case bool
123
- when true,false
124
- @unsupported_keys = bool
125
- when nil
126
- @unsupported_keys
127
- else
128
- raise "Please specify true or false to enable / disable this option"
133
+ def backends backends=nil
134
+ case backends
135
+ when Array
136
+ @backends = backends
137
+ when nil
138
+ @backends
139
+ else
140
+ raise "Please specify an array of valid Plist4r Backends"
141
+ end
129
142
  end
130
- end
131
143
 
132
- def parse_opts opts
133
- PlistOptionsHash.each do |opt|
134
- eval "@#{opt} = #{opts[opt.to_sym]}" if opts[opt.to_sym]
144
+ def parse_opts opts
145
+ PlistOptionsHash.each do |opt|
146
+ eval "@#{opt} = #{opts[opt.to_sym]}" if opts[opt.to_sym]
147
+ end
135
148
  end
136
- end
137
149
 
138
- def open filename=nil
139
- @filename = filename if filename
140
- raise "No filename specified" unless @filename
141
- @hash = @plist_cache.open
142
- end
150
+ def open filename=nil
151
+ @filename = filename if filename
152
+ raise "No filename specified" unless @filename
153
+ # @hash = @plist_cache.open
154
+ @plist_cache.open
155
+ end
143
156
 
144
- def << *args, &blk
145
- edit *args, &blk
146
- end
157
+ def << *args, &blk
158
+ edit *args, &blk
159
+ end
147
160
 
148
- def edit *args, &blk
149
- instance_eval &blk
150
- end
161
+ def edit *args, &blk
162
+ instance_eval &blk
163
+ end
151
164
 
152
- def method_missing method_sym, *args, &blk
153
- @plist_type.send method_sym, *args, &blk
154
- end
165
+ def method_missing method_sym, *args, &blk
166
+ @plist_type.send method_sym, *args, &blk
167
+ end
155
168
 
156
- def import_hash hash=nil
157
- case path
158
- when ::ActiveSupport::OrderedHash
159
- @hash = hash
160
- when nil
161
- @hash = ::ActiveSupport::OrderedHash.new
162
- else
163
- raise "Please use ::ActiveSupport::OrderedHash.new for your hashes"
169
+ def import_hash hash=nil
170
+ case hash
171
+ when ::ActiveSupport::OrderedHash
172
+ @hash = hash
173
+ when nil
174
+ @hash = ::ActiveSupport::OrderedHash.new
175
+ else
176
+ raise "Please use ::ActiveSupport::OrderedHash.new for your hashes"
177
+ end
164
178
  end
165
- end
166
179
 
167
- def to_hash
168
- @hash
169
- end
180
+ def to_hash
181
+ @hash
182
+ end
170
183
 
171
- def to_xml
172
- @plist_cache.to_xml
173
- end
184
+ def to_xml
185
+ @plist_cache.to_xml
186
+ end
174
187
 
175
- def to_binary
176
- @plist_cache.to_binary
177
- end
188
+ def to_binary
189
+ @plist_cache.to_binary
190
+ end
178
191
 
179
- def to_next_step
180
- @plist_cache.to_next_step
181
- end
192
+ def to_next_step
193
+ @plist_cache.to_next_step
194
+ end
182
195
 
183
- def save
184
- raise "No filename specified" unless @filename
185
- @plist_cache.save
186
- end
196
+ def save
197
+ raise "No filename specified" unless @filename
198
+ @plist_cache.save
199
+ end
187
200
 
188
- def save_as filename
189
- @filename = filename
190
- save
201
+ def save_as filename
202
+ @filename = filename
203
+ save
204
+ end
191
205
  end
192
206
  end
193
207
 
194
-
195
-
196
- # plutil -convert xml1 @filename
197
- # plutil -convert binary1 @filename
198
-
199
-
200
208
  module Plist4r
201
- class Plist
202
- include ::Plist4r::Popen4
209
+ class OldPlist
203
210
 
204
211
  def initialize path_prefix, plist_str, &blk
205
212
  plist_str << ".plist" unless plist_str =~ /\.plist$/
@@ -1,66 +1,70 @@
1
1
 
2
2
  require 'plist4r/backend'
3
-
4
- class Plist4r::PlistCache
5
- def initialize plist, *args, &blk
6
- @plist = plist
7
- @backend = Backend.new plist, *args, &blk
8
- end
3
+ module Plist4r
4
+ class PlistCache
5
+ def initialize plist, *args, &blk
6
+ @plist = plist
7
+ @backend = Backend.new plist, *args, &blk
8
+ end
9
9
 
10
- def checksum
11
- @plist.to_hash.hash
12
- end
10
+ def checksum
11
+ @plist.to_hash.hash
12
+ end
13
13
 
14
- def last_checksum
15
- @checksum
16
- end
14
+ def last_checksum
15
+ @checksum
16
+ end
17
17
 
18
- def update_checksum
19
- @checksum = @plist.to_hash.hash
20
- end
18
+ def update_checksum
19
+ @checksum = @plist.to_hash.hash
20
+ end
21
21
 
22
- def needs_update
23
- checksum != last_checksum
24
- end
22
+ def needs_update
23
+ checksum != last_checksum
24
+ end
25
25
 
26
- def to_xml
27
- if needs_update
28
- update_checksum
29
- @xml = @backend.call :to_xml
30
- else
31
- @xml
26
+ def to_xml
27
+ puts "in to_xml"
28
+ # if needs_update
29
+ puts "needs update"
30
+ update_checksum
31
+ @xml = @backend.call :to_xml
32
+ # else
33
+ # @xml
34
+ # end
32
35
  end
33
- end
34
36
 
35
- def to_binary
36
- if needs_update
37
- update_checksum
38
- @binary = @backend.call :to_binary
39
- else
40
- @binary
37
+ def to_binary
38
+ if needs_update
39
+ update_checksum
40
+ @binary = @backend.call :to_binary
41
+ else
42
+ @binary
43
+ end
41
44
  end
42
- end
43
45
 
44
- def to_next_step
45
- if needs_update
46
- update_checksum
47
- @next_step = @backend.call :to_next_step
48
- else
49
- @next_step
46
+ def to_next_step
47
+ if needs_update
48
+ update_checksum
49
+ @next_step = @backend.call :to_next_step
50
+ else
51
+ @next_step
52
+ end
50
53
  end
51
- end
52
-
53
- def open
54
- @backend.call :load
55
- update_checksum
56
- end
57
54
 
58
- def save
59
- if needs_update
55
+ def open
56
+ @backend.call :open
60
57
  update_checksum
61
- @backend.call :save
62
- else
63
- true
58
+ @plist
59
+ end
60
+
61
+ def save
62
+ if needs_update
63
+ update_checksum
64
+ @backend.call :save
65
+ else
66
+ true
67
+ end
64
68
  end
65
69
  end
66
- end
70
+ end