Sha256: 16623f7f6700c499886fbf3e6fb3587684504123dc67885561243c26cffd5f4c

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Doggy
  module SharedHelpers
    MAX_TRIES = 5

    def self.strip_heredoc(string)
      indent = string.scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
      string.gsub(/^[ \t]{#{indent}}/, '')
    end

    def self.with_retry(times: MAX_TRIES, reraise: false)
      tries = 0
      while tries < times
        begin
          yield
          break
        rescue => e
          error "Caught error! Attempt #{tries}..."
          error "#{e.class.name}: #{e.message}"
          error "#{e.backtrace.join("\n")}"
          tries += 1

          raise e if tries >= times && reraise
        end
      end
    end

    def self.agree(prompt)
      raise Error, "Not a tty" unless $stdin.tty?

      puts prompt + " (Y/N)"
      line = $stdin.readline.chomp.upcase
      puts
      line == "Y"
    end

    def self.error(msg)
      puts "[ERROR] #{ msg }"
    end

    def self.find_root
      File.dirname(find_file("Gemfile"))
    end

    def self.find_file(*names)
      search_up(*names) do |filename|
        return filename if File.file?(filename)
      end
    end

    def self.search_up(*names)
      previous = nil
      current  = File.expand_path(Pathname.pwd)

      until !File.directory?(current) || current == previous
        names.each do |name|
          filename = File.join(current, name)
          yield filename
        end
        current, previous = File.expand_path("..", current), current
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
doggy-0.2.2 lib/doggy/shared_helpers.rb