Module: Neo4j::TypeConverters

Defined in:
lib/neo4j/type_converters/type_converters.rb

Overview

Responsible for converting values from and to Java Neo4j and Lucene. You can implement your own converter by implementing the method convert?, index_as 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.

Examples:

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

      def index_as
        String
      end
    end
  end
end

Defined Under Namespace

Classes: BooleanConverter, DateConverter, DateTimeConverter, DefaultConverter, FixnumConverter, FloatConverter, StringConverter, SymbolConverter, TimeConverter

Class Method Summary collapse

Class Method Details

.convert(value, attribute = nil, klass = nil, enforce_type = true) ⇒ Object

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.



300
301
302
# File 'lib/neo4j/type_converters/type_converters.rb', line 300

def convert(value, attribute = nil, klass = nil, enforce_type = true)
  converter(attribute_type(value, attribute, klass), enforce_type).to_java(value)
end

.converter(type = nil, enforce_type = true) ⇒ Object

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 DefaultConverter.



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/neo4j/type_converters/type_converters.rb', line 284

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 }.join(", ")} or create a custom type converter." if !found && enforce_type
  found or DefaultConverter
end

.converters=(converters) ⇒ Object

Mostly for testing purpose, You can use this method in order to add a converter while the neo4j has already started.



277
278
279
# File 'lib/neo4j/type_converters/type_converters.rb', line 277

def converters=(converters)
  @converters = converters
end

.to_java(clazz, key, value) ⇒ Object

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.



307
308
309
310
# File 'lib/neo4j/type_converters/type_converters.rb', line 307

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

.to_ruby(clazz, key, value) ⇒ Object

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.



314
315
316
317
# File 'lib/neo4j/type_converters/type_converters.rb', line 314

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