Sha256: 26de4a587e36465b12bdc8bf352ffa0dde53399633f2c055ac7ad96b59a5c03f

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module Fezzik
  module Util
    def self.capture_output(&block)
      output = StringIO.new
      Thread.current[:stdout] = output
      block.call
      output.string
    ensure
      Thread.current[:stdout] = STDOUT
    end

    def self.split_task_and_params(task_with_params)
      params_match = /(.+)\[(.+)\]/.match(task_with_params)
      if params_match
        task = params_match[1]
        params = params_match[2].split(",")
      else
        task = task_with_params
        params = nil
      end
      [task, params]
    end

    # Blocks passed to this function will ensure `domain` is always an Array. In addition, if `user` is set it
    # will automatically prepend the value of `user` to the front of any domains missing a username. This allows a
    # fezzik user to write the following in their configuration and have it "just work":
    #     set :domain, ["example.com", "test@example2.com"]
    #     set :user, "root"
    # `domain` will be equal to ["root@example.com", "test@example2.com"]
    def self.with_prepended_user(&block)
      old_domain = domain
      begin
        set :domain, Array(domain).map { |d| (defined?(user) && !d.include?("@")) ? "#{user}@#{d}" : d }
        block.call
      ensure
        set :domain, old_domain
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fezzik-0.7.4 lib/fezzik/util.rb
fezzik-0.7.3 lib/fezzik/util.rb