Class: Cosmos::GenericConversion

Inherits:
Conversion show all
Defined in:
lib/cosmos/conversions/generic_conversion.rb

Overview

Performs a generic conversion by evaluating Ruby code

Instance Attribute Summary collapse

Attributes inherited from Conversion

#converted_array_size, #converted_bit_size, #converted_type

Instance Method Summary collapse

Constructor Details

#initialize(code_to_eval, converted_type = nil, converted_bit_size = nil) ⇒ GenericConversion

Returns a new instance of GenericConversion.

Parameters:

  • code_to_eval (String)

    The Ruby code to evaluate which should return the converted value

  • converted_type (Symbol) (defaults to: nil)

    The converted data type. Must be one of BinaryAccessor::DATA_TYPES

  • converted_bit_size (Integer) (defaults to: nil)

    The size in bits of the converted value



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cosmos/conversions/generic_conversion.rb', line 36

def initialize(code_to_eval, converted_type = nil, converted_bit_size = nil)
  super()
  @code_to_eval = code_to_eval
  if ConfigParser.handle_nil(converted_type)
    converted_type = converted_type.to_s.upcase.intern
    raise "Invalid type #{converted_type}" unless BinaryAccessor::DATA_TYPES.include?(converted_type)

    @converted_type = converted_type
  end
  @converted_bit_size = Integer(converted_bit_size) if ConfigParser.handle_nil(converted_bit_size)
end

Instance Attribute Details

#code_to_evalString

Returns The Ruby code to evaluate which should return the converted value.

Returns:

  • (String)

    The Ruby code to evaluate which should return the converted value



28
29
30
# File 'lib/cosmos/conversions/generic_conversion.rb', line 28

def code_to_eval
  @code_to_eval
end

Instance Method Details

#as_jsonObject



73
74
75
# File 'lib/cosmos/conversions/generic_conversion.rb', line 73

def as_json
  { 'class' => self.class.name.to_s, 'params' => [@code_to_eval, @converted_type, @converted_bit_size] }
end

#call(value, packet, buffer) ⇒ Object

Perform the conversion on the value.

Parameters:

  • value (Object)

    The value to convert

  • packet (Packet)

    The packet which contains the value. This can be useful to reach into the packet and use other values in the conversion.

  • buffer (String)

    The packet buffer

Returns:

  • The converted value



49
50
51
52
53
54
# File 'lib/cosmos/conversions/generic_conversion.rb', line 49

def call(value, packet, buffer)
  myself = packet # For backwards compatibility
  if true or myself # Remove unused variable warning for myself
    return eval(@code_to_eval)
  end
end

#to_config(read_or_write) ⇒ String

Returns Config fragment for this conversion.

Parameters:

  • read_or_write (String)

    Either 'READ' or 'WRITE'

Returns:

  • (String)

    Config fragment for this conversion



63
64
65
66
67
68
69
70
71
# File 'lib/cosmos/conversions/generic_conversion.rb', line 63

def to_config(read_or_write)
  config = "    GENERIC_#{read_or_write}_CONVERSION_START"
  config << " #{@converted_type}" if @converted_type
  config << " #{@converted_bit_size}" if @converted_bit_size
  config << "\n"
  config << @code_to_eval
  config << "    GENERIC_#{read_or_write}_CONVERSION_END\n"
  config
end

#to_sString

Returns The conversion class followed by the code to evaluate.

Returns:

  • (String)

    The conversion class followed by the code to evaluate



57
58
59
# File 'lib/cosmos/conversions/generic_conversion.rb', line 57

def to_s
  "#{@code_to_eval}"
end