Sha256: 78c4e7d6b385a8413c575fd2f06332b577b7e5da0f696579f5d1b80d39bacd59

Contents?: true

Size: 1000 Bytes

Versions: 5

Compression:

Stored size: 1000 Bytes

Contents

module Troo
  class Sentence
    class << self
      # @param  [Array]
      # @param  [String]
      # @return [String]
      def construct(elements, label = 'elements')
        new(elements, label).construct
      end
    end

    # @param  [Array]
    # @param  [String]
    # @return [Troo::Sentence]
    def initialize(elements, label)
      @elements, @label = elements, label
    end

    # @return [String]
    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

5 entries across 5 versions & 1 rubygems

Version Path
troo-0.0.15 lib/troo/presentation/sentence.rb
troo-0.0.14 lib/troo/presentation/sentence.rb
troo-0.0.13 lib/troo/presentation/sentence.rb
troo-0.0.12 lib/troo/presentation/sentence.rb
troo-0.0.11 lib/troo/presentation/sentence.rb