Module: Surrealist::ValueAssigner

Defined in:
lib/surrealist/value_assigner.rb

Overview

A class that determines the correct value to return for serialization. May descend recursively.

Class Method Summary collapse

Class Method Details

.assign(schema, instance) ⇒ Hash

Assigns value returned from a method to a corresponding key in the schema hash.

Parameters:

  • instance (Object)

    the instance of the object which methods from the schema are called on.

  • schema (Struct)

    containing a single schema key and value

Returns:

  • (Hash)

    schema


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/surrealist/value_assigner.rb', line 13

def assign(schema, instance)
  value = raw_value(instance, schema)

  # set to track and prevent infinite self references in surrealization
  @skip_set ||= Set.new

  if value.respond_to?(:build_schema)
    yield assign_nested_record(instance, value)
  elsif Helper.collection?(value) && !value.empty? && value.all? { |v| Helper.surrealist?(v.class) }
    yield assign_nested_collection(instance, value)
  else
    yield value
  end
end