Method: Apia::Definitions::Type#cast

Defined in:
lib/apia/definitions/type.rb

#cast(value, request: nil, path: []) ⇒ Object?

Cast the given value into an response that can be sent to the consumer.

Parameters:

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/apia/definitions/type.rb', line 43

def cast(value, request: nil, path: [])
  return nil if value.nil?

  if scalar? || enum?
    # If this is a scalar or an enum, we're going to just return the
    # value that they return. There's nothing complicated about them
    # and they return scalars.
    klass.cast(value)

  elsif object?
    # If this field returns an object, we'll go ahead and generate
    # the hash for the object at this point.
    object = klass.new(value)

    # If this item shouldn't be included. we'll return :skip which
    # will instruct the field set not to include it at all.
    return :skip unless object.include?(request)

    # Otherwise, we'll return the hash
    object.hash(request: request, path: path)

  elsif polymorph?
    # If the type is a polymorph and this value
    option = klass.option_for_value(value)
    option.cast(value, request: request, path: path)

  end
end