• 👍

    我以为会一直嵌套呢 😄

  • 如果有A任务耗时多了怎么办 如果有A任务失败了怎么办

  • 其实直接在rails route里面把你record的路由全部写出来就好了 一旦有新的记录就重新reload一次,

    match your_record_url, to: 'controller#action', via: :all, defaults: { id: record_id }
    

    如果 试着去用Rails内置的route方法解析和查找记录 或许可以这样用 ps: 如果路由记录太多话 可能需要考虑其他办法

    require 'singleton'
    require 'action_dispatch'
    require 'active_support/all'
    class RequestRoutes
      include Singleton
    
      def match(path, method)
        memos = simulate.simulate(path).try(:memos)
        return nil if memos.blank?
        memos.reverse.find { |memo| memo[:request_method] == method }
      end
    
      def add_route(api_request)
        append_to_ast(api_request)
        clear_cache
      end
    
      def clear_cache
        @simulate = nil
      end
    
      def reload
        @ast = nil
        @simulate = nil
      end
    
      private
    
      def simulate
        @simulate ||= begin
          builder = ActionDispatch::Journey::GTG::Builder.new ActionDispatch::Journey::Nodes::Or.new ast
          table = builder.transition_table
          ActionDispatch::Journey::GTG::Simulator.new table
        end
      end
    
      def ast
        @ast ||= []
        # 这里可以把你所有的记录全部转换成ast
        # YourRecord.all.map do |api_request|
        #   parse_to_nodes(api_request)
        #  end
      end
    
      def parse_to_nodes(api_request)
        memo = {
          request_id: api_request[:id],
          request_method: api_request[:method],
          pattern: ActionDispatch::Journey::Path::Pattern.from_string(api_request[:url])
        }
        nodes = parser.parse (api_request[:url])
        nodes.each { |n| n.memo = memo }
        nodes
      end
    
      def append_to_ast(api_request)
        @ast << parse_to_nodes(api_request)
      end
    
      def parser
        ActionDispatch::Journey::Parser.new
      end
    end
    
    request = {
        id: 1,
        method: 'GET',
        url: '/publishers/:p_id/magazines/:m_id/photos/:p_id'
    }
    RequestRoutes.instance.add_route(request)
    memo =  RequestRoutes.instance.match('/publishers/1/magazines/2/photos/1', 'GET')
    match_date = memo[:pattern].match('/publishers/1/magazines/2/photos/1')
    match_date.names.zip(match_date.captures).to_h
    
  • 老婆是做测试的 经常会跑去TesterHome逛逛 赞一个!

  • 赞! 有没有电子版? 喜欢用kindle 走哪看哪

  • 膜拜一下

  • 一份迟了两年多才看到的帖子 赞一个

  • 关于多对多关系 at 2016年11月23日
    class Book < ActiveRecord::Base
      has_many :book_tags
      has_many :tags, through: :book_tags
    end
    
    class Tag < ActiveRecord::Base
      has_many :book_tags
      has_many :books, through: :book_tags
    end
    
    class BookTag < ActiveRecord::Base
      belongs_to :tag
      belongs_to :book
    end
    
    book = Book.create
    tag = Tag.create
    book.tags << tag || tag.books << book