class DataTypes::BitField

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method DataTypes::BaseType::new
# File lib/active_model_serializers_binary/data_types.rb, line 135
def initialize(options = {})
  length = options[:bin_length].blank? ? 1 : (options[:bin_length] > 32 ? 32 : options[:bin_length])
  super options.merge :bit_length => length, :sign => :unsigned
end

Public Instance Methods

dump(value=nil) click to toggle source
# File lib/active_model_serializers_binary/data_types.rb, line 160
def dump(value=nil)
  before_dump( value )
  data = @value.pack(format).unpack('b*').first.chars.each_slice(word_length).map(&:join).map{|n| n.slice(0,bit_length)}
  @raw_value = [data.join].pack('b*').unpack('C*')
end
format() click to toggle source
# File lib/active_model_serializers_binary/data_types.rb, line 140
def format
  if bit_length <= 8 # 8 bits
    'C*'
  elsif bit_length <= 16 # 16 bits
    'v*'
  else # 32 bits
    'l*'
  end
end
load(raw_value) click to toggle source
# File lib/active_model_serializers_binary/data_types.rb, line 166
def load(raw_value)
  self.value = check_raw_value(raw_value).pack('C*').unpack('b*').first.chars.slice(0,@bit_length*@count).each_slice(bit_length).map(&:join).map{|n| [n].pack('b*').unpack('C*').first}
  after_load
end
word_length() click to toggle source
# File lib/active_model_serializers_binary/data_types.rb, line 150
def word_length
  if bit_length <= 8 # 8 bits
    8
  elsif bit_length <= 16 # 16 bits
    16
  else # 32 bits
    32
  end
end