binaryparse 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/blocker.rb +41 -0
  2. data/test/test_blocker.rb +20 -0
  3. metadata +2 -2
data/lib/blocker.rb CHANGED
@@ -591,6 +591,47 @@ module BinaryBlocker
591
591
  BinaryBlocker.register_klass(:time, PackedDateTimeEncoder)
592
592
  BinaryBlocker.register_klass(:datetime, PackedDateTimeEncoder)
593
593
 
594
+ # Seems awfully specific, why is this here? Well my boss was nice
595
+ # enough to open source the code, so I figured something that made
596
+ # our (company) work easier could go in, even if I would have
597
+ # normally rejected it.
598
+ class PackedDateTimeHHMMEncoder < PackedNumberEncoder
599
+ def initialize_options(*opts)
600
+ super
601
+ @opts[:length] = 12
602
+ end
603
+
604
+ def internal_block(val)
605
+ if val
606
+ super sprintf("%04d%02d%02d%02d%02d", val.year, val.month, val.mday, val.hour, val.min).to_i
607
+ else
608
+ super 0
609
+ end
610
+ end
611
+
612
+ def internal_deblock(io)
613
+ buffer = io.read(@bytes)
614
+ result = buffer.unpack(@format)
615
+ year, month, day, hour, min = result.first.unpack("A4A2A2A2A2").map { |v| v.to_i }
616
+ if month.zero?
617
+ nil
618
+ else
619
+ Time.local(year, month, day, hour, min, 0)
620
+ end
621
+ end
622
+
623
+ def valid?
624
+ case @value
625
+ when Time : true
626
+ when nil : true
627
+ else
628
+ false
629
+ end
630
+ end
631
+ end
632
+ BinaryBlocker.register_klass(:time_hhmm, PackedDateTimeHHMMEncoder)
633
+
634
+
594
635
  class OneOfEncoder < Encoder
595
636
 
596
637
  def inspect
data/test/test_blocker.rb CHANGED
@@ -498,4 +498,24 @@ class TestBlocker < Test::Unit::TestCase
498
498
  assert(ss)
499
499
  #assert_equal("Name", ss.name)
500
500
  end
501
+
502
+ class BBTimeHHMM < BinaryBlocker::Blocker
503
+ has_one :now, :time_hhmm
504
+ end
505
+
506
+ def test_packed_datetime_hhmm
507
+ now = Time.local(1985, 5, 30, 7, 6, 5)
508
+ b = BBTimeHHMM.new
509
+ b.now = now
510
+
511
+ buf = b.block
512
+ assert_equal(12 / 2, buf.size)
513
+
514
+ b2 = BBTimeHHMM.new(buf)
515
+ assert_not_equal(now, b2.now)
516
+
517
+ now = Time.local(1985, 5, 30, 7, 6, 0)
518
+ assert_equal(now, b2.now)
519
+ end
520
+
501
521
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: binaryparse
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.2
7
- date: 2006-11-03 00:00:00 -05:00
6
+ version: 0.2.3
7
+ date: 2006-11-07 00:00:00 -05: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:
10
10
  - lib