Class: Cosmos::GenericConversion
- Inherits:
-
Conversion
- Object
- Conversion
- Cosmos::GenericConversion
- Defined in:
- lib/cosmos/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_bit_size, #converted_type
Instance Method Summary collapse
-
#call(value, packet, buffer) ⇒ Object
Perform the conversion on the value.
-
#initialize(code_to_eval, converted_type = nil, converted_bit_size = nil) ⇒ GenericConversion
constructor
A new instance of GenericConversion.
-
#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) ⇒ GenericConversion
Returns a new instance of GenericConversion.
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/cosmos/conversions/generic_conversion.rb', line 29 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_eval ⇒ String
Returns The Ruby code to evaluate which should return the converted value.
21 22 23 |
# File 'lib/cosmos/conversions/generic_conversion.rb', line 21 def code_to_eval @code_to_eval end |
Instance Method Details
#call(value, packet, buffer) ⇒ Object
Perform the conversion on the value.
41 42 43 44 45 46 |
# File 'lib/cosmos/conversions/generic_conversion.rb', line 41 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_s ⇒ String
Returns The conversion class followed by the code to evaluate.
49 50 51 |
# File 'lib/cosmos/conversions/generic_conversion.rb', line 49 def to_s "#{@code_to_eval}" end |