Module: ByteObject::ByteAttributes

Defined in:
lib/ByteObject.rb

Overview

Whenever ByteObject is included into a class, it also extends that class with the ByteAttributes module. This is the module that bestows the byte-specific attribute methods.

Instance Method Summary collapse

Instance Method Details

#attr_byte(key, size, signed) ⇒ void

This method returns an undefined value.

This method creates an ordinary attribute reader and a specialized attribute writer for the given key. The writer will clamp any values passed to the new attribute based on its size and whether or not it is signed. Under normal circumstances, you will not need to call this method yourself. It is used by this module to generate the attribute methods given to any extended classes.

Parameters:

  • key (Symbol)

    The name of the attribute.

  • size (Integer)

    The size, in bits, of the attribute.

  • signed (Boolean)

    Whether or not the attribute can be negative.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ByteObject.rb', line 50

def attr_byte(key, size, signed)
  attr_reader(key)

  bytesize = (2**size)
  min = signed ? -bytesize/2 : 0
  max = signed ? (bytesize/2) - 1 : bytesize - 1

  define_method("#{key}=") do |val|
    instance_variable_set("@#{key}", val.clamp(min, max))
  end
end

#attr_s16bit(*keys) ⇒ Object

Creates an 16-bit signed attribute reader and writer.



# File 'lib/ByteObject.rb', line 26

#attr_s32bit(*keys) ⇒ Object

Creates an 32-bit signed attribute reader and writer.



# File 'lib/ByteObject.rb', line 32

#attr_s64bit(*keys) ⇒ Object

Creates an 64-bit signed attribute reader and writer.



# File 'lib/ByteObject.rb', line 38

#attr_s8bit(*keys) ⇒ Object

Creates an 8-bit signed attribute reader and writer.



# File 'lib/ByteObject.rb', line 20

#attr_u16bit(*keys) ⇒ Object

Creates a 16-bit unsigned attribute reader and writer.



# File 'lib/ByteObject.rb', line 23

#attr_u32bit(*keys) ⇒ Object

Creates an 32-bit unsigned attribute reader and writer.



# File 'lib/ByteObject.rb', line 29

#attr_u64bit(*keys) ⇒ Object

Creates an 64-bit unsigned attribute reader and writer.



# File 'lib/ByteObject.rb', line 35

#attr_u8bit(*keys) ⇒ Object

Creates an 8-bit unsigned attribute reader and writer.



# File 'lib/ByteObject.rb', line 16