Class: ISO8583::Bitmap
- Inherits:
-
Object
- Object
- ISO8583::Bitmap
- Includes:
- DaFunk::Helper
- Defined in:
- lib/iso8583/bitmap.rb
Overview
This class constructs an object for handling bitmaps with which ISO8583 messages typically begin. Bitmaps are either 8 or 16 bytes long, an extended length bitmap is indicated by the first bit being set. In all likelyhood, you won’t be using this class much, it’s used transparently by the Message class.
Class Method Summary collapse
-
.parse(str, hex_bitmap = false) ⇒ Object
Parse the bytes in string and return the Bitmap and bytes remaining in ‘str` after the bitmap is taken away.
Instance Method Summary collapse
-
#[](i) ⇒ Object
Returns whether the bit is set or not.
-
#[]=(i, value) ⇒ Object
Set the bit to the indicated value.
-
#each ⇒ Object
yield once with the number of each set field.
- #hex_bitmap? ⇒ Boolean
-
#initialize(message = nil, hex_bitmap = false) ⇒ Bitmap
constructor
create a new Bitmap object.
-
#set(i) ⇒ Object
Sets bit #i.
-
#to_bytes ⇒ Object
(also: #to_b)
Generate the bytes representing this bitmap.
- #to_hex ⇒ Object
-
#to_s ⇒ Object
Generate a String representation of this bitmap in the form: 01001100110000011010110110010100100110011000001101011011001010.
-
#unset(i) ⇒ Object
Unsets bit #i.
Constructor Details
#initialize(message = nil, hex_bitmap = false) ⇒ Bitmap
create a new Bitmap object. In case an iso message is passed in, that messages bitmap will be parsed. If not, this initializes and empty bitmap.
18 19 20 21 22 23 |
# File 'lib/iso8583/bitmap.rb', line 18 def initialize( = nil, hex_bitmap=false) @bmp = Array.new(128, false) @hex_bitmap = hex_bitmap ? () : nil end |
Class Method Details
.parse(str, hex_bitmap = false) ⇒ Object
Parse the bytes in string and return the Bitmap and bytes remaining in ‘str` after the bitmap is taken away.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/iso8583/bitmap.rb', line 111 def parse(str, hex_bitmap = false) bmp = Bitmap.new(str, hex_bitmap) rest = if bmp.hex_bitmap? bmp[1] ? str[32, str.length] : str[16, str.length] else bmp[1] ? str[16, str.length] : str[8, str.length] end [ bmp, rest ] end |
Instance Method Details
#[](i) ⇒ Object
Returns whether the bit is set or not.
35 36 37 |
# File 'lib/iso8583/bitmap.rb', line 35 def [](i) @bmp[i-1] end |
#[]=(i, value) ⇒ Object
Set the bit to the indicated value. Only ‘true` sets the bit, any other value unsets it.
41 42 43 44 45 46 47 48 |
# File 'lib/iso8583/bitmap.rb', line 41 def []=(i, value) if i > 128 raise ISO8583Exception.new("Bits > 128 are not permitted.") elsif i < 2 raise ISO8583Exception.new("Bits < 2 are not permitted (continutation bit is set automatically)") end @bmp[i-1] = (value == true) end |
#each ⇒ Object
yield once with the number of each set field.
30 31 32 |
# File 'lib/iso8583/bitmap.rb', line 30 def each #:yields: each bit set in the bitmap except the first bit. @bmp[1..-1].each_with_index {|set, i| yield i+2 if set} end |
#hex_bitmap? ⇒ Boolean
25 26 27 |
# File 'lib/iso8583/bitmap.rb', line 25 def hex_bitmap? !!@hex_bitmap end |
#set(i) ⇒ Object
Sets bit #i
51 52 53 |
# File 'lib/iso8583/bitmap.rb', line 51 def set(i) self[i] = true end |
#to_bytes ⇒ Object Also known as: to_b
Generate the bytes representing this bitmap.
61 62 63 |
# File 'lib/iso8583/bitmap.rb', line 61 def to_bytes self.to_s.to_i(2).to_s(16).upcase end |
#to_hex ⇒ Object
66 67 68 |
# File 'lib/iso8583/bitmap.rb', line 66 def to_hex self.to_s.to_i(2).to_s(16).upcase.unpack("H*").first end |
#to_s ⇒ Object
Generate a String representation of this bitmap in the form: 01001100110000011010110110010100100110011000001101011011001010
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/iso8583/bitmap.rb', line 72 def to_s #check whether any `high` bits are set ret = (65..128).find {|bit| !!self[bit]} high, @bmp[0] = ret ? [128, true] : [64, false] str = "" 1.upto(high) do|i| str << (self[i] ? '1' : '0') end str end |
#unset(i) ⇒ Object
Unsets bit #i
56 57 58 |
# File 'lib/iso8583/bitmap.rb', line 56 def unset(i) self[i] = false end |