Sha256: 0f96f3c64d8b955b5787e34be4ec57b5fe888f70b848215dbf9b11cbe4fb4dca

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 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

6 entries across 6 versions & 1 rubygems

Version Path
nixenvironment-0.0.118 lib/nixenvironment/plist.rb
nixenvironment-0.0.117 lib/nixenvironment/plist.rb
nixenvironment-0.0.116 lib/nixenvironment/plist.rb
nixenvironment-0.0.115 lib/nixenvironment/plist.rb
nixenvironment-0.0.114 lib/nixenvironment/plist.rb
nixenvironment-0.0.113 lib/nixenvironment/plist.rb