Class: ISO8583::Bitmap
- Inherits:
-
Object
- Object
- ISO8583::Bitmap
- 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) ⇒ 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.
-
#initialize(message = nil) ⇒ 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_s ⇒ Object
Generate a String representation of this bitmap in the form: 01001100110000011010110110010100100110011000001101011011001010.
-
#unset(i) ⇒ Object
Unsets bit #i.
Constructor Details
#initialize(message = nil) ⇒ 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 24 25 |
# File 'lib/iso8583/bitmap.rb', line 18 def initialize( = nil) @bmp = Array.new(128, false) if ! else end end |
Class Method Details
.parse(str) ⇒ Object
Parse the bytes in string and return the Bitmap and bytes remaining in ‘str` after the bitmap is taken away.
103 104 105 106 107 |
# File 'lib/iso8583/bitmap.rb', line 103 def parse(str) bmp = Bitmap.new(str) rest = bmp[1] ? str[16, str.length] : str[8, str.length] [ bmp, rest ] end |
Instance Method Details
#[](i) ⇒ Object
Returns whether the bit is set or not.
33 34 35 |
# File 'lib/iso8583/bitmap.rb', line 33 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.
39 40 41 42 43 44 45 46 |
# File 'lib/iso8583/bitmap.rb', line 39 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.
28 29 30 |
# File 'lib/iso8583/bitmap.rb', line 28 def each #:yields: each bit set in the bitmap. @bmp.each_with_index {|set, i| yield i+1 if set} end |
#set(i) ⇒ Object
Sets bit #i
49 50 51 |
# File 'lib/iso8583/bitmap.rb', line 49 def set(i) self[i] = true end |
#to_bytes ⇒ Object Also known as: to_b
Generate the bytes representing this bitmap.
59 60 61 62 63 64 |
# File 'lib/iso8583/bitmap.rb', line 59 def to_bytes arr = [self.to_s] # tricky and ugly, setting bit[1] only when generating to_s... count = self[1] ? 128 : 64 arr.pack("B#{count}") end |
#to_s ⇒ Object
Generate a String representation of this bitmap in the form: 01001100110000011010110110010100100110011000001101011011001010
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/iso8583/bitmap.rb', line 69 def to_s #check whether any `high` bits are set @bmp[0] = false 65.upto(128) {|i| if self[i] # if so, set continuation bit @bmp[0] = true break end } str = "" 1.upto(self[1] ? 128 : 64) {|i| str << (self[i] ? "1" : "0") } str end |
#unset(i) ⇒ Object
Unsets bit #i
54 55 56 |
# File 'lib/iso8583/bitmap.rb', line 54 def unset(i) self[i] = false end |