Class: RelatonItu::Pubid

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

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix:, sector:, code:, **args) ⇒ Pubid

Create a new ITU publication identifier.

Parameters:

  • prefix (String)
  • sector (String)
  • type (String, nil)
  • code (String)
  • suppl (String, nil)

    number

  • year (String, nil)
  • month (String, nil)
  • amd (String, nil)

    amendment number



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/relaton_itu/pubid.rb', line 52

def initialize(prefix:, sector:, code:, **args)
  @prefix = prefix
  @sector = sector
  @type = args[:type]
  @code, year, month = date_from_code code
  @suppl = args[:suppl]
  @annex = args[:annex]
  @year = args[:year] || year
  @month = roman_to_2digit args[:month] || month
  @amd = args[:amd]
end

Instance Attribute Details

#amdObject

Returns the value of attribute amd.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def amd
  @amd
end

#annexObject

Returns the value of attribute annex.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def annex
  @annex
end

#codeObject

Returns the value of attribute code.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def code
  @code
end

#monthObject

Returns the value of attribute month.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def month
  @month
end

#prefixObject

Returns the value of attribute prefix.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def prefix
  @prefix
end

#sectorObject

Returns the value of attribute sector.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def sector
  @sector
end

#supplObject

Returns the value of attribute suppl.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def suppl
  @suppl
end

#typeObject

Returns the value of attribute type.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def type
  @type
end

#yearObject

Returns the value of attribute year.



38
39
40
# File 'lib/relaton_itu/pubid.rb', line 38

def year
  @year
end

Class Method Details

.parse(id) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/relaton_itu/pubid.rb', line 64

def self.parse(id)
  id_parts = Parser.new.parse(id).to_h.transform_values(&:to_s)
  new(**id_parts)
rescue Parslet::ParseFailed => e
  Util.warn "WARNING: `#{id}` is invalid ITU publication identifier \n" \
            "#{e.parse_failure_cause.ascii_tree}"
  raise e
end

Instance Method Details

#===(other, ignore_args = []) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/relaton_itu/pubid.rb', line 99

def ===(other, ignore_args = [])
  hash = to_h with_type: false
  other_hash = other.to_h with_type: false
  hash.delete(:month)
  other_hash.delete(:month)
  hash.delete(:year) if ignore_args.include?(:year)
  other_hash.delete(:year) unless hash[:year]
  hash == other_hash
end

#to_h(with_type: true) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



73
74
75
76
77
78
79
80
81
82
# File 'lib/relaton_itu/pubid.rb', line 73

def to_h(with_type: true) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  hash = { prefix: prefix, sector: sector, code: code }
  hash[:type] = type if type && with_type
  hash[:suppl] = suppl if suppl
  hash[:annex] = annex if annex
  hash[:year] = year if year
  hash[:month] = month if month
  hash[:amd] = amd if amd
  hash
end

#to_refObject



84
85
86
# File 'lib/relaton_itu/pubid.rb', line 84

def to_ref
  to_s ref: true
end

#to_s(ref: false) ⇒ Object

rubocop:disable Metrics/AbcSize



88
89
90
91
92
93
94
95
96
97
# File 'lib/relaton_itu/pubid.rb', line 88

def to_s(ref: false) # rubocop:disable Metrics/AbcSize
  s = "#{prefix}-#{sector}"
  s << " #{type}" if type && !ref
  s << " #{code}"
  s << " Suppl. #{suppl}" if suppl
  s << " Annex #{annex}" if annex
  s << date_to_s
  s << " Amd #{amd}" if amd
  s
end