module Neo4j::Shared::TypeConverters
Constants
- CONVERTERS
Private Class Methods
# File lib/neo4j/shared/type_converters.rb 416 def converter_for(type) 417 type.respond_to?(:db_type) ? type : CONVERTERS[type] 418 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/neo4j/shared/type_converters.rb 423 def formatted_for_db?(found_converter, value) 424 return false unless found_converter.respond_to?(:db_type) 425 found_converter.respond_to?(:converted?) ? found_converter.converted?(value) : value.is_a?(found_converter.db_type) 426 end
# File lib/neo4j/shared/type_converters.rb 389 def included(_) 390 Neo4j::Shared::TypeConverters.constants.each do |constant_name| 391 constant = Neo4j::Shared::TypeConverters.const_get(constant_name) 392 register_converter(constant) if constant.respond_to?(:convert_type) 393 end 394 end
# File lib/neo4j/shared/type_converters.rb 428 def register_converter(converter) 429 CONVERTERS[converter.convert_type] = converter 430 end
@param [Symbol] direction either :to_ruby or :to_other
# File lib/neo4j/shared/type_converters.rb 408 def to_other(direction, value, type) 409 fail "Unknown direction given: #{direction}" unless direction == :to_ruby || direction == :to_db 410 found_converter = converter_for(type) 411 return value unless found_converter 412 return value if direction == :to_db && formatted_for_db?(found_converter, value) 413 found_converter.send(direction, value) 414 end
# File lib/neo4j/shared/type_converters.rb 396 def typecast_attribute(typecaster, value) 397 fail ArgumentError, "A typecaster must be given, #{typecaster} is invalid" unless typecaster.respond_to?(:to_ruby) 398 return value if value.nil? 399 typecaster.to_ruby(value) 400 end
# File lib/neo4j/shared/type_converters.rb 402 def typecaster_for(primitive_type) 403 return nil if primitive_type.nil? 404 CONVERTERS[primitive_type] 405 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 [Neo4j::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/neo4j/shared/type_converters.rb 335 def convert_properties_to(obj, medium, properties) 336 direction = medium == :ruby ? :to_ruby : :to_db 337 properties.each_pair do |key, value| 338 next if skip_conversion?(obj, key, value) 339 properties[key] = convert_property(key, value, direction) 340 end 341 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/neo4j/shared/type_converters.rb 347 def convert_property(key, value, direction) 348 converted_property(primitive_type(key.to_sym), value, direction) 349 end
# File lib/neo4j/shared/type_converters.rb 351 def supports_array?(key) 352 type = primitive_type(key.to_sym) 353 type.respond_to?(:supports_array?) && type.supports_array? 354 end
# File lib/neo4j/shared/type_converters.rb 360 def typecast_attribute(typecaster, value) 361 Neo4j::Shared::TypeConverters.typecast_attribute(typecaster, value) 362 end
# File lib/neo4j/shared/type_converters.rb 356 def typecaster_for(value) 357 Neo4j::Shared::TypeConverters.typecaster_for(value) 358 end
Private Instance Methods
# File lib/neo4j/shared/type_converters.rb 366 def converted_property(type, value, direction) 367 return nil if value.nil? 368 CONVERTERS[type] || type.respond_to?(:db_type) ? TypeConverters.to_other(direction, value, type) : value 369 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/neo4j/shared/type_converters.rb 372 def primitive_type(attr) 373 case 374 when serialized_properties.include?(attr) 375 serialized_properties[attr] 376 when magic_typecast_properties.include?(attr) 377 magic_typecast_properties[attr] 378 else 379 fetch_upstream_primitive(attr) 380 end 381 end
Returns true if the property isn't defined in the model or if it is nil
# File lib/neo4j/shared/type_converters.rb 384 def skip_conversion?(obj, attr, value) 385 value.nil? || !obj.class.attributes.key?(attr) 386 end