Class: Modern::Descriptor::Core

Inherits:
Struct
  • Object
show all
Defined in:
lib/modern/descriptor/core.rb

Overview

The class that encapsulates all routes, along with their configuration and metadata. This class can recursively include itself to mount other instances inside of itself; this is used by App to generate OpenAPI documentation and design routing accordingly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Struct::Copy

#copy

Constructor Details

#initialize(fields) ⇒ Core

Returns a new instance of Core.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/modern/descriptor/core.rb', line 31

def initialize(fields)
  super

  securities = routes.map(&:security).flatten.uniq
  duplicate_names = securities.map(&:name).duplicates

  raise "Duplicate but not identical securities by names: #{duplicate_names.join(', ')}" \
    unless duplicate_names.empty?

  @securities_by_name = securities.map { |s| [s.name, s] }.to_h.freeze

  # This could be a set, but I like being able to just pull values in debug and this is
  # only iterated over.
  @root_schemas =
    routes.map do |route|
      [
        route.request_body&.type,
        route.responses.map(&:content).flatten.map(&:type)
      ]
    end.flatten.compact.uniq.freeze

  @routes_by_path = {}
  routes.each do |route|
    @routes_by_path[route.path] ||= {}
    @routes_by_path[route.path][route.http_method] = route
  end
  @routes_by_path.freeze

  @routes_by_id = routes.map { |route| [route.id, route] }.to_h.freeze
end

Instance Attribute Details

#root_schemasObject (readonly)

Returns the value of attribute root_schemas.



27
28
29
# File 'lib/modern/descriptor/core.rb', line 27

def root_schemas
  @root_schemas
end

#routes_by_idObject (readonly)

Returns the value of attribute routes_by_id.



28
29
30
# File 'lib/modern/descriptor/core.rb', line 28

def routes_by_id
  @routes_by_id
end

#routes_by_pathObject (readonly)

Returns the value of attribute routes_by_path.



29
30
31
# File 'lib/modern/descriptor/core.rb', line 29

def routes_by_path
  @routes_by_path
end

#securities_by_nameObject (readonly)

Returns the value of attribute securities_by_name.



26
27
28
# File 'lib/modern/descriptor/core.rb', line 26

def securities_by_name
  @securities_by_name
end