【问题标题】:How to call a Thor task multiple times?如何多次调用 Thor 任务?
【发布时间】:2012-10-05 15:19:37
【问题描述】:

雷神像 Rake(和 Make)一样具有任务管理功能。如果我多次调用一个任务,它只会有效地调用该任务一次。如何多次调用一个任务?

我尝试修改 @_invocations 哈希,但没有奏效:

require 'csv'
require './config/environment'

class MisReport < Thor

  desc "all", "generate mysql and postgres mis"
  def all
    generate("pg_mis_report", "pg")
    generate("mysql_mis_report", "mysql") 
  end

  desc "generate", "generate mis report"
  def generate(file_name = "mis_report_#{Time.now.to_s(:number)}", connection = "postgres") 
    if connection == "pg"
      puts "== postgres database"
      ActiveRecord::Base.establish_connection :development_mysql
    else
      puts "== mysql database"
      ActiveRecord::Base.establish_connection :development
    end

    # generate MIS

    puts
    puts "mis file is at: #{file_path}"
  end

end

【问题讨论】:

    标签: ruby rake thor


    【解决方案1】:
    class MisReport < Thor
    
      desc "all", "generate mysql and postgres mis"
      def all
        MisReport.new.invoke "generate", ["pg_mis_report", "pg"]
        MisReport.new.invoke "generate", ["mysql_mis_report", "mysql"]
      end
    end
    

    支持带选项调用。

    【讨论】:

      【解决方案2】:

      查看 Thor 文档,我找不到强制多次调用同一任务的方法。这可能是设计使然。

      我创建了一个解决方法,通过将报告生成请求组合到一个任务中来满足您的要求,该任务又依赖于 Thor 为非任务方法提供的“no_task”块。您可以灵活地添加要生成的新报告,方法是将它们添加到传递给generate 的散列参数中。

      class MisReport < Thor
      
        desc "all", "generate mysql and postgres mis"
        def all
          generate({:pg_mis_report => "pg", :mysql_mis_report => "mysql"})
        end
      
        desc "generate", "generate mis report"
        def generate(hash)
          hash.each_pair {|file_name, connection| generate_report(file_name.to_s, connection)}
        end
      
        no_tasks{
          def generate_report(file_name = "mis_report_#{Time.now.to_s(:number)}", connection = "postgres") 
      
            if connection == "pg"
              puts "== postgres database"
              #ActiveRecord::Base.establish_connection :development_mysql
            else
              puts "== mysql database"
              #ActiveRecord::Base.establish_connection :development
            end
      
            # generate MIS
      
            file_path = "/some/path/to/#{file_name}"
            puts
            puts "mis file is at: #{file_path}"
          end
        }
      
      end
      

      我已经测试过了,输出如下:

      $> thor mis_report:all
      == postgres database
      
      mis file is at: /some/path/to/pg_mis_report
      == mysql database
      
      mis file is at: /some/path/to/mysql_mis_report
      

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        这就是我通过为 Thor 创建一个模仿 Rake["my:task"].execute 工作原理的小型辅助方法来解决问题的方法。

        把它放在你的任务类的任何地方。

        no_commands do 
          def execute(task, args, options)
            (klass, task) = Thor::Util.find_class_and_command_by_namespace(task)
            klass.new.invoke(task, args, options)
          end
        end
        

        那么你可以像invoke一样使用execute

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-02
          • 2023-02-02
          • 2014-12-13
          相关资源
          最近更新 更多