Plist Key Names

To read from or write to a plist key, we must specify the key name, plus an optional value. There are several ways we can specify key names in Plist4r. The precise method syntax you choose will depend mostly upon convenience. However there are certain limitations to method names in Ruby in regards to uppercase / lowercase. And plist key names are usually capitalized (such as “WatchPaths”, and “CFBundleSignature”). To get around this we “CamelCase” and “snake_case” the key accessor methods, by default. Dont forget that many Info plist keys start with 2 capital letters. So “CFBundleSignature” should be written with an extra underscore between the “c” and the “f“

CF/NSKeyNames

  info_plist.<< do
    c_f_bundle_signature "com.mydomain.myapp"
  end
  info_plist.to_hash
  # => { "CFBundleSignature" => "com.mydomain.myapp" }

Corrected Key Names

Sometimes, the associated PlistType will come with a “tweaked” method name, is the true keyname cannot be expressed in terms of snake_case. For example

  launchd_plist.<< do
    inetd_compatibility :wait => true
  end
  launchd_plist.to_hash
  # => { "inetdCompatability" => { "Wait" => true } }
  # (the CamelCased key name would otherwise start with a capital letter, "InetdCompatability")

CamelCasing of Key Names

For an unknown key name which isnt specifically supported, these are all passed through method_missing, and automatically CamelCased. This is the default behaviour. Therefore

  plist.<< do
    some_arbitrary_key ["some","arbitrary","data"]
  end
  plist.to_hash
  # => { "SomeArbitraryKey" => ["some", "arbitrary", "data"] }

store(), [] and []=

Sometimes a plist key name will be in lowercase, and we cannot use method_missing(). To store or read the plist key we call the regular Hash accessor methods with the literal key name. See Plist4r::Plist#store, Plist4r::Plist#[], and Plist4r::Plist#[]=.

  # writing with plist.store("key",value)
  plist.<< do
    store "keyname" "value"
  end

  # reading and writing with plist["key"], and plist["key"]="value"
  plist["keyname"] = "value"
  plist["keyname"] # => "value"