Class: BinaryBlocker::PackedDateEncoder

Inherits:
PackedNumberEncoder show all
Defined in:
lib/blocker.rb

Instance Method Summary collapse

Methods inherited from PackedNumberEncoder

#initialize

Methods inherited from SimpleEncoder

#inspect, register

Methods inherited from Encoder

#block, #deblock, #initialize, #key_value?, #me

Constructor Details

This class inherits a constructor from BinaryBlocker::PackedNumberEncoder

Instance Method Details

#initialize_options(*opts) ⇒ Object



594
595
596
597
# File 'lib/blocker.rb', line 594

def initialize_options(*opts)
  super
  @opts[:length] = 8
end

#internal_block(val) ⇒ Object



599
600
601
602
603
604
605
# File 'lib/blocker.rb', line 599

def internal_block(val)
  if val
    super val.year * 10000 + val.month * 100 + val.mday
  else
    super 0
  end 
end

#internal_deblock(io) ⇒ Object



607
608
609
610
611
612
613
614
615
616
# File 'lib/blocker.rb', line 607

def internal_deblock(io)
  buffer = io.read(@bytes)
  result = buffer.unpack(@format)
  year, month, day = result.first.unpack("A4A2A2").map { |v| v.to_i }
  if month.zero?
    nil
  else
    Date.civil(year, month, day)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


618
619
620
621
622
623
624
625
626
# File 'lib/blocker.rb', line 618

def valid?
  case @value
  when Date ; true
  when Time ; true
  when nil  ; true
  else 
    false
  end        
end