Module: BinData::IntFactory

Included in:
BinData
Defined in:
lib/bindata/int.rb

Overview

Create classes on demand

Instance Method Summary collapse

Instance Method Details

#const_missing(name) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/bindata/int.rb', line 193

def const_missing(name)
  mappings = {
    /^Uint(\d+)be$/ => [:big,    :unsigned],
    /^Uint(\d+)le$/ => [:little, :unsigned],
    /^Int(\d+)be$/  => [:big,    :signed],
    /^Int(\d+)le$/  => [:little, :signed],
  }

  mappings.each_pair do |regex, args|
    if regex =~ name.to_s
      nbits = $1.to_i
      if nbits > 0 && (nbits % 8).zero?
        return Int.define_class(name, nbits, *args)
      end
    end
  end

  super
end