Class: RelatonW3c::PubId

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

Constant Summary collapse

PARTS =
%i[code stage type year date suff].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**parts) ⇒ PubId

Returns a new instance of PubId.



7
8
9
# File 'lib/relaton_w3c/pubid.rb', line 7

def initialize(**parts)
  PARTS.each { |part| send "#{part}=", parts[part] }
end

Class Method Details

.parse(docnumber) ⇒ RelatonW3c::PubId

Parse document identifier.

Parameters:

  • docnumber (String)

    document identifier

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/relaton_w3c/pubid.rb', line 18

def self.parse(docnumber) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  %r{
    (?:^|/)(?:(?:(?<stage>WD|CRD|CR|PR|PER|REC|SPSD|OBSL|RET)|(?<type>D?NOTE|TR))[\s/-])?
    (?<code>\w+(?:[+-][\w.]+)*?)
    (?:-(?<year>(?:18|19|20)\d{2}))?
    (?:-(?<date>\d{8}|\d{6}|\d{4}))?
    (?:/(?<suff>\w+))?(?:$|/)
  }xi =~ docnumber
  entry = { code: code }
  entry[:stage] = stage if stage
  entry[:type] = type if type && type != "TR"
  entry[:year] = year if year
  entry[:date] = date if date
  entry[:suff] = suff if suff
  new(**entry)
end

Instance Method Details

#==(other) ⇒ Boolean

Compare document identifiers against Hash ID representation.

Parameters:

  • other (Hash)

    hash of document identifier parts

Returns:

  • (Boolean)

    true if document identifiers are same



42
43
44
45
46
# File 'lib/relaton_w3c/pubid.rb', line 42

def ==(other) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  other[:code].casecmp?(code) && other[:stage] == stage && other[:type] == type &&
    (year.nil? || other[:year] == year) && (date.nil? || other[:date] == date) &&
    (suff.nil? || other[:suff]&.casecmp?(suff))
end

#to_hashHash

Convert docidentifier identifier to hash.

Returns:

  • (Hash)

    hash of docidentifier parts



53
54
55
# File 'lib/relaton_w3c/pubid.rb', line 53

def to_hash
  PARTS.each_with_object({}) { |part, hash| hash[part] = send part if send part }
end