Sha256: cb2ab89479bf13a19023630293e312fca4d4a65ae5b7dc5b97ace147765a0d25

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

require 'cfpropertylist'

module Nixenvironment
  class Plist
    FORMAT_BINARY = CFPropertyList::List::FORMAT_BINARY
    FORMAT_XML    = CFPropertyList::List::FORMAT_XML
    FORMAT_PLAIN  = CFPropertyList::List::FORMAT_PLAIN
    FORMAT_AUTO   = CFPropertyList::List::FORMAT_AUTO

    def self.from_file(path)
      # FIXME
      # used instead of new(CFPropertyList::List.new(:file => path))
      # because of odd <ArgumentError: invalid byte sequence in US-ASCII>
      plist = CFPropertyList::List.new
      plist.load_str(File.read(path))
      plist.filename = path
      new(plist)
    end

    def self.from_hash(hash)
      plist        = CFPropertyList::List.new
      plist.value  = CFPropertyList.guess(hash)
      plist.format = FORMAT_XML
      new(plist)
    end

    def self.from_str(str)
      plist = CFPropertyList::List.new
      plist.load_str(str)
      new(plist)
    end

    def initialize(plist)
      @plist = plist
      @path  = @plist.filename
      @data  = CFPropertyList.native_types(@plist.value)
    end

    def [](key)
      @data[key]
    end

    def []=(key, value)
      @data[key] = value
      @plist.value = CFPropertyList.guess(@data)
    end

    def save(path = nil, format = nil, formatted = true)
      path ||= @path
      raise 'Path to save plist is not specified!' unless path
      @plist.save(path, format, :formatted => formatted)
    end

    def to_s(format = FORMAT_AUTO, formatted = true)
      @plist.to_str(format, :formatted => formatted)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nixenvironment-0.0.65 lib/nixenvironment/plist.rb
nixenvironment-0.0.64 lib/nixenvironment/plist.rb
nixenvironment-0.0.63 lib/nixenvironment/plist.rb
nixenvironment-0.0.62 lib/nixenvironment/plist.rb
nixenvironment-0.0.61 lib/nixenvironment/plist.rb