Class BinaryBlocker::BitFieldEncoder
In: lib/blocker.rb
Parent: Encoder
GroupEncoder ListOfEncoder Blocker FixedArrayEncoder CountedArrayEncoder Encoder SimpleEncoder BitFieldEncoder OneOfEncoder PackedNumberEncoder PackedDateEncoder PackedDateTimeEncoder FixedStringEncoder FixedUTF16StringEncoder BinaryBlocker Module: BinaryBlocker

Methods

Public Class methods

[Source]

     # File lib/blocker.rb, line 666
666:     def initialize(type, bit_info, *opts)
667:       @type = BinaryBlocker.pack_symbols[type] || type
668:       @length = BinaryBlocker.sizeof_format(@type)
669:       @bit_info = {}
670:       pos = 0
671:       bit_info.each do |bi|
672:         case bi
673:         when Symbol
674:           @bit_info[bi.to_sym] = [pos,1]
675:           pos += 1
676:           
677:         when Fixnum
678:           pos += bi
679:           
680:         when Array
681:           @bit_info[bi.first.to_sym] = [pos, bi.last.to_i]
682:           pos += bi.last.to_i
683:         end
684:       end
685:       @value = 0
686:       initialize_options(*opts)
687:       initialize_data(*opts)
688:     end

Public Instance methods

[Source]

     # File lib/blocker.rb, line 690
690:     def internal_block(val)
691:       [val.raw_value].pack(@type)
692:     end

[Source]

     # File lib/blocker.rb, line 694
694:     def internal_deblock(io)
695:       buffer = io.read(@length)
696:       result = buffer.unpack(@type)
697:       result.first
698:     end

[Source]

     # File lib/blocker.rb, line 708
708:     def method_missing(sym, *args)
709:       if (bi = @bit_info[sym])
710:         v = @value >> bi.first
711:         mask = (1 << bi.last) - 1
712:         v = v & mask
713:       else
714:         sym = sym.to_s
715:         if sym[-1] == ?=
716:           if bi = @bit_info[sym[0..-2].to_sym]
717:             @value &= ~(((1 << bi.last) - 1) << bi.first)
718:             @value |= args.first.to_i << bi.first 
719:             return @value
720:           end
721:         end
722:         raise NoMethodError.new("undefined method `#{sym}''")
723:         # I was using super, but it was throwing a different error
724:         # which seemed wrong, not sure why
725:       end
726:     end

[Source]

     # File lib/blocker.rb, line 704
704:     def raw_value
705:       @value
706:     end

[Source]

     # File lib/blocker.rb, line 700
700:     def value
701:       self
702:     end

[Validate]