• 问一个数据库设计问题. at 2017年3月08日

    +1

  • +----------------------+-------+-------+---------+---------+-----+-------+
    | Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
    +----------------------+-------+-------+---------+---------+-----+-------+
    | Controllers          | 23878 | 13391 |     370 |    1760 |   4 |     5 |
    | Helpers              |  2381 |  1989 |       1 |     300 | 300 |     4 |
    | Models               | 21610 | 14640 |     335 |    1894 |   5 |     5 |
    | Mailers              |   745 |   625 |      27 |      91 |   3 |     4 |
    | Javascripts          | 18787 | 14934 |      35 |    2317 |  66 |     4 |
    | Libraries            |   702 |   552 |      14 |      71 |   5 |     5 |
    | Controller specs     | 11811 | 10239 |       2 |      17 |   8 |   600 |
    | Decorator specs      |   476 |   409 |       0 |       0 |   0 |     0 |
    | Feature specs        |  1082 |   910 |       0 |       1 |   0 |   908 |
    | Form specs           |  1505 |  1363 |       0 |       0 |   0 |     0 |
    | Helper specs         |   156 |    63 |       0 |       0 |   0 |     0 |
    | Lib specs            |    48 |    41 |       0 |       0 |   0 |     0 |
    | Mailer specs         |   393 |   329 |       0 |       0 |   0 |     0 |
    | Model specs          | 18993 | 13438 |       0 |      10 |   0 |  1341 |
    | Policy specs         |    36 |    31 |       0 |       0 |   0 |     0 |
    | Presenter specs      |   239 |   214 |       0 |       0 |   0 |     0 |
    | Query specs          |   174 |   156 |       0 |       0 |   0 |     0 |
    | Request specs        |   713 |   600 |       0 |       0 |   0 |     0 |
    | Serializer specs     |   784 |   419 |       0 |       0 |   0 |     0 |
    | Service specs        |  5376 |  4762 |       1 |      15 |  15 |   315 |
    | Uploader specs       |    51 |    41 |       0 |       0 |   0 |     0 |
    | Validator specs      |    75 |    64 |       1 |       1 |   1 |    62 |
    | Value specs          |   268 |   225 |       0 |       0 |   0 |     0 |
    | View specs           |  7107 |  6745 |       0 |       4 |   0 |  1684 |
    | Worker specs         |   114 |   101 |       0 |       0 |   0 |     0 |
    | Wrapper specs        |    32 |    26 |       0 |       0 |   0 |     0 |
    +----------------------+-------+-------+---------+---------+-----+-------+
    | Total                | 117536 | 86307 |     786 |    6481 |   8 |    11 |
    +----------------------+-------+-------+---------+---------+-----+-------+
      Code LOC: 46131     Test LOC: 40176     Code to Test Ratio: 1:0.9
    
  • 分母+1

  • 👍

    我以为会一直嵌套呢 😄

  • 如果有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 走哪看哪