Class: Flic::Protocol::Primitives::Enum

Inherits:
BinData::Primitive
  • Object
show all
Defined in:
lib/flic/protocol/primitives/enum.rb

Overview

An abstract class for 1 byte enums

Defined Under Namespace

Classes: Error, InvalidOctetError, InvalidOptionError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.max_octetInteger

Returns the byte value for the option with the largest byte value.

Returns:

  • (Integer)

    the byte value for the option with the largest byte value



36
37
38
# File 'lib/flic/protocol/primitives/enum.rb', line 36

def max_octet
  octets.max
end

.next_available_octetInteger

Returns the next available byte value (starting at 0x00).

Returns:

  • (Integer)

    the next available byte value (starting at 0x00)



41
42
43
44
45
46
47
# File 'lib/flic/protocol/primitives/enum.rb', line 41

def next_available_octet
  if max_octet
    1 + max_octet
  else
    0
  end
end

.octet_optionHash

Returns a map of byte values to options.

Returns:

  • (Hash)

    a map of byte values to options



21
22
23
# File 'lib/flic/protocol/primitives/enum.rb', line 21

def octet_option
  @octet_option ||= {}
end

.octetsArray

Returns the valid byte values for this enum.

Returns:

  • (Array)

    the valid byte values for this enum



31
32
33
# File 'lib/flic/protocol/primitives/enum.rb', line 31

def octets
  octet_option.keys
end

.option_octetHash

Returns a map of options to byte values.

Returns:

  • (Hash)

    a map of options to byte values



16
17
18
# File 'lib/flic/protocol/primitives/enum.rb', line 16

def option_octet
  @option_octet ||= {}
end

.optionsArray

Returns the valid options for this enum.

Returns:

  • (Array)

    the valid options for this enum



26
27
28
# File 'lib/flic/protocol/primitives/enum.rb', line 26

def options
  option_octet.keys
end

Instance Method Details

#getObject



68
69
70
71
72
73
74
# File 'lib/flic/protocol/primitives/enum.rb', line 68

def get
  if octet_option.has_key?(octet)
    octet_option[octet]
  else
    raise InvalidOctetError, "No such octet `#{octet.inspect}` for enum #{inspect}"
  end
end

#set(option) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/flic/protocol/primitives/enum.rb', line 76

def set(option)
  if option_octet.has_key?(option)
    self.octet = self.class.option_octet[option]
  else
    raise InvalidOptionError, "No such option `#{option.inspect}` for enum #{inspect}"
  end
end