Class: BinaryBlocker::BitFieldEncoder
Instance Method Summary
collapse
Methods inherited from Encoder
#block, #deblock, #key_value?, #me
Constructor Details
#initialize(type, bit_info, *opts) ⇒ BitFieldEncoder
Returns a new instance of BitFieldEncoder.
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
# File 'lib/blocker.rb', line 1002
def initialize(type, bit_info, *opts)
@type = BinaryBlocker.pack_symbols[type] || type
@length = BinaryBlocker.sizeof_format(@type)
@bit_info = {}
pos = 0
bit_info.each do |bi|
case bi
when Symbol
@bit_info[bi.to_sym] = [pos,1]
pos += 1
when Fixnum
pos += bi
when Array
@bit_info[bi.first.to_sym] = [pos, bi.last.to_i]
pos += bi.last.to_i
end
end
@value = 0
initialize_options(*opts)
initialize_data(*opts)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
|
# File 'lib/blocker.rb', line 1044
def method_missing(sym, *args)
if (bi = @bit_info[sym])
v = @value >> bi.first
mask = (1 << bi.last) - 1
v = v & mask
else
sym = sym.to_s
if sym[-1] == ?=
if bi = @bit_info[sym[0..-2].to_sym]
@value &= ~(((1 << bi.last) - 1) << bi.first)
@value |= args.first.to_i << bi.first
return @value
end
end
raise NoMethodError.new("undefined method `#{sym}''")
end
end
|
Instance Method Details
#internal_block(val) ⇒ Object
1026
1027
1028
|
# File 'lib/blocker.rb', line 1026
def internal_block(val)
[val.raw_value || 0].pack(@type)
end
|
#internal_deblock(io) ⇒ Object
1030
1031
1032
1033
1034
|
# File 'lib/blocker.rb', line 1030
def internal_deblock(io)
buffer = io.read(@length)
result = buffer.unpack(@type)
result.first
end
|
#raw_value ⇒ Object
1040
1041
1042
|
# File 'lib/blocker.rb', line 1040
def raw_value
@value
end
|
#value ⇒ Object
1036
1037
1038
|
# File 'lib/blocker.rb', line 1036
def value
self
end
|