Class: DataTypes::BitField
- 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 = {}) ⇒ BitField
Returns a new instance of BitField.
135
136
137
138
|
# 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
|
Instance Method Details
#dump(value = nil) ⇒ Object
160
161
162
163
164
|
# 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
|
140
141
142
143
144
145
146
147
148
|
# File 'lib/active_model_serializers_binary/data_types.rb', line 140
def format
if bit_length <= 8
'C*'
elsif bit_length <= 16
'v*'
else
'l*'
end
end
|
#load(raw_value) ⇒ Object
166
167
168
169
|
# 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 ⇒ Object
150
151
152
153
154
155
156
157
158
|
# File 'lib/active_model_serializers_binary/data_types.rb', line 150
def word_length
if bit_length <= 8
8
elsif bit_length <= 16
16
else
32
end
end
|