Class: Label

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

Overview

Label = Letter + Number [+ Description]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Label

Returns a new instance of Label.

Raises:



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

#descriptionObject

Returns the value of attribute description.



5
6
7
# File 'lib/label.rb', line 5

def description
  @description
end

#letterObject

Returns the value of attribute letter.



5
6
7
# File 'lib/label.rb', line 5

def letter
  @letter
end

#numberObject

Returns the value of attribute number.



5
6
7
# File 'lib/label.rb', line 5

def number
  @number
end

Instance Method Details

#nameObject

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_strObject



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