Sha256: 9eb1d61896391e434ed470839d1d2d0cd9f547e075a2233ccdf51acd844aa549

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 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)
      new(CFPropertyList::List.new(:file => path, :format => ENV['UNITY_BUILD'].present? ? FORMAT_XML : FORMAT_AUTO))
    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, :convert_unknown_to_string => true)
    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

1 entries across 1 versions & 1 rubygems

Version Path
nixenvironment-0.0.112 lib/nixenvironment/plist.rb