binaryparse 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/binaryparse.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = "binaryparse"
5
- s.version = "0.1.4"
5
+ s.version = "0.1.5"
6
6
  s.author = "Patrick Hurley"
7
7
  s.email = "phurley@gmail.com"
8
8
  s.homepage = "http://binaryparse.rubyforge.org/"
data/lib/blocker.rb CHANGED
@@ -144,10 +144,14 @@ module BinaryBlocker
144
144
  if @post_deblock
145
145
  self.value = @post_deblock.call(self.value)
146
146
  end
147
- self.value
147
+ self.value || self.valid?
148
148
  end
149
149
  end
150
-
150
+
151
+ def key_value?
152
+ @opts[:key]
153
+ end
154
+
151
155
  protected
152
156
 
153
157
  # Override valid? to allow check constraints on a particular
@@ -315,6 +319,7 @@ module BinaryBlocker
315
319
  sym = sym.to_s
316
320
  if sym[-1] == ?=
317
321
  if pos = @lookup[sym[0..-2].to_sym]
322
+ raise NoMethodError.new("undefined method `#{sym}''") if @value[pos].key_value?
318
323
  return @value[pos].value = args.first
319
324
  end
320
325
  end
@@ -479,13 +484,23 @@ module BinaryBlocker
479
484
  buffer = io.read(@bytes)
480
485
  result = buffer.unpack(@format)
481
486
  year, month, day = result.first.unpack("A4A2A2").map { |v| v.to_i }
482
- if year.zero?
487
+ if month.zero?
483
488
  nil
484
489
  else
485
490
  Date.civil(year, month, day)
486
491
  end
487
492
  end
488
-
493
+
494
+ def valid?
495
+ case @value
496
+ when Date : true
497
+ when Time : true
498
+ when nil : true
499
+ else
500
+ false
501
+ end
502
+ end
503
+
489
504
  end
490
505
  BinaryBlocker.register_klass(:date, PackedDateEncoder)
491
506
 
@@ -507,13 +522,22 @@ module BinaryBlocker
507
522
  buffer = io.read(@bytes)
508
523
  result = buffer.unpack(@format)
509
524
  year, month, day, hour, min, sec = result.first.unpack("A4A2A2A2A2A2").map { |v| v.to_i }
510
- if year.zero?
525
+ if month.zero?
511
526
  nil
512
527
  else
513
528
  Time.local(year, month, day, hour, min, sec)
514
529
  end
515
530
  end
516
531
 
532
+ def valid?
533
+ case @value
534
+ when Time : true
535
+ when nil : true
536
+ else
537
+ false
538
+ end
539
+ end
540
+
517
541
  end
518
542
  BinaryBlocker.register_klass(:time, PackedDateTimeEncoder)
519
543
  BinaryBlocker.register_klass(:datetime, PackedDateTimeEncoder)
data/test/test_blocker.rb CHANGED
@@ -24,20 +24,14 @@ class TestBlocker < Test::Unit::TestCase
24
24
 
25
25
  def test_simple_valid
26
26
  bb = BBTest2.new
27
- bb.foo = 32
28
27
  bb.bar = 24
29
28
 
30
- assert_equal(32, bb.foo)
31
29
  assert_equal(24, bb.bar)
32
- assert(!bb.valid?)
33
-
34
- bb.foo = 42
35
30
  assert(bb.valid?)
36
31
  end
37
32
 
38
33
  def test_round_trip
39
34
  bb = BBTest2.new
40
- bb.foo = 42
41
35
  bb.bar = 21
42
36
  buf = bb.block
43
37
 
@@ -53,9 +47,9 @@ class TestBlocker < Test::Unit::TestCase
53
47
 
54
48
  def test_failed_deblock
55
49
  bb = BBTest2.new
56
- bb.foo = 43
57
50
  bb.bar = 21
58
51
  buf = bb.block
52
+ buf[0] = 0
59
53
 
60
54
  bb2 = BBTest2.new
61
55
  status = bb2.deblock(StringIO.new(buf))
@@ -65,7 +59,6 @@ class TestBlocker < Test::Unit::TestCase
65
59
  BBTest2.new(StringIO.new(buf))
66
60
  end
67
61
 
68
- bb.foo = 42
69
62
  io = StringIO.new(bb.block)
70
63
  assert(bb2.deblock(io))
71
64
 
@@ -88,7 +81,6 @@ class TestBlocker < Test::Unit::TestCase
88
81
 
89
82
  def test_has_one_of
90
83
  bs1 = BBSub1.new
91
- bs1.foo = 42
92
84
  buf = bs1.block
93
85
  bb = BBTest3.new(StringIO.new(buf))
94
86
  assert(bb)
@@ -96,7 +88,6 @@ class TestBlocker < Test::Unit::TestCase
96
88
  assert_equal(42, bb.foo.foo)
97
89
 
98
90
  bs2 = BBSub2.new
99
- bs2.bar = 21
100
91
  io = StringIO.new(bs2.block)
101
92
  bb = BBTest3.new(io)
102
93
  assert(bb)
@@ -120,9 +111,7 @@ class TestBlocker < Test::Unit::TestCase
120
111
 
121
112
  def test_fixed_array
122
113
  bs1 = BBSub1.new
123
- bs1.foo = 42
124
114
  bs2 = BBSub2.new
125
- bs2.bar = 21
126
115
 
127
116
  buf = bs1.block + bs2.block + bs1.block
128
117
  bb = BBTest4.new(buf)
@@ -139,11 +128,8 @@ class TestBlocker < Test::Unit::TestCase
139
128
  fa = BBTest4.new
140
129
  assert(fa)
141
130
  fa.fooboo[0] = BBSub1.new
142
- fa.fooboo[0].foo = 42
143
131
  fa.fooboo[1] = BBSub2.new
144
- fa.fooboo[1].bar = 21
145
132
  fa.fooboo[2] = BBSub1.new
146
- fa.fooboo[2].foo = 42
147
133
  assert_equal(42, fa.fooboo[0].foo)
148
134
  assert_equal(21, fa.fooboo[1].bar)
149
135
  assert_equal(42, fa.fooboo[2].foo)
@@ -167,9 +153,7 @@ class TestBlocker < Test::Unit::TestCase
167
153
 
168
154
  def test_counted_array
169
155
  bb1 = BBSub1.new
170
- bb1.foo = 42
171
156
  bb2 = BBSub2.new
172
- bb2.bar = 21
173
157
 
174
158
  fa = BBTest5.new
175
159
  assert(fa)
@@ -430,4 +414,29 @@ class TestBlocker < Test::Unit::TestCase
430
414
  assert_equal('troaeipo', b2.str)
431
415
  end
432
416
 
417
+ class BBDateRecord < BinaryBlocker::Blocker
418
+ has_one :date, :date
419
+ end
420
+
421
+ def test_null_dates
422
+ t = BBDateRecord.new
423
+ assert(buf = t.block)
424
+
425
+ t2 = BBDateRecord.new(buf)
426
+ assert(t2)
427
+ assert_nil(t2.date)
428
+ end
429
+
430
+ class BBTimeRecord < BinaryBlocker::Blocker
431
+ has_one :t, :time
432
+ end
433
+
434
+ def test_null_times
435
+ t = BBTimeRecord.new
436
+ assert(buf = t.block)
437
+
438
+ t2 = BBTimeRecord.new(buf)
439
+ assert(t2)
440
+ assert_nil(t2.t)
441
+ end
433
442
  end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: binaryparse
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.4
6
+ version: 0.1.5
7
7
  date: 2006-09-13 00:00:00 -04:00
8
8
  summary: Binaryparse is a simple Ruby DSL to parse semi-complicated binary structures. This includes structures dynamic in length, which cannot be handled by DL::Struct or BitStructEx.
9
9
  require_paths:
@@ -29,7 +29,6 @@ post_install_message:
29
29
  authors:
30
30
  - Patrick Hurley
31
31
  files:
32
- - binaryparse-0.1.4.gem
33
32
  - binaryparse.gemspec
34
33
  - examples
35
34
  - lib
File without changes