module Neo4j::Shared::TypeConverters

Constants

CONVERTERS

Private Class Methods

converter_for(type) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
401 def converter_for(type)
402   type.respond_to?(:db_type) ? type : CONVERTERS[type]
403 end
formatted_for_db?(found_converter, value) click to toggle source

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
408 def formatted_for_db?(found_converter, value)
409   return false unless found_converter.respond_to?(:db_type)
410   found_converter.respond_to?(:converted) ? found_converter.converted?(value) : value.is_a?(found_converter.db_type)
411 end
included(_) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
374 def included(_)
375   Neo4j::Shared::TypeConverters.constants.each do |constant_name|
376     constant = Neo4j::Shared::TypeConverters.const_get(constant_name)
377     register_converter(constant) if constant.respond_to?(:convert_type)
378   end
379 end
register_converter(converter) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
413 def register_converter(converter)
414   CONVERTERS[converter.convert_type] = converter
415 end
to_other(direction, value, type) click to toggle source

@param [Symbol] direction either :to_ruby or :to_other

    # File lib/neo4j/shared/type_converters.rb
393 def to_other(direction, value, type)
394   fail "Unknown direction given: #{direction}" unless direction == :to_ruby || direction == :to_db
395   found_converter = converter_for(type)
396   return value unless found_converter
397   return value if direction == :to_db && formatted_for_db?(found_converter, value)
398   found_converter.send(direction, value)
399 end
typecast_attribute(typecaster, value) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
381 def typecast_attribute(typecaster, value)
382   fail ArgumentError, "A typecaster must be given, #{typecaster} is invalid" unless typecaster.respond_to?(:to_ruby)
383   return value if value.nil?
384   typecaster.to_ruby(value)
385 end
typecaster_for(primitive_type) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
387 def typecaster_for(primitive_type)
388   return nil if primitive_type.nil?
389   CONVERTERS[primitive_type]
390 end

Public Instance Methods

convert_properties_to(obj, medium, properties) click to toggle source

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
320 def convert_properties_to(obj, medium, properties)
321   direction = medium == :ruby ? :to_ruby : :to_db
322   properties.each_pair do |key, value|
323     next if skip_conversion?(obj, key, value)
324     properties[key] = convert_property(key, value, direction)
325   end
326 end
convert_property(key, value, direction) click to toggle source

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
332 def convert_property(key, value, direction)
333   converted_property(primitive_type(key.to_sym), value, direction)
334 end
supports_array?(key) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
336 def supports_array?(key)
337   type = primitive_type(key.to_sym)
338   type.respond_to?(:supports_array?) && type.supports_array?
339 end
typecast_attribute(typecaster, value) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
345 def typecast_attribute(typecaster, value)
346   Neo4j::Shared::TypeConverters.typecast_attribute(typecaster, value)
347 end
typecaster_for(value) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
341 def typecaster_for(value)
342   Neo4j::Shared::TypeConverters.typecaster_for(value)
343 end

Private Instance Methods

converted_property(type, value, direction) click to toggle source
    # File lib/neo4j/shared/type_converters.rb
351 def converted_property(type, value, direction)
352   return nil if value.nil?
353   type.respond_to?(:db_type) || TypeConverters::CONVERTERS[type] ? TypeConverters.to_other(direction, value, type) : value
354 end
primitive_type(attr) click to toggle source

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
357 def primitive_type(attr)
358   case
359   when self.serialized_properties_keys.include?(attr)
360     serialized_properties[attr]
361   when self.magic_typecast_properties_keys.include?(attr)
362     self.magic_typecast_properties[attr]
363   else
364     self.fetch_upstream_primitive(attr)
365   end
366 end
skip_conversion?(obj, attr, value) click to toggle source

Returns true if the property isn’t defined in the model or if it is nil

    # File lib/neo4j/shared/type_converters.rb
369 def skip_conversion?(obj, attr, value)
370   !obj.class.attributes[attr] || value.nil?
371 end