Class: Label
- Inherits:
-
Object
- Object
- Label
- Defined in:
- lib/label.rb
Overview
Label = Letter + Number [+ Description]
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#letter ⇒ Object
Returns the value of attribute letter.
-
#number ⇒ Object
Returns the value of attribute number.
Instance Method Summary collapse
-
#initialize(input) ⇒ Label
constructor
A new instance of Label.
-
#name ⇒ Object
Implemented for compatibility reasons.
-
#parse(label) ⇒ Object
Return the label letter for a change if the label name matches the expected pattern.
- #to_str ⇒ Object
Constructor Details
#initialize(input) ⇒ Label
Returns a new instance of Label.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/label.rb', line 18 def initialize(input) raise InvalidInput, 'Invalid, it must be a non-empty string' unless input p = parse(input) raise InvalidLabel, format('Invalid label "%<input>s"', { input: input }) unless p @letter = p[0].upcase @number = p[1] @description = p[2] unless p[2].empty? end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
5 6 7 |
# File 'lib/label.rb', line 5 def description @description end |
#letter ⇒ Object
Returns the value of attribute letter.
5 6 7 |
# File 'lib/label.rb', line 5 def letter @letter end |
#number ⇒ Object
Returns the value of attribute number.
5 6 7 |
# File 'lib/label.rb', line 5 def number @number end |
Instance Method Details
#name ⇒ Object
Implemented for compatibility reasons
30 31 32 |
# File 'lib/label.rb', line 30 def name to_str end |
#parse(label) ⇒ Object
Return the label letter for a change if the label name matches the expected pattern. nil otherwise.
9 10 11 12 13 14 15 16 |
# File 'lib/label.rb', line 9 def parse(label) m = match = label.match(/^([a-z])(\d+)\s*-?\s*(.*)$/i) return nil unless m letter, digits, text = match.captures number = digits.to_i [letter, number, text] end |
#to_str ⇒ Object
34 35 36 |
# File 'lib/label.rb', line 34 def to_str format('%<l>s%<n>d - %<d>s', { l: @letter, n: @number, d: @description }) end |