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')


22
23
24
25
# File 'lib/vedeu/sentence.rb', line 22

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

Instance Attribute Details

#elementsArray (readonly, protected)

Returns:

  • (Array)


48
49
50
# File 'lib/vedeu/sentence.rb', line 48

def elements
  @elements
end

#labelString (readonly, protected)

Returns:

  • (String)


52
53
54
# File 'lib/vedeu/sentence.rb', line 52

def label
  @label
end

Class Method Details

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

Parameters:

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

Returns:

  • (String)


15
16
17
# File 'lib/vedeu/sentence.rb', line 15

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

Instance Method Details

#but_lastArray (private)

Returns:

  • (Array)


72
73
74
# File 'lib/vedeu/sentence.rb', line 72

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

#constructString

Returns:

  • (String)


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

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)


87
88
89
# File 'lib/vedeu/sentence.rb', line 87

def count
  elements.size
end

#firstObject (private)

Returns:

  • (Object)


77
78
79
# File 'lib/vedeu/sentence.rb', line 77

def first
  elements.first
end

#lastObject (private)

Returns:

  • (Object)


82
83
84
# File 'lib/vedeu/sentence.rb', line 82

def last
  elements[-1]
end

#many?Boolean (private)

Returns:



67
68
69
# File 'lib/vedeu/sentence.rb', line 67

def many?
  count > 2
end

#one?Boolean (private)

Returns:



57
58
59
# File 'lib/vedeu/sentence.rb', line 57

def one?
  count == 1
end

#two?Boolean (private)

Returns:



62
63
64
# File 'lib/vedeu/sentence.rb', line 62

def two?
  count == 2
end