Class: ISO8583::Bitmap

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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
# File 'lib/iso8583/bitmap.rb', line 18

def initialize(message = nil)
  @bmp = Array.new(128, false)
  if message
    initialize_from_message message
  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.



102
103
104
105
106
# File 'lib/iso8583/bitmap.rb', line 102

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.



32
33
34
# File 'lib/iso8583/bitmap.rb', line 32

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.



38
39
40
41
42
43
44
45
# File 'lib/iso8583/bitmap.rb', line 38

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

#eachObject

yield once with the number of each set field. :yields: each bit set in the bitmap. Except for the first bit which is used to determine the bitmap sizec



27
28
29
# File 'lib/iso8583/bitmap.rb', line 27

def each
  @bmp.each_with_index {|set, i| yield i+1 if set && i != 0}
end

#set(i) ⇒ Object

Sets bit #i



48
49
50
# File 'lib/iso8583/bitmap.rb', line 48

def set(i)
  self[i] = true
end

#to_bytesObject Also known as: to_b

Generate the bytes representing this bitmap.



58
59
60
61
62
63
# File 'lib/iso8583/bitmap.rb', line 58

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_sObject

Generate a String representation of this bitmap in the form: 01001100110000011010110110010100100110011000001101011011001010



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/iso8583/bitmap.rb', line 68

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 = "".force_encoding("ASCII-8BIT")
  1.upto(self[1] ? 128 : 64) {|i|
    str << (self[i] ? "1" : "0")
  }
  str
end

#unset(i) ⇒ Object

Unsets bit #i



53
54
55
# File 'lib/iso8583/bitmap.rb', line 53

def unset(i)
  self[i] = false
end