Sha256: c35b5b4f0626db5b72271f1a5c9ecec6342b00f4285de7095f3769fcef72f4e2

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

# This file is part of PacketGen
# See https://github.com/lemontree55/packetgen for more informations
# Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
# Copyright (C) 2024 LemonTree55 <lenontree@proton.me>
# This program is published under MIT license.

module PacketGen
  module Types
    # Mixin to define minimal API for a class to be embbeded as a field in
    # {Fields} type.
    #
    # == Optional methods
    # These methods may, optionally, be defined by fieldable types:
    # * +from_human+ to load data from a human-readable string.
    # @author Sylvain Daubert
    # @since 3.1.6
    module Fieldable
      # Get type name
      # @return [String]
      def type_name
        self.class.to_s.split('::').last
      end

      # rubocop:disable Lint/UselessMethodDefinition
      # These methods are defined for documentation.

      # Populate object from a binary string
      # @param [String] str
      # @return [Fields] self
      # @abstract subclass should overload it.
      def read(str)
        super
      end

      # Return object as a binary string
      # @return [String]
      # @abstract subclass should overload it.
      def to_s
        super
      end

      # Size of object as binary string
      # @return [Integer]
      def sz
        to_s.size
      end

      # Return a human-readbale string
      # @return [String]
      # @abstract subclass should overload it.
      def to_human
        super
      end

      # rubocop:enable Lint/UselessMethodDefinition

      # Format object when inspecting a {Fields} object
      # @return [String]
      def format_inspect
        to_human
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
packetgen-3.3.3 lib/packetgen/types/fieldable.rb
packetgen-3.3.2 lib/packetgen/types/fieldable.rb