Class: Vedeu::Sentence

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/sentence.rb

Overview

Converts the array of elements provided into a comma separated sentence with the penultimate and ultimate elements separated with the word ‘and’.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements, label = 'elements') ⇒ Vedeu::Sentence

Parameters:

  • elements (Array)
  • label (String) (defaults to: 'elements')


20
21
22
23
# File 'lib/vedeu/sentence.rb', line 20

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

Instance Attribute Details

#elementsArray (readonly, protected)

Returns:

  • (Array)


46
47
48
# File 'lib/vedeu/sentence.rb', line 46

def elements
  @elements
end

#labelString (readonly, protected)

Returns:

  • (String)


50
51
52
# File 'lib/vedeu/sentence.rb', line 50

def label
  @label
end

Class Method Details

.construct(elements, label = 'elements') ⇒ String

Parameters:

  • elements (Array)
  • label (String) (defaults to: 'elements')

Returns:

  • (String)


13
14
15
# File 'lib/vedeu/sentence.rb', line 13

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

Instance Method Details

#but_lastArray (private)

Returns:

  • (Array)


70
71
72
# File 'lib/vedeu/sentence.rb', line 70

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

#constructString

Returns:

  • (String)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vedeu/sentence.rb', line 26

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

#countFixnum (private)

Returns:

  • (Fixnum)


85
86
87
# File 'lib/vedeu/sentence.rb', line 85

def count
  elements.size
end

#firstvoid (private)

This method returns an undefined value.



75
76
77
# File 'lib/vedeu/sentence.rb', line 75

def first
  elements.first
end

#lastvoid (private)

This method returns an undefined value.



80
81
82
# File 'lib/vedeu/sentence.rb', line 80

def last
  elements[-1]
end

#many?Boolean (private)

Returns:



65
66
67
# File 'lib/vedeu/sentence.rb', line 65

def many?
  count > 2
end

#one?Boolean (private)

Returns:



55
56
57
# File 'lib/vedeu/sentence.rb', line 55

def one?
  count == 1
end

#two?Boolean (private)

Returns:



60
61
62
# File 'lib/vedeu/sentence.rb', line 60

def two?
  count == 2
end