Class: Vedeu::Sentence
- Inherits:
-
Object
- Object
- Vedeu::Sentence
- 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
- #elements ⇒ Array readonly protected
- #label ⇒ String readonly protected
Class Method Summary collapse
Instance Method Summary collapse
- #but_last ⇒ Array private
- #construct ⇒ String
- #count ⇒ Fixnum private
- #first ⇒ void private
- #initialize(elements, label = 'elements') ⇒ Vedeu::Sentence constructor
- #last ⇒ void private
- #many? ⇒ Boolean private
- #one? ⇒ Boolean private
- #two? ⇒ Boolean private
Constructor Details
#initialize(elements, label = 'elements') ⇒ Vedeu::Sentence
20 21 22 23 |
# File 'lib/vedeu/sentence.rb', line 20 def initialize(elements, label = 'elements') @elements = elements @label = label end |
Instance Attribute Details
#elements ⇒ Array (readonly, protected)
46 47 48 |
# File 'lib/vedeu/sentence.rb', line 46 def elements @elements end |
#label ⇒ String (readonly, protected)
50 51 52 |
# File 'lib/vedeu/sentence.rb', line 50 def label @label end |
Class Method Details
.construct(elements, label = 'elements') ⇒ 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_last ⇒ Array (private)
70 71 72 |
# File 'lib/vedeu/sentence.rb', line 70 def but_last elements[0...-1].join(', ') end |
#construct ⇒ 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 |
#count ⇒ Fixnum (private)
85 86 87 |
# File 'lib/vedeu/sentence.rb', line 85 def count elements.size end |
#first ⇒ void (private)
This method returns an undefined value.
75 76 77 |
# File 'lib/vedeu/sentence.rb', line 75 def first elements.first end |
#last ⇒ void (private)
This method returns an undefined value.
80 81 82 |
# File 'lib/vedeu/sentence.rb', line 80 def last elements[-1] end |