Class: BinaryBlocker::PackedDateTimeEncoder

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



670
671
672
673
# File 'lib/blocker.rb', line 670

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

#internal_block(val) ⇒ Object



675
676
677
678
679
680
681
# File 'lib/blocker.rb', line 675

def internal_block(val)
  if val
    super sprintf("%04d%02d%02d%02d%02d%02d", val.year, val.month, val.mday, val.hour, val.min, val.sec).to_i
  else
    super 0
  end
end

#internal_deblock(io) ⇒ Object



683
684
685
686
687
688
689
690
691
692
# File 'lib/blocker.rb', line 683

def internal_deblock(io)
  buffer = io.read(@bytes)
  result = buffer.unpack(@format)
  year, month, day, hour, min, sec = result.first.unpack("A4A2A2A2A2A2").map { |v| v.to_i }
  if month.zero?
    nil
  else
    Time.local(year, month, day, hour, min, sec)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


694
695
696
697
698
699
700
701
# File 'lib/blocker.rb', line 694

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