lib/open_api/dsl.rb in zero-rails_openapi-1.0.0 vs lib/open_api/dsl.rb in zero-rails_openapi-1.1.0

- old
+ new

@@ -6,53 +6,59 @@ base.extend ClassMethods end # TODO: Doc-Block Comments module ClassMethods + def ctrl_path path + @_ctrl_path = path + @_apis_tag = path.split('/').last.camelize + end + def apis_set desc = '', external_doc_url = '', &block - @_api_infos, @_ctrl_infos = { }, { } + @_ctrl_infos = { } # current `tag`, this means that tags is currently divided by controllers. - tag = @_ctrl_infos[:tag] = { name: controller_name.camelize } + tag = @_ctrl_infos[:tag] = { name: @_apis_tag ||= controller_name.camelize } tag[:description] = desc if desc.present? tag[:externalDocs] = { description: 'ref', url: external_doc_url } if external_doc_url.present? current_ctrl = @_ctrl_infos[:components] = CtrlInfoObj.new current_ctrl.instance_eval &block if block_given? end def open_api method, summary = '', &block - # select the routing info corresponding to the current method from the routing list. - action_path = "#{controller_path}##{method}" + apis_set if @_ctrl_infos.nil? + + # select the routing info (corresponding to the current method) from the routing list. + action_path = "#{@_ctrl_path ||= controller_path}##{method}" routes_info = ctrl_routes_list.select { |api| api[:action_path].match? /^#{action_path}$/ }.first - puts "[zero-rails_openapi] Routing mapping failed: #{controller_path}##{method}" or return if routes_info.nil? + puts "[zero-rails_openapi] Routing mapping failed: #{@_ctrl_path}##{method}" or return if routes_info.nil? # structural { path: { http_method:{ } } }, for Paths Object. - # it will be merged into :paths - path = @_api_infos[routes_info[:path]] ||= { } + path = (@_api_infos ||= { })[routes_info[:path]] ||= { } current_api = path[routes_info[:http_verb]] = - ApiInfoObj.new(action_path).merge!( summary: summary, operationId: method, tags: [controller_name.camelize] ) + ApiInfoObj.new(action_path).merge! summary: summary, operationId: method, tags: [@_apis_tag] current_api.tap do |it| it.instance_eval &block if block_given? [method, :all].each do |key| # blocks_store_key - @apis_blocks[key]&.each { |blk| it.instance_eval &blk } + @_apis_blocks&.[](key)&.each { |blk| it.instance_eval &blk } end end end # For DRY; method could be symbol array def open_api_set method = :all, desc = '', &block - @apis_blocks ||= { } + @_apis_blocks ||= { } if method.is_a? Array - method.each { |m| (@apis_blocks[m.to_sym] ||= [ ]) << block } + method.each { |m| (@_apis_blocks[m.to_sym] ||= [ ]) << block } else - (@apis_blocks[method.to_sym] ||= [ ]) << block + (@_apis_blocks[method.to_sym] ||= [ ]) << block end end def ctrl_routes_list @routes_list ||= Generator.generate_routes_list - @routes_list[controller_path] + @routes_list[@_ctrl_path] end end end end