Module: BinStruct::Structable

Included in:
AbstractTLV, Array, CString, Int, IntString, OUI, String
Defined in:
lib/bin_struct/structable.rb

Overview

Mixin to define minimal API for a class to be embbeded as an attribute in Struct object.

Optional methods

This method may, optionally, be defined by structable types:

  • from_human to load data from a human-readable ruby object (String, Integer,…).

Author:

  • Sylvain Daubert (2016-2024)

  • LemonTree55

Instance Method Summary collapse

Instance Method Details

#format_inspect::String

Format object when inspecting a BinStruct::Struct object

Returns:

  • (::String)


60
61
62
# File 'lib/bin_struct/structable.rb', line 60

def format_inspect
  to_human
end

#read(str) ⇒ self

This method is abstract.

subclass should overload it.

Populate object from a binary string

Parameters:

  • str (::String)

Returns:

  • (self)


32
33
34
# File 'lib/bin_struct/structable.rb', line 32

def read(str)
  super
end

#szInteger

Size of object as binary string

Returns:

  • (Integer)


45
46
47
# File 'lib/bin_struct/structable.rb', line 45

def sz
  to_s.size
end

#to_human::String, Integer

This method is abstract.

subclass should overload it.

Return a human-readbale object

Returns:

  • (::String, Integer)


52
53
54
# File 'lib/bin_struct/structable.rb', line 52

def to_human
  super
end

#to_s::String

This method is abstract.

subclass should overload it.

Return object as a binary string

Returns:

  • (::String)


39
40
41
# File 'lib/bin_struct/structable.rb', line 39

def to_s
  super
end

#type_name::String

Get type name

Returns:

  • (::String)


21
22
23
# File 'lib/bin_struct/structable.rb', line 21

def type_name
  self.class.to_s.split('::').last
end