Class: Plutil

Inherits:
Object
  • Object
show all
Defined in:
lib/tm_bundle/plutil.rb

Defined Under Namespace

Modules: JSON

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Plutil

Returns a new instance of Plutil.



74
75
76
77
78
79
80
# 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

Class Method Details

.call(*args, &block) ⇒ Object Also known as: plutil

Also aliased as ‘Plutil.plutil` Usage:

Plutil.(:remove, "keypath", file: plist, &:read)
Plutil.(:extract, "keypath", :json, file: plist, &:read)


30
31
32
# File 'lib/tm_bundle/plutil.rb', line 30

def self.call(*args, &block)
  new(*args).execute(&block)
end

.convert(path, to: :json, &block) ⇒ Object

Usage:

Plutil.convert plist, to: 'json' do |converted_io|
  JSON.load(converted_io)
end


52
53
54
55
# 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) ⇒ Object



70
71
72
# File 'lib/tm_bundle/plutil.rb', line 70

def self.dump_json(*args)
  Plutil::JSON.dump(*args)
end

.load_json(*args) ⇒ Object

Shorthand to ‘Plutil::JSON.load(plist)`



66
67
68
# File 'lib/tm_bundle/plutil.rb', line 66

def self.load_json(*args)
  Plutil::JSON.load(*args)
end

.replace(path, keypath, data, as: :xml, &block) ⇒ Object

Usage:

Plutil.replace(plist, 'name', data, as: 'xml', &:read)


61
62
63
# 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

Instance Method Details

#execute(&block) ⇒ Object



82
83
84
85
86
87
# 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_argsObject



90
# File 'lib/tm_bundle/plutil.rb', line 90

def input_args; @in && ['--', normalize_io_arg(@in)]; end

#output_argsObject



89
# File 'lib/tm_bundle/plutil.rb', line 89

def output_args; @out && ['-o', normalize_io_arg(@out)]; end

#stdin?Boolean

Returns:

  • (Boolean)


91
# File 'lib/tm_bundle/plutil.rb', line 91

def stdin?; @in.to_s == 'stdin'; end