Module: Xcodeproj::Plist

Defined in:
lib/xcodeproj/plist.rb

Overview

Provides support for loading and serializing property list files.

Constant Summary collapse

KNOWN_IMPLEMENTATIONS =

The known modules that can serialize plists.

[]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.implementationNil

Deprecated.

This method will be removed in 2.0

Returns:

  • (Nil)


68
69
70
# File 'lib/xcodeproj/plist.rb', line 68

def implementation
  @implementation
end

Class Method Details

.autoload_implementationNil

Deprecated.

This method will be removed in 2.0

Returns:

  • (Nil)


75
76
# File 'lib/xcodeproj/plist.rb', line 75

def self.autoload_implementation
end

.file_in_conflict?(contents) ⇒ Bool

Returns Checks whether there are merge conflicts in the file.

Parameters:

  • contents (#to_s)

    The contents of the file.

Returns:

  • (Bool)

    Checks whether there are merge conflicts in the file.



83
84
85
86
87
88
89
90
91
92
# File 'lib/xcodeproj/plist.rb', line 83

def self.file_in_conflict?(contents)
  conflict_regex = /
    ^<{7}(?!<) # Exactly 7 left arrows at the beginning of the line
    [\w\W]* # Anything
    ^={7}(?!=) # Exactly 7 equality symbols at the beginning of the line
    [\w\W]* # Anything
    ^>{7}(?!>) # Exactly 7 right arrows at the beginning of the line
  /xm
  contents.match(conflict_regex)
end

.read_from_path(path) ⇒ Hash

Returns the native objects loaded from a property list file.

Parameters:

  • path (#to_s)

    The path of the file.

Returns:

  • (Hash)

    Returns the native objects loaded from a property list file.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xcodeproj/plist.rb', line 14

def self.read_from_path(path)
  path = path.to_s
  unless File.exist?(path)
    raise Informative, "The plist file at path `#{path}` doesn't exist."
  end
  contents = File.read(path)
  if file_in_conflict?(contents)
    raise Informative, "The file `#{path}` is in a merge conflict."
  end
  case Nanaimo::Reader.plist_type(contents)
  when :xml, :binary
    CFPropertyList.native_types(CFPropertyList::List.new(:data => contents).value)
  else
    Nanaimo::Reader.new(contents).parse!.as_ruby
  end
end

.write_to_path(hash, path) ⇒ Object

Serializes a hash as an XML property list file.

Parameters:

  • hash (#to_hash)

    The hash to store.

  • path (#to_s)

    The path of the file.

Raises:

  • (IOError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/xcodeproj/plist.rb', line 39

def self.write_to_path(hash, path)
  if hash.respond_to?(:to_hash)
    hash = hash.to_hash
  else
    raise TypeError, "The given `#{hash.inspect}` must respond " \
                      "to #to_hash'."
  end

  unless path.is_a?(String) || path.is_a?(Pathname)
    raise TypeError, "The given `#{path}` must be a string or 'pathname'."
  end
  path = path.to_s
  raise IOError, 'Empty path.' if path.empty?

  File.open(path, 'w') do |f|
    plist = Nanaimo::Plist.new(hash, :xml)
    Nanaimo::Writer::XMLWriter.new(plist, :pretty => true, :output => f, :strict => false).write
  end
end