lib/open_api/dsl.rb in zero-rails_openapi-1.5.1 vs lib/open_api/dsl.rb in zero-rails_openapi-1.5.2

- old
+ new

@@ -1,67 +1,68 @@ -require 'open_api/dsl/api_info_obj' +require 'open_api/dsl/api_info' require 'open_api/dsl/components' module OpenApi module DSL def self.included(base) base.extend ClassMethods end # TODO: Doc-Block Comments module ClassMethods - def ctrl_path path - @_ctrl_path = path - @_apis_tag = path.split('/').last.camelize + def route_base path + @route_base = path + @doc_tag = path.split('/').last.camelize end - def apis_tag name: nil, desc: '', external_doc_url: '' - # current `tag`, this means that tags is currently divided by controllers. - @_apis_tag = name if name.present? - @_apis_tag ||= controller_name.camelize - tag = (@_ctrl_infos = { })[:tag] = { name: @_apis_tag } + def doc_tag name: nil, desc: '', external_doc_url: nil + # apis will group by the tags. + @doc_tag = name if name.present? + @doc_tag ||= controller_name.camelize + tag = (@doc_info = { })[:tag] = { name: @doc_tag } tag[:description] = desc if desc.present? - tag[:externalDocs] = { description: 'ref', url: external_doc_url } if external_doc_url.present? + tag[:externalDocs] = { description: 'ref', url: external_doc_url } if external_doc_url end def components &block - apis_tag if @_ctrl_infos.nil? - current_ctrl = @_ctrl_infos[:components] = Components.new - current_ctrl.instance_exec(&block) - current_ctrl.process_objs + doc_tag if @doc_info.nil? + current_doc = @doc_info[:components] = Components.new + current_doc.instance_exec(&block) + current_doc.process_objs end - def api action, summary = '', http: nil, skip: [ ], use: [ ], &block - apis_tag if @_ctrl_infos.nil? + def api action, summary = '', http: http_method = nil, skip: [ ], use: [ ], &block + doc_tag if @doc_info.nil? # select the routing info (corresponding to the current method) from routing list. - action_path = "#{@_ctrl_path ||= controller_path}##{action}" - routes_info = ctrl_routes_list&.select { |api| api[:action_path].match?(/^#{action_path}$/) }&.first - pp "[ZRO Warning] Routing mapping failed: #{@_ctrl_path}##{action}" and return if routes_info.nil? + action_path = "#{@route_base ||= controller_path}##{action}" + routes = ctrl_routes_list&.select { |api| api[:action_path].match?(/^#{action_path}$/) } + pp "[ZRO Warning] Routing mapping failed: #{@route_base}##{action}" and return if routes.blank? - api = ApiInfoObj.new(action_path, skip: Array(skip), use: Array(use)) - .merge! description: '', summary: summary, operationId: action, tags: [@_apis_tag], - parameters: [ ], requestBody: '', responses: { }, security: [ ], servers: [ ] - [action, :all].each { |blk_key| @_api_dry_blocks&.[](blk_key)&.each { |blk| api.instance_eval(&blk) } } - api.param_use = [ ] # `skip` and `use` only affect `api_dry`'s blocks - api.param_skip = [ ] - api.param_use = [ ] # `skip` and `use` only affect `api_dry`'s blocks + api = ApiInfo.new(action_path, skip: Array(skip), use: Array(use)) + .merge! description: '', summary: summary, operationId: action, tags: [@doc_tag], + parameters: [ ], requestBody: '', responses: { }, security: [ ], servers: [ ] + [action, :all].each { |blk_key| @zro_dry_blocks&.[](blk_key)&.each { |blk| api.instance_eval(&blk) } } + api.param_use = api.param_skip = [ ] # `skip` and `use` only affect `api_dry`'s blocks api.instance_exec(&block) if block_given? api.process_objs api.delete_if { |_, v| v.blank? } - path = (@_api_infos ||= { })[routes_info[:path]] ||= { } - (http || routes_info[:http_verb]).split('|').each { |verb| path[verb] = api } + routes.each do |route| + path = (@api_info ||= { })[route[:path]] ||= { } + (http || route[:http_verb]).split('|').each { |verb| path[verb] = api } + end + api end # method could be symbol array, like: %i[ .. ] def api_dry action = :all, desc = '', &block - @_api_dry_blocks ||= { } - Array(action).each { |a| (@_api_dry_blocks[a.to_sym] ||= [ ]) << block } + @zro_dry_blocks ||= { } + Array(action).each { |a| (@zro_dry_blocks[a.to_sym] ||= [ ]) << block } end def ctrl_routes_list - Generator.routes_list[@_ctrl_path] + Generator.routes_list[@route_base] end end end end