Class: SwaggerApi::Components

Inherits:
Object
  • Object
show all
Includes:
ActiveAttr::Model, SwaggerApi::Concerns::StiSchema
Defined in:
lib/swagger_api/components.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controllersObject

Returns the value of attribute controllers.



8
9
10
# File 'lib/swagger_api/components.rb', line 8

def controllers
  @controllers
end

Instance Method Details

#createObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/swagger_api/components.rb', line 10

def create
  return @components unless @components.nil?
  @components = {}
  controllers.each do |controller|
    if controller.custom_model_file.nil?
      @components.merge!(create_model_components_from_controller(controller))
    else
      @components.merge!(custom_json(controller.custom_model_file))
    end
  end
  @components
end

#create_model_components_from_controller(controller) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/swagger_api/components.rb', line 28

def create_model_components_from_controller(controller)
  if sti?(controller.model)
    sti_models(controller.model.constantize)
  else
    {
      controller.model => ComponentSchema.new(controller: controller).create
    }
  end
end

#custom_json(custom_model_file) ⇒ Object



23
24
25
26
# File 'lib/swagger_api/components.rb', line 23

def custom_json(custom_model_file)
  file = File.read(custom_model_file)
  JSON.parse(file)
end

#sti_models(model_klass) ⇒ Object



38
39
40
41
42
# File 'lib/swagger_api/components.rb', line 38

def sti_models(model_klass)
  model_klass.descendants.map do |klass|
    [klass.name, ComponentSchema.new(controller: OpenStruct.new(model: klass.name)).create]
  end.to_h
end