Class: Apia::Definitions::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/apia/definitions/type.rb

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Type

Returns a new instance of Type.



7
8
9
# File 'lib/apia/definitions/type.rb', line 7

def initialize(type)
  @type = type
end

Instance Method Details

#argument_set?Boolean

Does this field return an argument set?

Returns:

  • (Boolean)


75
76
77
# File 'lib/apia/definitions/type.rb', line 75

def argument_set?
  klass&.ancestors&.include?(Apia::ArgumentSet)
end

#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

#enum?Boolean

Does this field return an enum?

Returns:

  • (Boolean)


89
90
91
# File 'lib/apia/definitions/type.rb', line 89

def enum?
  klass&.ancestors&.include?(Apia::Enum)
end

#idObject



11
12
13
# File 'lib/apia/definitions/type.rb', line 11

def id
  klass&.definition&.id
end

#klassObject



15
16
17
18
19
20
21
# File 'lib/apia/definitions/type.rb', line 15

def klass
  if @type.is_a?(Symbol) || @type.is_a?(String)
    Scalars.fetch(@type.to_sym)
  else
    @type
  end
end

#object?Boolean

Does this field return an object?

Returns:

  • (Boolean)


96
97
98
# File 'lib/apia/definitions/type.rb', line 96

def object?
  klass&.ancestors&.include?(Apia::Object)
end

#polymorph?Boolean

Does this field return a polymorph?

Returns:

  • (Boolean)


103
104
105
# File 'lib/apia/definitions/type.rb', line 103

def polymorph?
  klass&.ancestors&.include?(Apia::Polymorph)
end

#scalar?Boolean

Does this field return a scalar?

Returns:

  • (Boolean)


82
83
84
# File 'lib/apia/definitions/type.rb', line 82

def scalar?
  klass&.ancestors&.include?(Apia::Scalar)
end

#usable_for_argument?Boolean

Can this type be used for an argument?

Returns:

  • (Boolean)


33
34
35
# File 'lib/apia/definitions/type.rb', line 33

def usable_for_argument?
  scalar? || enum? || argument_set?
end

#usable_for_field?Boolean

Can this type actually be used?

Returns:

  • (Boolean)


26
27
28
# File 'lib/apia/definitions/type.rb', line 26

def usable_for_field?
  scalar? || object? || enum? || polymorph?
end