Module: MediaWikiVagrant::SpecHelpers::String

Defined in:
tests/spec/support/string.rb

Instance Method Summary collapse

Instance Method Details

#align(str) ⇒ Object

Removes the least amount of leading spaces from the beginning of each line in order to left align the given string. This method helps to keep things tidy when using heredocs in examples.

Examples:

align(<<-end)
  foo
    bar
end
# => "foo\n  bar"


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'tests/spec/support/string.rb', line 15

def align(str)
  padding = str.each_line.reduce(nil) do |min, line|
    line_padding = line.match(/^( +)[^ ]/) { |m| m[1].length }

    if min && line_padding
      [min, line_padding].min
    else
      min || line_padding
    end
  end

  padding.nil? ? str : str.gsub(/^ {#{padding}}/, '')
end