Sha256: 4faa376b9e16c14b3e99aee92abe5b6f304ce55e4e3dc104810ea6c92e8b43b8

Contents?: true

Size: 827 Bytes

Versions: 3

Compression:

Stored size: 827 Bytes

Contents

module Troo
  class Sentence
    class << self
      def construct(elements, label = 'elements')
        new(elements, label).construct
      end
    end

    def initialize(elements, label)
      @elements, @label = elements, label
    end

    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

    attr_reader :elements, :label

    def one?
      count == 1
    end

    def two?
      count == 2
    end

    def many?
      count > 2
    end

    def but_last
      elements[0...-1].join(', ')
    end

    def first
      elements.first
    end

    def last
      elements[-1]
    end

    def count
      elements.size
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
troo-0.0.10 lib/troo/presentation/sentence.rb
troo-0.0.9 lib/troo/presentation/sentence.rb
troo-0.0.8 lib/troo/presentation/sentence.rb