class Plutil
Public Class Methods
call(*args, &block)
click to toggle source
Also aliased as `Plutil.plutil` Usage:
Plutil.(:remove, "keypath", file: plist, &:read) Plutil.(:extract, "keypath", :json, file: plist, &:read)
# File lib/tm_bundle/plutil.rb, line 30 def self.call(*args, &block) new(*args).execute(&block) end
Also aliased as: plutil
convert(path, to: :json, &block)
click to toggle source
Usage:
Plutil.convert plist, to: 'json' do |converted_io| JSON.load(converted_io) end
# File lib/tm_bundle/plutil.rb, line 52 def self.convert(path, to: :json, &block) to = "xml1" if to.to_s == 'xml' plutil(:convert, to, out: :stdin, file: path.to_s, &block) end
dump_json(*args)
click to toggle source
# File lib/tm_bundle/plutil.rb, line 70 def self.dump_json(*args) Plutil::JSON.dump(*args) end
load_json(*args)
click to toggle source
Shorthand to `Plutil::JSON.load(plist)`
# File lib/tm_bundle/plutil.rb, line 66 def self.load_json(*args) Plutil::JSON.load(*args) end
new(*args)
click to toggle source
# File lib/tm_bundle/plutil.rb, line 74 def initialize(*args) options = args.extract_options! @command, *@args = *args @in, @out, @mode = *options.values_at(:in, :out, :mode) @in ||= options[:file] @mode ||= auto_mode end
replace(path, keypath, data, as: :xml, &block)
click to toggle source
Usage:
Plutil.replace(plist, 'name', data, as: 'xml', &:read)
# File lib/tm_bundle/plutil.rb, line 61 def self.replace(path, keypath, data, as: :xml, &block) plutil(:replace, keypath, "-#{as}", data, file: path.to_s, &block) end
Public Instance Methods
execute(&block)
click to toggle source
# File lib/tm_bundle/plutil.rb, line 82 def execute(&block) io = IO.popen(cmd, @mode) block.call(io) ensure io.close if io && !io.closed? end
input_args()
click to toggle source
# File lib/tm_bundle/plutil.rb, line 90 def input_args; @in && ['--', normalize_io_arg(@in)]; end
output_args()
click to toggle source
# File lib/tm_bundle/plutil.rb, line 89 def output_args; @out && ['-o', normalize_io_arg(@out)]; end
stdin?()
click to toggle source
# File lib/tm_bundle/plutil.rb, line 91 def stdin?; @in.to_s == 'stdin'; end
Private Instance Methods
auto_mode()
click to toggle source
# File lib/tm_bundle/plutil.rb, line 105 def auto_mode case @command when :convert then stdin? ? 'w+' : 'r' when :replace then 'w+' else 'r' end end
cmd()
click to toggle source
# File lib/tm_bundle/plutil.rb, line 94 def cmd ['plutil', "-#@command"] + Array(@args.map(&:to_s)) + Array(output_args) + Array(input_args) end
normalize_io_arg(io)
click to toggle source
# File lib/tm_bundle/plutil.rb, line 98 def normalize_io_arg(io) case io.to_s when 'stdin', 'stdout' then '-' else io end end