Module: Hackmac::Plist
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.
Instance Method Summary collapse
-
#as_hash(*a) ⇒ Hash
Returns a duplicate of the internal plist hash.
-
#each {|key, value| ... } ⇒ Hash
The each method iterates over the parsed plist data.
-
#method_missing(name, *a) ⇒ Object
The method_missing method provides dynamic access to plist data by handling attribute reads and writes through method calls.
-
#plist(*cmd) ⇒ void
Parses XML output from a command into a plist hash.
-
#to_json(*a) ⇒ String
The to_json method converts the parsed plist data to a JSON string.
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.
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.
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.
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.
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.
68 69 70 |
# File 'lib/hackmac/plist.rb', line 68 def to_json(*a) as_hash.to_json(*a) end |