Responsible for converting values from and to Java Neo4j and Lucene. You can implement your own
converter by implementing the method convert?
to_java and to_ruby in this module.
There are currently three default converters that are triggered when a Time, Date or a DateTime is read or written if there is a type declared for the property.
Example of writing your own marshalling converter:
class Foo include Neo4j::NodeMixin property :thing, :type => MyType end module Neo4j::TypeConverters class MyTypeConverter class << self def convert?(type) type == MyType end def to_java(val) "silly:#{val}" end def to_ruby(val) val.sub(/silly:/, '') end end end end
Converts the given value to a Java type by using the registered converters. It just looks at the class of the given value unless an attribute name is given. It will convert it if there is a converter registered (in Neo4j::Config) for this value.
# File lib/neo4j/type_converters/type_converters.rb, line 246 def convert(value, attribute = nil, klass = nil, enforce_type = true) converter(attribute_type(value, attribute, klass), enforce_type).to_java(value) end
Always returns a converter that handles ::to_ruby or ::to_java if
enforce_type is set to false then it will raise in case of
unknown type otherwise it will return the DevaultConverter.
# File lib/neo4j/type_converters/type_converters.rb, line 229 def converter(type = nil, enforce_type = true) return DefaultConverter unless type @converters ||= begin Neo4j::TypeConverters.constants.find_all do |c| Neo4j::TypeConverters.const_get(c).respond_to?(:convert?) end.map do |c| Neo4j::TypeConverters.const_get(c) end end found = @converters.find {|c| c.convert?(type) } raise "The type #{type.inspect} is unknown. Use one of #{@converters.map{|c| c.name.demodulize.sub('Converter','') }.join(", ")} or create a custom type converter." if !found && enforce_type found or DefaultConverter end
Mostly for testing purpose, You can use this method in order to add a converter while the neo4j has already started.
# File lib/neo4j/type_converters/type_converters.rb, line 222 def converters=(converters) @converters = converters end
Converts the given property (key, value) to Java if there is a type converter for given type. The type must also be declared using Neo4j::NodeMixin#property property_name, :type => clazz If no Converter is defined for this value then it simply returns the given value.
# File lib/neo4j/type_converters/type_converters.rb, line 253 def to_java(clazz, key, value) type = clazz._decl_props[key.to_sym] && clazz._decl_props[key.to_sym][:type] converter(type).to_java(value) end
Converts the given property (key, value) to Ruby if there is a type converter for given type. If no Converter is defined for this value then it simply returns the given value.
# File lib/neo4j/type_converters/type_converters.rb, line 260 def to_ruby(clazz, key, value) type = clazz._decl_props[key.to_sym] && clazz._decl_props[key.to_sym][:type] converter(type).to_ruby(value) end