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


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

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


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

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.


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

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.


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

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