lib/bindata/int.rb in bindata-2.4.3 vs lib/bindata/int.rb in bindata-2.4.4
- old
+ new
@@ -1,19 +1,24 @@
+require 'thread'
require 'bindata/base_primitive'
module BinData
# Defines a number of classes that contain an integer. The integer
# is defined by endian, signedness and number of bytes.
module Int #:nodoc: all
+ @@mutex = Mutex.new
+
class << self
def define_class(name, nbits, endian, signed)
- unless BinData.const_defined?(name)
- BinData.module_eval <<-END
- class #{name} < BinData::BasePrimitive
- Int.define_methods(self, #{nbits}, :#{endian}, :#{signed})
- end
- END
+ @@mutex.synchronize do
+ unless BinData.const_defined?(name)
+ new_class = Class.new(BinData::BasePrimitive)
+ Int.define_methods(new_class, nbits, endian.to_sym, signed.to_sym)
+ RegisteredClasses.register(name, new_class)
+
+ BinData.const_set(name, new_class)
+ end
end
BinData.const_get(name)
end