Class: BinaryBlocker::PackedDateTimeHHMMEncoder

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

Overview

Seems awfully specific, why is this here? Well my boss was nice enough to open source the code, so I figured something that made our (company) work easier could go in, even if I would have normally rejected it.

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



712
713
714
715
# File 'lib/blocker.rb', line 712

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

#internal_block(val) ⇒ Object



717
718
719
720
721
722
723
# File 'lib/blocker.rb', line 717

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

#internal_deblock(io) ⇒ Object



725
726
727
728
729
730
731
732
733
734
# File 'lib/blocker.rb', line 725

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

#valid?Boolean

Returns:

  • (Boolean)


736
737
738
739
740
741
742
743
# File 'lib/blocker.rb', line 736

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