module ActiveGraph::Shared::TypeConverters
Constants
- CONVERTERS
Private Class Methods
# File lib/active_graph/shared/type_converters.rb 422 def converter_for(type) 423 type.respond_to?(:db_type) ? type : CONVERTERS[type] 424 end
Attempts to determine whether conversion should be skipped because the object is already of the anticipated output type. @param [#convert_type] found_converter An object that responds to convert_type, hinting that it is a type converter. @param value The value for conversion.
# File lib/active_graph/shared/type_converters.rb 429 def formatted_for_db?(found_converter, value) 430 return false unless found_converter.respond_to?(:db_type) 431 found_converter.respond_to?(:converted?) ? found_converter.converted?(value) : value.is_a?(found_converter.db_type) 432 end
# File lib/active_graph/shared/type_converters.rb 395 def included(_) 396 ActiveGraph::Shared::TypeConverters.constants.each do |constant_name| 397 constant = ActiveGraph::Shared::TypeConverters.const_get(constant_name) 398 register_converter(constant) if constant.respond_to?(:convert_type) 399 end 400 end
# File lib/active_graph/shared/type_converters.rb 434 def register_converter(converter) 435 CONVERTERS[converter.convert_type] = converter 436 end
@param [Symbol] direction either :to_ruby or :to_other
# File lib/active_graph/shared/type_converters.rb 414 def to_other(direction, value, type) 415 fail "Unknown direction given: #{direction}" unless direction == :to_ruby || direction == :to_db 416 found_converter = converter_for(type) 417 return value unless found_converter 418 return value if direction == :to_db && formatted_for_db?(found_converter, value) 419 found_converter.send(direction, value) 420 end
# File lib/active_graph/shared/type_converters.rb 402 def typecast_attribute(typecaster, value) 403 fail ArgumentError, "A typecaster must be given, #{typecaster} is invalid" unless typecaster.respond_to?(:to_ruby) 404 return value if value.nil? 405 typecaster.to_ruby(value) 406 end
# File lib/active_graph/shared/type_converters.rb 408 def typecaster_for(primitive_type) 409 return nil if primitive_type.nil? 410 CONVERTERS[primitive_type] 411 end
Public Instance Methods
Modifies a hash's values to be of types acceptable to Neo4j or matching what the user defined using `type` in property definitions. @param [ActiveGraph::Shared::Property] obj A node or rel that mixes in the Property
module @param [Symbol] medium Indicates the type of conversion to perform. @param [Hash] properties A hash of symbol-keyed properties for conversion.
# File lib/active_graph/shared/type_converters.rb 335 def convert_properties_to(obj, medium, properties) 336 direction = medium == :ruby ? :to_ruby : :to_db 337 properties.to_h.each_pair do |key, value| 338 next if skip_conversion?(obj, key, value) 339 340 converted_value = convert_property(key, value, direction) 341 if properties.is_a?(ActiveGraph::AttributeSet) 342 properties.write_cast_value(key, converted_value) 343 else 344 properties[key] = converted_value 345 end 346 end 347 end
Converts a single property from its current format to its db- or Ruby-expected output type. @param [Symbol] key A property declared on the model @param value The value intended for conversion @param [Symbol] direction Either :to_ruby or :to_db, indicates the type of conversion to perform
# File lib/active_graph/shared/type_converters.rb 353 def convert_property(key, value, direction) 354 converted_property(primitive_type(key.to_sym), value, direction) 355 end
# File lib/active_graph/shared/type_converters.rb 357 def supports_array?(key) 358 type = primitive_type(key.to_sym) 359 type.respond_to?(:supports_array?) && type.supports_array? 360 end
# File lib/active_graph/shared/type_converters.rb 366 def typecast_attribute(typecaster, value) 367 ActiveGraph::Shared::TypeConverters.typecast_attribute(typecaster, value) 368 end
# File lib/active_graph/shared/type_converters.rb 362 def typecaster_for(value) 363 ActiveGraph::Shared::TypeConverters.typecaster_for(value) 364 end
Private Instance Methods
# File lib/active_graph/shared/type_converters.rb 372 def converted_property(type, value, direction) 373 return nil if value.nil? 374 CONVERTERS[type] || type.respond_to?(:db_type) ? TypeConverters.to_other(direction, value, type) : value 375 end
If the attribute is to be typecast using a custom converter, which converter should it use? If no, returns the type to find a native serializer.
# File lib/active_graph/shared/type_converters.rb 378 def primitive_type(attr) 379 case 380 when serialized_properties.include?(attr) 381 serialized_properties[attr] 382 when magic_typecast_properties.include?(attr) 383 magic_typecast_properties[attr] 384 else 385 fetch_upstream_primitive(attr) 386 end 387 end
Returns true if the property isn't defined in the model or if it is nil
# File lib/active_graph/shared/type_converters.rb 390 def skip_conversion?(obj, attr, value) 391 value.nil? || !obj.class.attributes.key?(attr) 392 end