Class: Nanaimo::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/nanaimo/reader.rb

Overview

Transforms plist strings into Plist objects.

Defined Under Namespace

Classes: ParseError, UnsupportedPlistFormatError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ Reader

Returns a new instance of Reader.

Parameters:

  • contents (String)

    The plist to be parsed



95
96
97
# File 'lib/nanaimo/reader.rb', line 95

def initialize(contents)
  @scanner = StringScanner.new(contents)
end

Class Method Details

.from_file(file_path) ⇒ Plist

Returns A parsed plist from the given file.

Parameters:

Returns:

  • (Plist)

    A parsed plist from the given file



89
90
91
# File 'lib/nanaimo/reader.rb', line 89

def self.from_file(file_path)
  new(File.read(file_path))
end

.plist_type(plist_contents) ⇒ Symbol

Returns The file format of the plist in the given string.

Parameters:

Returns:

  • (Symbol)

    The file format of the plist in the given string.



74
75
76
77
78
79
80
81
82
83
# File 'lib/nanaimo/reader.rb', line 74

def self.plist_type(plist_contents)
  case plist_contents
  when /\Abplist/
    :binary
  when /\A<\?xml/
    :xml
  else
    :ascii
  end
end

Instance Method Details

#parse!Plist

Parses the contents of the plist

Returns:

  • (Plist)

    The parsed Plist object.



103
104
105
106
107
108
109
110
111
112
# File 'lib/nanaimo/reader.rb', line 103

def parse!
  plist_format = ensure_ascii_plist!
  read_string_encoding
  root_object = parse_object

  eat_whitespace!
  raise_parser_error ParseError, 'Found additional characters after parsing the root plist object' unless @scanner.eos?

  Nanaimo::Plist.new(root_object, plist_format)
end