Class: OpenC3::GenericConversion
- Inherits:
-
Conversion
- Object
- Conversion
- OpenC3::GenericConversion
- Defined in:
- lib/openc3/conversions/generic_conversion.rb
Overview
Performs a generic conversion by evaluating Ruby code
Instance Attribute Summary collapse
-
#code_to_eval ⇒ String
The Ruby code to evaluate which should return the converted value.
Attributes inherited from Conversion
#converted_array_size, #converted_bit_size, #converted_type
Instance Method Summary collapse
- #as_json(*a) ⇒ Object
-
#call(value, packet, buffer) ⇒ Object
Perform the conversion on the value.
-
#initialize(code_to_eval, converted_type = nil, converted_bit_size = nil, converted_array_size = nil) ⇒ GenericConversion
constructor
A new instance of GenericConversion.
-
#to_config(read_or_write) ⇒ String
Config fragment for this conversion.
-
#to_s ⇒ String
The conversion class followed by the code to evaluate.
Constructor Details
#initialize(code_to_eval, converted_type = nil, converted_bit_size = nil, converted_array_size = nil) ⇒ GenericConversion
Returns a new instance of GenericConversion.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/openc3/conversions/generic_conversion.rb', line 41 def initialize(code_to_eval, converted_type = nil, converted_bit_size = nil, converted_array_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) @converted_array_size = Integer(converted_array_size) if ConfigParser.handle_nil(converted_array_size) end |
Instance Attribute Details
#code_to_eval ⇒ String
Returns The Ruby code to evaluate which should return the converted value.
31 32 33 |
# File 'lib/openc3/conversions/generic_conversion.rb', line 31 def code_to_eval @code_to_eval end |
Instance Method Details
#as_json(*a) ⇒ Object
80 81 82 83 84 |
# File 'lib/openc3/conversions/generic_conversion.rb', line 80 def as_json(*a) result = super(*a) result['params'] = [@code_to_eval, @converted_type, @converted_bit_size, @converted_array_size] result end |
#call(value, packet, buffer) ⇒ Object
Perform the conversion on the value.
55 56 57 58 59 60 |
# File 'lib/openc3/conversions/generic_conversion.rb', line 55 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.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/openc3/conversions/generic_conversion.rb', line 69 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 << " #{@converted_array_size}" if @converted_array_size config << "\n" config << @code_to_eval config << " GENERIC_#{read_or_write}_CONVERSION_END\n" config end |
#to_s ⇒ String
Returns The conversion class followed by the code to evaluate.
63 64 65 |
# File 'lib/openc3/conversions/generic_conversion.rb', line 63 def to_s "#{@code_to_eval}" end |