udat 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/udat.rb +43 -31
  2. metadata +2 -2
data/lib/udat.rb CHANGED
@@ -316,40 +316,49 @@ class Udat
316
316
  # The object must be enclosed in square brackets.
317
317
  def self.read_from_stream(io)
318
318
  begin
319
- char = io.read(1)
320
- return nil if char.nil? or char.length < 1
321
- if char == "\\"
319
+ begin
322
320
  char = io.read(1)
323
321
  return nil if char.nil? or char.length < 1
324
- end
325
- end while char != "["
326
- buffer = ""
327
- level = 1
328
- while true
329
- char = io.read(1)
330
- if char.nil? or char.length < 1
331
- return EOFError, "Unexpected end of file in UDAT stream."
332
- end
333
- if char == "\\"
334
- buffer << char
322
+ if char == "\\"
323
+ char = io.read(1)
324
+ return nil if char.nil? or char.length < 1
325
+ end
326
+ end while char != "[" and char != "<"
327
+ buffer = (char == "[") ? "" : nil
328
+ level = 1
329
+ while true
335
330
  char = io.read(1)
336
331
  if char.nil? or char.length < 1
337
332
  return EOFError, "Unexpected end of file in UDAT stream."
338
333
  end
339
- elsif char == "[" or char == "<"
340
- level += 1
341
- elsif char == "]" or char == ">"
342
- level -= 1
343
- end
344
- if level > 0
345
- buffer << char
346
- else
347
- break
334
+ if char == "\\"
335
+ buffer << char if buffer
336
+ char = io.read(1)
337
+ if char.nil? or char.length < 1
338
+ return EOFError, "Unexpected end of file in UDAT stream."
339
+ end
340
+ elsif char == "[" or char == "<"
341
+ level += 1
342
+ elsif char == "]" or char == ">"
343
+ level -= 1
344
+ end
345
+ if level > 0
346
+ buffer << char if buffer
347
+ else
348
+ break
349
+ end
348
350
  end
349
- end
351
+ end while buffer.nil?
350
352
  return parse(buffer)
351
353
  end
352
354
 
355
+ # Encodes an UDAT object and writes it enclosed by square brackets to a
356
+ # stream.
357
+ def write_to_stream(io)
358
+ io << "[#{self.to_udat}]"
359
+ return self
360
+ end
361
+
353
362
  end
354
363
 
355
364
 
@@ -566,14 +575,13 @@ class UdatCollection < Udat
566
575
  end
567
576
 
568
577
  # Behaves differently depending on the type of the argument. If the
569
- # argument is an Array (responds to 'to_ary') with 2 elements, a new
570
- # key/value pair is added, with the key being the first element of the
571
- # array, and the value being the second argument of the array. Arrays
572
- # with any other number of elements cause an error. If the argument is
573
- # not an array, then it is added as a value without a key.
578
+ # argument is an Array with 2 elements, a new key/value pair is added,
579
+ # with the key being the first element of the array, and the value being
580
+ # the second argument of the array. Arrays with any other number of
581
+ # elements cause an error. If the argument is not an array, then it is
582
+ # added as a value without a key.
574
583
  def <<(value)
575
- if value.respond_to? :to_ary
576
- value = value.to_ary
584
+ if value.kind_of? Array
577
585
  unless value.length == 2
578
586
  raise ArgumentError,
579
587
  "#{value.length} array elements found where 2 were expected."
@@ -959,5 +967,9 @@ class IO
959
967
  def read_udat
960
968
  Udat.read_from_stream(self)
961
969
  end
970
+ # Calls object.write_to_stream(self).
971
+ def write_udat(object)
972
+ object.write_to_stream(self)
973
+ end
962
974
  end
963
975
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: udat
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.1
7
- date: 2007-05-23 00:00:00 +00:00
6
+ version: 1.1.2
7
+ date: 2007-05-24 00:00:00 +00:00
8
8
  summary: Parser and generator for UDAT documents, a generic data format similar to XML or YAML.
9
9
  require_paths:
10
10
  - lib/