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 ⇒ Object private
- #initialize(elements, label = 'elements') ⇒ Vedeu::Sentence constructor
- #last ⇒ Object private
- #many? ⇒ Boolean private
- #one? ⇒ Boolean private
- #two? ⇒ Boolean private
Constructor Details
#initialize(elements, label = 'elements') ⇒ Vedeu::Sentence
22 23 24 25 |
# File 'lib/vedeu/sentence.rb', line 22 def initialize(elements, label = 'elements') @elements = elements @label = label end |
Instance Attribute Details
#elements ⇒ Array (readonly, protected)
48 49 50 |
# File 'lib/vedeu/sentence.rb', line 48 def elements @elements end |
#label ⇒ String (readonly, protected)
52 53 54 |
# File 'lib/vedeu/sentence.rb', line 52 def label @label end |
Class Method Details
.construct(elements, label = 'elements') ⇒ 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_last ⇒ Array (private)
72 73 74 |
# File 'lib/vedeu/sentence.rb', line 72 def but_last elements[0...-1].join(', ') end |
#construct ⇒ 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 |
#count ⇒ Fixnum (private)
87 88 89 |
# File 'lib/vedeu/sentence.rb', line 87 def count elements.size end |
#first ⇒ Object (private)
77 78 79 |
# File 'lib/vedeu/sentence.rb', line 77 def first elements.first end |
#last ⇒ Object (private)
82 83 84 |
# File 'lib/vedeu/sentence.rb', line 82 def last elements[-1] end |