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.
Defined Under Namespace
Classes: BooleanConverter, DateConverter, DateTimeConverter, DefaultConverter, FixnumConverter, FloatConverter, StringConverter, SymbolConverter, TimeConverter
Class Method Summary collapse
-
.convert(value, attribute = nil, klass = nil, enforce_type = true) ⇒ Object
Converts the given value to a Java type by using the registered converters.
-
.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. -
.converters=(converters) ⇒ Object
Mostly for testing purpose, You can use this method in order to add a converter while the neo4j has already started.
-
.to_java(clazz, key, value) ⇒ Object
Converts the given property (key, value) to Java if there is a type converter for given type.
-
.to_ruby(clazz, key, value) ⇒ Object
Converts the given property (key, value) to Ruby if there is a type converter for given type.
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.
298 299 300 |
# File 'lib/neo4j/type_converters/type_converters.rb', line 298 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.
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/neo4j/type_converters/type_converters.rb', line 282 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.
275 276 277 |
# File 'lib/neo4j/type_converters/type_converters.rb', line 275 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.
305 306 307 308 |
# File 'lib/neo4j/type_converters/type_converters.rb', line 305 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.
312 313 314 315 |
# File 'lib/neo4j/type_converters/type_converters.rb', line 312 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 |