lib/erp_integration/resource.rb in erp_integration-0.30.0 vs lib/erp_integration/resource.rb in erp_integration-0.31.0

- old
+ new

@@ -46,9 +46,28 @@ attributes.each_pair do |name, value| public_send(:"#{name}=", value) if respond_to?(:"#{name}=") end end + private + + # The `parse_value` method is used to convert serialized ERP objects + # into a Ruby object that can be assigned to the resource attribute. + # + # @param value [Object] The raw API response value. + # @return [Object] The converted value. + def parse_value(value) + value_class = value.is_a?(Hash) && value['__class__'] + return value unless value_class + + ruby_class = Object.const_get("ErpIntegration::Types::#{value_class}") + value_key = value.keys.find { |key| key == value_class.downcase } + + ruby_class.new(value[value_key]).object + rescue NameError + value + end + class << self # Dynamically defines and loads the adapter for the class inheriting from # the `ErpIntegration::Resource`. # @return [Class] The adapter class for the resource. def adapter