class Troo::Sentence

Attributes

elements[R]
label[R]

Public Class Methods

construct(elements, label = 'elements') click to toggle source

@param [Array] @param [String] @return [String]

# File lib/troo/presentation/sentence.rb, line 7
def construct(elements, label = 'elements')
  new(elements, label).construct
end
new(elements, label) click to toggle source

@param [Array] @param [String] @return [Troo::Sentence]

# File lib/troo/presentation/sentence.rb, line 15
def initialize(elements, label)
  @elements, @label = elements, label
end

Public Instance Methods

construct() click to toggle source

@return [String]

# File lib/troo/presentation/sentence.rb, line 20
def construct
  if one?
    first
  elsif two?
    elements.join(' and ')
  elsif many?
    [but_last, last].join(' and ')
  else
    "No #{label} have been assigned."
  end
end

Private Instance Methods

but_last() click to toggle source
# File lib/troo/presentation/sentence.rb, line 48
def but_last
  elements[0...-1].join(', ')
end
count() click to toggle source
# File lib/troo/presentation/sentence.rb, line 60
def count
  elements.size
end
first() click to toggle source
# File lib/troo/presentation/sentence.rb, line 52
def first
  elements.first
end
last() click to toggle source
# File lib/troo/presentation/sentence.rb, line 56
def last
  elements[-1]
end
many?() click to toggle source
# File lib/troo/presentation/sentence.rb, line 44
def many?
  count > 2
end
one?() click to toggle source
# File lib/troo/presentation/sentence.rb, line 36
def one?
  count == 1
end
two?() click to toggle source
# File lib/troo/presentation/sentence.rb, line 40
def two?
  count == 2
end