Class: Cosmos::ProcessorConversion

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

Overview

Retrieves the result from an item processor

Instance Attribute Summary

Attributes inherited from Conversion

#converted_array_size, #converted_bit_size, #converted_type

Instance Method Summary collapse

Constructor Details

#initialize(processor_name, result_name, converted_type = nil, converted_bit_size = nil) ⇒ ProcessorConversion

Returns a new instance of ProcessorConversion.

Parameters:

  • processor_name (String)

    The name of the associated processor

  • result_name (String)

    The name of the associated result in the processor

  • converted_type (String or nil) (defaults to: nil)

    The datatype of the result of the processor

  • converted_bit_size (Integer or nil) (defaults to: nil)

    The bit size of the result of the processor



29
30
31
32
33
34
35
36
37
38
# File 'lib/cosmos/conversions/processor_conversion.rb', line 29

def initialize(processor_name, result_name, converted_type = nil, converted_bit_size = nil)
  super()
  @processor_name = processor_name.to_s.upcase
  @result_name = result_name.to_s.upcase.intern
  if ConfigParser.handle_nil(converted_type)
    @converted_type = converted_type.to_s.upcase.intern
    raise ArgumentError, "Unknown converted type: #{converted_type}" if !BinaryAccessor::DATA_TYPES.include?(@converted_type)
  end
  @converted_bit_size = Integer(converted_bit_size) if ConfigParser.handle_nil(converted_bit_size)
end

Instance Method Details

#as_jsonObject



61
62
63
# File 'lib/cosmos/conversions/processor_conversion.rb', line 61

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

#call(value, packet, buffer) ⇒ Varies

Returns The result of the associated processor.

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:

  • (Varies)

    The result of the associated processor



42
43
44
# File 'lib/cosmos/conversions/processor_conversion.rb', line 42

def call(value, packet, buffer)
  packet.processors[@processor_name].results[@result_name] || 0 # Never return nil
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



53
54
55
56
57
58
59
# File 'lib/cosmos/conversions/processor_conversion.rb', line 53

def to_config(read_or_write)
  config = "    #{read_or_write}_CONVERSION #{self.class.name.class_name_to_filename} #{@processor_name} #{@result_name}"
  config << " #{@converted_type}" if @converted_type
  config << " #{@converted_bit_size}" if @converted_bit_size
  config << "\n"
  config
end

#to_sString

Returns The type of processor.

Returns:

  • (String)

    The type of processor



47
48
49
# File 'lib/cosmos/conversions/processor_conversion.rb', line 47

def to_s
  "ProcessorConversion #{@processor_name} #{@result_name}"
end