Class: DataTypes::UInt16

Inherits:
BaseType show all
Defined in:
lib/active_model_serializers_binary/data_types.rb

Instance Attribute Summary

Attributes inherited from BaseType

#bit_length, #count, #endianess, #length, #name, #parent, #raw_value, #sign, #type, #value

Instance Method Summary collapse

Methods inherited from BaseType

#after_load, #before_dump, #check, #check_raw_value, #check_value, #size, #to_s, #trim

Constructor Details

#initialize(options = {}) ⇒ UInt16

Returns a new instance of UInt16.



70
71
72
# File 'lib/active_model_serializers_binary/data_types.rb', line 70

def initialize(options = {})
  super options.merge :bit_length => 16, :sign => :unsigned
end

Instance Method Details

#dump(value = nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/active_model_serializers_binary/data_types.rb', line 74

def dump(value=nil)
  before_dump( value )
  if @endianess == :big
    @raw_value = @value.pack('S>*').unpack('C*')
  else
    @raw_value = @value.pack('S<*').unpack('C*')
  end
end

#load(raw_value) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/active_model_serializers_binary/data_types.rb', line 83

def load(raw_value) 
  @raw_value = check_raw_value(raw_value)
  if @endianess == :big
    self.value = @raw_value.pack('C*').unpack('S>*')
  else
    self.value = @raw_value.pack('C*').unpack('S<*')
  end
  after_load
end