Module: Hackmac::Plist

Included in:
DiskInfo, Disks, IOReg, Kext
Defined in:
lib/hackmac/plist.rb

Overview

A module that provides methods for parsing and interacting with Property List (plist) data

The Plist module offers functionality to parse XML-formatted plist data from shell commands and provides convenient access to the resulting hash structure. It includes methods for executing commands, parsing their XML output, and accessing plist values through dynamic method calls.

Examples:

class MyClass
  include Hackmac::Plist
end

obj = MyClass.new
obj.plist('diskutil', 'info', '-plist', '/dev/disk0')
# Access parsed data through method_missing or as_hash

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *a) ⇒ Object

The method_missing method provides dynamic access to plist data by handling attribute reads and writes through method calls

This method intercepts undefined method calls on objects that include the Plist module, allowing convenient access to plist values using method-style syntax. When a method name ends with an equals sign, it sets the corresponding plist key to the provided value. Otherwise, if the method name matches an existing plist key, it returns the value.

Parameters:

  • name (Symbol)

    the method name being called

  • a (Array)

    the arguments passed to the method

Returns:

  • (Object)

    the value of the plist key when reading, or nil when setting



85
86
87
88
89
90
91
92
# File 'lib/hackmac/plist.rb', line 85

def method_missing(name, *a)
  n = name.to_s
  if n =~ /(.+)=\z/
    @plist[$1] = a.first
  elsif @plist.key?(n)
    @plist[n]
  end
end

Instance Method Details

#as_hash(*a) ⇒ Hash

Returns a duplicate of the internal plist hash

This method provides access to the parsed plist data by returning a shallow copy of the internal @plist instance variable. This allows external code to read the plist contents without directly modifying the original data structure.

Returns:

  • (Hash)

    a duplicate of the plist hash containing the parsed XML data



42
43
44
# File 'lib/hackmac/plist.rb', line 42

def as_hash(*a)
  @plist.dup
end

#each {|key, value| ... } ⇒ Hash

The each method iterates over the parsed plist data

This method provides an iterator interface for the plist hash by delegating to the as_hash method’s each implementation. It allows callers to enumerate over the key-value pairs in the parsed plist structure.

Yields:

  • (key, value)

    yields each key-value pair from the plist

Returns:

  • (Hash)

    returns the hash representation of the plist.



55
56
57
# File 'lib/hackmac/plist.rb', line 55

def each(&block)
  as_hash.each(&block)
end

#plist(*cmd) ⇒ void

This method returns an undefined value.

Parses XML output from a command into a plist hash

This method executes a shell command and parses its XML output into a Ruby hash using the Plist library. The resulting hash is stored in the @plist instance variable for later access through other methods.

Parameters:

  • cmd (Array<String>)

    command and arguments to execute



31
32
33
# File 'lib/hackmac/plist.rb', line 31

def plist(*cmd)
  @plist = ::Plist.parse_xml(`#{Shellwords.join(cmd)}`)
end

#to_json(*a) ⇒ String

The to_json method converts the parsed plist data to a JSON string

This method takes the internal plist hash and serializes it into a JSON format using the standard to_json method from the Hash class. It provides a convenient way to output the plist data in JSON representation.

Parameters:

  • a (Array)

    additional arguments to pass to the underlying to_json method

Returns:

  • (String)

    a JSON string representation of the plist data



68
69
70
# File 'lib/hackmac/plist.rb', line 68

def to_json(*a)
  as_hash.to_json(*a)
end