Class: Apiwork::Introspection::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/introspection/type.rb

Overview

Wraps custom type definitions.

Types can be objects (with shapes) or unions (with variants).

Examples:

Object type

api.types[:address].object? # => true
api.types[:address].shape[:city] # => Param for city field

Union type

api.types[:payment_method].union? # => true
api.types[:payment_method].variants # => [Param, ...]
api.types[:payment_method].discriminator # => :type

Instance Method Summary collapse

Constructor Details

#initialize(dump) ⇒ Type

Returns a new instance of Type.



19
20
21
# File 'lib/apiwork/introspection/type.rb', line 19

def initialize(dump)
  @dump = dump
end

Instance Method Details

#deprecated?Boolean

Whether this type is deprecated.

Returns:

  • (Boolean)


115
116
117
# File 'lib/apiwork/introspection/type.rb', line 115

def deprecated?
  @dump[:deprecated]
end

#descriptionString?

The description for this type.

Returns:

  • (String, nil)


75
76
77
# File 'lib/apiwork/introspection/type.rb', line 75

def description
  @dump[:description]
end

#discriminatorSymbol?

The discriminator for this type.

Returns:

  • (Symbol, nil)


67
68
69
# File 'lib/apiwork/introspection/type.rb', line 67

def discriminator
  @dump[:discriminator]
end

#exampleObject?

The example for this type.

Returns:



99
100
101
# File 'lib/apiwork/introspection/type.rb', line 99

def example
  @dump[:example]
end

#extendsArray<Symbol>

The extends for this type.

Returns:

  • (Array<Symbol>)


83
84
85
# File 'lib/apiwork/introspection/type.rb', line 83

def extends
  @dump[:extends]
end

#extends?Boolean

Whether this type extends other types.

Returns:

  • (Boolean)


91
92
93
# File 'lib/apiwork/introspection/type.rb', line 91

def extends?
  extends.any?
end

#object?Boolean

Whether this type is an object.

Returns:

  • (Boolean)


35
36
37
# File 'lib/apiwork/introspection/type.rb', line 35

def object?
  type == :object
end

#scopeString?

The scope for this type.

Returns:

  • (String, nil)


107
108
109
# File 'lib/apiwork/introspection/type.rb', line 107

def scope
  @dump[:scope]
end

#shapeHash{Symbol => Param}

The shape for this type.

Returns:

  • (Hash{Symbol => Param})


51
52
53
# File 'lib/apiwork/introspection/type.rb', line 51

def shape
  @shape ||= @dump[:shape].transform_values { |dump| Param.build(dump) }
end

#to_hHash

Converts this type to a hash.

Returns:

  • (Hash)


123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/apiwork/introspection/type.rb', line 123

def to_h
  {
    deprecated: deprecated?,
    description: description,
    discriminator: discriminator,
    example: example,
    extends: extends,
    scope: scope,
    shape: shape.transform_values(&:to_h),
    type: type,
    variants: variants.map(&:to_h),
  }
end

#typeSymbol?

The type for this type.

Returns:

  • (Symbol, nil)


27
28
29
# File 'lib/apiwork/introspection/type.rb', line 27

def type
  @dump[:type]
end

#union?Boolean

Whether this type is a union.

Returns:

  • (Boolean)


43
44
45
# File 'lib/apiwork/introspection/type.rb', line 43

def union?
  type == :union
end

#variantsArray<Param>

The variants for this type.

Returns:



59
60
61
# File 'lib/apiwork/introspection/type.rb', line 59

def variants
  @variants ||= @dump[:variants].map { |variant| Param.build(variant) }
end