Module: RSpec::TagMatchers::Helpers::SentenceHelper

Included in:
RSpec::TagMatchers::HasDateSelect, RSpec::TagMatchers::HasTag
Defined in:
lib/rspec/tag_matchers/helpers/sentence_helper.rb

Instance Method Summary (collapse)

Instance Method Details

- (String) make_sentence(*strings)

Joins multiple strings into a sentence with punctuation and conjunctions.

Examples:

Forming sentences

make_sentence("foo")                # => "foo"
make_sentence("foo", "bar")         # => "foo and bar"
make_sentence("foo", "bar", "baz")  # => "foo, bar, and baz"

Overriding the conjunction

make_sentence("foo", "bar", "baz", :conjunction => "or")  # => "foo, bar, or baz"


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspec/tag_matchers/helpers/sentence_helper.rb', line 20

def make_sentence(*strings)
  options     = strings.last.is_a?(Hash) ? strings.pop : {}
  strings     = strings.flatten.map(&:to_s).reject(&:empty?)
  conjunction = options[:conjunction] || "and"

  case strings.count
  when 0
    ""
  when 1
    strings.first
  else
    last       = strings.pop
    puncuation = strings.count > 1 ? ", " : " "

    [strings, "#{conjunction} #{last}"].flatten.join(puncuation)
  end
end