Sha256: 1698d9bbbb14fe2197aedae638119ed06bda0e06b5ce56669593d8497bbc54c0

Contents?: true

Size: 879 Bytes

Versions: 3

Compression:

Stored size: 879 Bytes

Contents

module Vedeu

  class Sentence

    class << self

      def construct(elements, label = 'elements')
        new(elements, label).construct
      end

    end # Sentence eigenclass

    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 # Sentence

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.3.0 lib/vedeu/support/sentence.rb
vedeu-0.2.12 lib/vedeu/support/sentence.rb
vedeu-0.2.11 lib/vedeu/support/sentence.rb