utils.rb 1.01 KB
Newer Older
1 2 3 4 5 6 7
module Gitlab
  module Utils
    extend self

    # Run system command without outputting to stdout.
    #
    # @param  cmd [Array<String>]
Valery Sizov's avatar
Valery Sizov committed
8
    # @return [Boolean]
9
    def system_silent(cmd)
10
      Popen.popen(cmd).last.zero?
11
    end
12 13 14 15

    def force_utf8(str)
      str.force_encoding(Encoding::UTF_8)
    end
16

17 18 19 20 21 22 23 24 25 26 27 28 29
    # A slugified version of the string, suitable for inclusion in URLs and
    # domain names. Rules:
    #
    #   * Lowercased
    #   * Anything not matching [a-z0-9-] is replaced with a -
    #   * Maximum length is 63 bytes
    #   * First/Last Character is not a hyphen
    def slugify(str)
      return str.downcase
        .gsub(/[^a-z0-9]/, '-')[0..62]
        .gsub(/(\A-+|-+\z)/, '')
    end

30 31 32 33 34 35 36
    def to_boolean(value)
      return value if [true, false].include?(value)
      return true if value =~ /^(true|t|yes|y|1|on)$/i
      return false if value =~ /^(false|f|no|n|0|off)$/i

      nil
    end
37 38 39 40 41 42 43 44

    def boolean_to_yes_no(bool)
      if bool
        'Yes'
      else
        'No'
      end
    end
45 46
  end
end