Module: ActiveGraph::Shared::TypeConverters

Included in:
DeclaredProperties, TypeConverters
Defined in:
lib/active_graph/shared/type_converters.rb

Defined Under Namespace

Classes: BaseConverter, BigDecimalConverter, Boolean, BooleanConverter, DateConverter, DateTimeConverter, DurationConverter, EnumConverter, FloatConverter, IntegerConverter, JSONConverter, ObjectConverter, StringConverter, TimeConverter, YAMLConverter

Constant Summary collapse

CONVERTERS =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.converter_for(type) ⇒ Object



458
459
460
# File 'lib/active_graph/shared/type_converters.rb', line 458

def converter_for(type)
  type.respond_to?(:db_type) ? type : CONVERTERS[type]
end

.formatted_for_db?(found_converter, value) ⇒ Boolean

Attempts to determine whether conversion should be skipped because the object is already of the anticipated output type.

Parameters:

  • found_converter (#convert_type)

    An object that responds to #convert_type, hinting that it is a type converter.

  • value

    The value for conversion.

Returns:



465
466
467
468
# File 'lib/active_graph/shared/type_converters.rb', line 465

def formatted_for_db?(found_converter, value)
  return false unless found_converter.respond_to?(:db_type)
  found_converter.respond_to?(:converted?) ? found_converter.converted?(value) : value.is_a?(found_converter.db_type)
end

.included(_) ⇒ Object



431
432
433
434
435
436
# File 'lib/active_graph/shared/type_converters.rb', line 431

def included(_)
  ActiveGraph::Shared::TypeConverters.constants.each do |constant_name|
    constant = ActiveGraph::Shared::TypeConverters.const_get(constant_name)
    register_converter(constant, force: false) if constant.respond_to?(:convert_type)
  end
end

.register_converter(converter, force: true) ⇒ Object



470
471
472
473
# File 'lib/active_graph/shared/type_converters.rb', line 470

def register_converter(converter, force: true)
  return if CONVERTERS.key?(converter.convert_type) && !force
  CONVERTERS[converter.convert_type] = converter
end

.to_other(direction, value, type) ⇒ Object

Parameters:

  • direction (Symbol)

    either :to_ruby or :to_other



450
451
452
453
454
455
456
# File 'lib/active_graph/shared/type_converters.rb', line 450

def to_other(direction, value, type)
  fail "Unknown direction given: #{direction}" unless direction == :to_ruby || direction == :to_db
  found_converter = converter_for(type)
  return value unless found_converter
  return value if direction == :to_db && formatted_for_db?(found_converter, value)
  found_converter.send(direction, value)
end

.typecast_attribute(typecaster, value) ⇒ Object



438
439
440
441
442
# File 'lib/active_graph/shared/type_converters.rb', line 438

def typecast_attribute(typecaster, value)
  fail ArgumentError, "A typecaster must be given, #{typecaster} is invalid" unless typecaster.respond_to?(:to_ruby)
  return value if value.nil?
  typecaster.to_ruby(value)
end

.typecaster_for(primitive_type) ⇒ Object



444
445
446
447
# File 'lib/active_graph/shared/type_converters.rb', line 444

def typecaster_for(primitive_type)
  return nil if primitive_type.nil?
  CONVERTERS[primitive_type]
end

Instance Method Details

#convert_properties_to(obj, medium, properties) ⇒ Object

Modifies a hash's values to be of types acceptable to Neo4j or matching what the user defined using type in property definitions.

Parameters:

  • obj (ActiveGraph::Shared::Property)

    A node or rel that mixes in the Property module

  • medium (Symbol)

    Indicates the type of conversion to perform.

  • properties (Hash)

    A hash of symbol-keyed properties for conversion.



371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/active_graph/shared/type_converters.rb', line 371

def convert_properties_to(obj, medium, properties)
  direction = medium == :ruby ? :to_ruby : :to_db
  properties.to_h.each_pair do |key, value|
    next if skip_conversion?(obj, key, value)

    converted_value = convert_property(key, value, direction)
    if properties.is_a?(ActiveGraph::AttributeSet)
      properties.write_cast_value(key, converted_value)
    else
      properties[key] = converted_value
    end
  end
end

#convert_property(key, value, direction) ⇒ Object

Converts a single property from its current format to its db- or Ruby-expected output type.

Parameters:

  • key (Symbol)

    A property declared on the model

  • value

    The value intended for conversion

  • direction (Symbol)

    Either :to_ruby or :to_db, indicates the type of conversion to perform



389
390
391
# File 'lib/active_graph/shared/type_converters.rb', line 389

def convert_property(key, value, direction)
  converted_property(primitive_type(key.to_sym), value, direction)
end

#supports_array?(key) ⇒ Boolean

Returns:



393
394
395
396
# File 'lib/active_graph/shared/type_converters.rb', line 393

def supports_array?(key)
  type = primitive_type(key.to_sym)
  type.respond_to?(:supports_array?) && type.supports_array?
end

#typecast_attribute(typecaster, value) ⇒ Object



402
403
404
# File 'lib/active_graph/shared/type_converters.rb', line 402

def typecast_attribute(typecaster, value)
  ActiveGraph::Shared::TypeConverters.typecast_attribute(typecaster, value)
end

#typecaster_for(value) ⇒ Object



398
399
400
# File 'lib/active_graph/shared/type_converters.rb', line 398

def typecaster_for(value)
  ActiveGraph::Shared::TypeConverters.typecaster_for(value)
end