Class: Iev::IevCode

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/iev/iev_code.rb

Overview

Immutable value object that decomposes an IEV concept code into its structural parts: area code, section code, and number.

The IEV code format is AAA-BB-CC where:

AAA = area code (e.g. "103")
BB  = section sub-code (e.g. "01")
CC  = concept number (e.g. "02")

Examples:

Full concept code

code = Iev::IevCode.new("103-01-02")
code.area_code    #=> "103"
code.section_code #=> "103-01"
code.number       #=> "02"
code.area_uri     #=> "area-103"
code.section_uri  #=> "section-103-01"

Section code (no concept number)

code = Iev::IevCode.new("103-01")
code.area_code    #=> "103"
code.section_code #=> "103-01"
code.number       #=> nil
code.section_uri  #=> "section-103-01"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ IevCode

Returns a new instance of IevCode.

Parameters:

  • code (#to_s)

    IEV reference, e.g. “103-01-02”



33
34
35
36
37
38
39
40
# File 'lib/iev/iev_code.rb', line 33

def initialize(code)
  @raw = code.to_s
  parts = @raw.split("-")
  @area_code = parts[0]
  @section_code = parts.length >= 2 ? "#{parts[0]}-#{parts[1]}" : nil
  @number = parts.length >= 3 ? parts[2] : nil
  freeze
end

Instance Attribute Details

#area_codeObject (readonly)

Returns the value of attribute area_code.



30
31
32
# File 'lib/iev/iev_code.rb', line 30

def area_code
  @area_code
end

#numberObject (readonly)

Returns the value of attribute number.



30
31
32
# File 'lib/iev/iev_code.rb', line 30

def number
  @number
end

#rawObject (readonly)

Returns the value of attribute raw.



30
31
32
# File 'lib/iev/iev_code.rb', line 30

def raw
  @raw
end

#section_codeObject (readonly)

Returns the value of attribute section_code.



30
31
32
# File 'lib/iev/iev_code.rb', line 30

def section_code
  @section_code
end

Class Method Details

.parse(code) ⇒ IevCode?

Safe constructor that returns nil for codes that don’t parse.

Parameters:

Returns:



74
75
76
77
78
# File 'lib/iev/iev_code.rb', line 74

def self.parse(code)
  new(code)
rescue ArgumentError
  nil
end

Instance Method Details

#<=>(other) ⇒ Object



67
68
69
# File 'lib/iev/iev_code.rb', line 67

def <=>(other)
  to_s <=> other.to_s
end

#==(other) ⇒ Object Also known as: eql?



58
59
60
# File 'lib/iev/iev_code.rb', line 58

def ==(other)
  other.is_a?(self.class) && raw == other.raw
end

#area_uriObject



42
43
44
# File 'lib/iev/iev_code.rb', line 42

def area_uri
  "area-#{area_code}"
end

#hashObject



63
64
65
# File 'lib/iev/iev_code.rb', line 63

def hash
  raw.hash
end

#section_uriObject



46
47
48
# File 'lib/iev/iev_code.rb', line 46

def section_uri
  "section-#{section_code}" if section_code
end

#to_sObject



50
51
52
# File 'lib/iev/iev_code.rb', line 50

def to_s
  @raw
end

#to_strObject



54
55
56
# File 'lib/iev/iev_code.rb', line 54

def to_str
  @raw
end