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.
-
#exist? ⇒ Object?
The exist? method checks whether plist data has been loaded.
-
#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) ⇒ Object
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.
94 95 96 97 98 99 100 101 |
# File 'lib/hackmac/plist.rb', line 94 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.
51 52 53 |
# File 'lib/hackmac/plist.rb', line 51 def as_hash(*a) @plist.dup.to_h 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.
64 65 66 |
# File 'lib/hackmac/plist.rb', line 64 def each(&block) as_hash.each(&block) end |
#exist? ⇒ Object?
The exist? method checks whether plist data has been loaded
This method returns a truthy value if plist data has been successfully parsed and stored in the instance variable, or nil if no plist data is available.
40 41 42 |
# File 'lib/hackmac/plist.rb', line 40 def exist? @plist end |
#plist(*cmd) ⇒ Object
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.
29 30 31 |
# File 'lib/hackmac/plist.rb', line 29 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.
77 78 79 |
# File 'lib/hackmac/plist.rb', line 77 def to_json(*a) as_hash.to_json(*a) end |