Sha256: 96e2afd0390187b86c96bf29f56e2addb223cdf036376ba958ee7406d72521b1

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

module Vedeu

  # Take a collection of words (elements) and form a sentence from them.
  #
  # @example
  #   elements = ['Hydrogen', 'Helium', 'Lithium']
  #   Vedeu::Sentence.construct(elements) # => 'Hydrogen, Helium and Lithium'
  #
  class Sentence

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

    # @param elements [Array]
    # @param label [String]
    # @return [Vedeu::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

    # @!attribute [r] elements
    # @return [Array]
    attr_reader :elements

    # @!attribute [r] label
    # @return [String]
    attr_reader :label

    # @return [Boolean]
    def one?
      count == 1
    end

    # @return [Boolean]
    def two?
      count == 2
    end

    # @return [Boolean]
    def many?
      count > 2
    end

    # @return [String]
    def but_last
      elements[0...-1].join(', ')
    end

    # @return [String]
    def first
      elements.first
    end

    # @return [String]
    def last
      elements[-1]
    end

    # @return [Fixnum]
    def count
      elements.size
    end

  end # Sentence

end # Vedeu

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vedeu-0.4.9 lib/vedeu/support/sentence.rb
vedeu-0.4.8 lib/vedeu/support/sentence.rb
vedeu-0.4.7 lib/vedeu/support/sentence.rb
vedeu-0.4.6 lib/vedeu/support/sentence.rb