Class: BinaryCodec::ComparableSerializedType

Inherits:
SerializedType show all
Defined in:
lib/binary-codec/types/serialized_type.rb

Direct Known Subclasses

Hash, Uint

Instance Attribute Summary

Attributes inherited from SerializedType

#bytes

Instance Method Summary collapse

Methods inherited from SerializedType

from, from_bytes, from_hex, from_json, from_parser, get_type_by_name, #initialize, #to_byte_sink, #to_bytes, #to_hex, #to_json, #to_s

Constructor Details

This class inherits a constructor from BinaryCodec::SerializedType

Instance Method Details

#compare_to(other) ⇒ Integer

Overload this method in subclasses to define comparison logic

Parameters:

  • other (Object)
    • The object to compare ‘self` to

Returns:

  • (Integer)
    • Returns -1, 0, or 1 depending on the comparison

Raises:

  • (NotImplementedError)


128
129
130
# File 'lib/binary-codec/types/serialized_type.rb', line 128

def compare_to(other)
  raise NotImplementedError, "Cannot compare #{self} and #{other}"
end

#eq(other) ⇒ Object

Compare if ‘self` is equal to `other`



105
106
107
# File 'lib/binary-codec/types/serialized_type.rb', line 105

def eq(other)
  compare_to(other) == 0
end

#gt(other) ⇒ Object

Compare if ‘self` is greater than `other`



110
111
112
# File 'lib/binary-codec/types/serialized_type.rb', line 110

def gt(other)
  compare_to(other) > 0
end

#gte(other) ⇒ Object

Compare if ‘self` is greater than or equal to `other`



115
116
117
# File 'lib/binary-codec/types/serialized_type.rb', line 115

def gte(other)
  compare_to(other) >= 0
end

#lt(other) ⇒ Object

Compare if ‘self` is less than `other`



100
101
102
# File 'lib/binary-codec/types/serialized_type.rb', line 100

def lt(other)
  compare_to(other) < 0
end

#lte(other) ⇒ Object

Compare if ‘self` is less than or equal to `other`



120
121
122
# File 'lib/binary-codec/types/serialized_type.rb', line 120

def lte(other)
  compare_to(other) <= 0
end