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

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

    amendment number

  • amd_date (String, nil)

    amendment



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/relaton_itu/pubid.rb', line 62

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

Instance Attribute Details

#amdObject

Returns the value of attribute amd.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def amd
  @amd
end

#amd_dateObject

Returns the value of attribute amd_date.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def amd_date
  @amd_date
end

#annexObject

Returns the value of attribute annex.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def annex
  @annex
end

#codeObject

Returns the value of attribute code.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def code
  @code
end

#dayObject

Returns the value of attribute day.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def day
  @day
end

#monthObject

Returns the value of attribute month.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def month
  @month
end

#prefixObject

Returns the value of attribute prefix.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def prefix
  @prefix
end

#sectorObject

Returns the value of attribute sector.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def sector
  @sector
end

#supplObject

Returns the value of attribute suppl.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def suppl
  @suppl
end

#typeObject

Returns the value of attribute type.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def type
  @type
end

#versionObject

Returns the value of attribute version.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def version
  @version
end

#yearObject

Returns the value of attribute year.



45
46
47
# File 'lib/relaton_itu/pubid.rb', line 45

def year
  @year
end

Class Method Details

.parse(id) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/relaton_itu/pubid.rb', line 77

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

Instance Method Details

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/relaton_itu/pubid.rb', line 117

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

#to_h(with_type: true) ⇒ Object

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



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

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[:version] = version if version
  hash[:year] = year if year
  hash[:month] = month if month
  hash[:day] = day if day
  hash[:amd] = amd if amd
  hash[:amd_date] = amd_date if amd_date
  hash
end

#to_refObject



100
101
102
# File 'lib/relaton_itu/pubid.rb', line 100

def to_ref
  to_s ref: true
end

#to_s(ref: false) ⇒ Object

rubocop:disable Metrics/AbcSize



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/relaton_itu/pubid.rb', line 104

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 << " (V#{version})" if version
  s << date_to_s
  s << " Amd #{amd}" if amd
  s << " (#{amd_date})" if amd_date
  s
end