Class: RelatonBib::DocumentIdentifier

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

Overview

Document identifier.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ DocumentIdentifier

Returns a new instance of DocumentIdentifier.

Parameters:

  • id (String)
  • type (String, nil)
  • scope (String, nil)
  • primary (Bolean, nil)
  • language (String, nil)


18
19
20
21
22
23
24
25
# File 'lib/relaton_bib/document_identifier.rb', line 18

def initialize(**args)
  @id       = args[:id]
  @type     = args[:type]
  @scope    = args[:scope]
  @primary  = args[:primary]
  @language = args[:language]
  @script   = args[:script]
end

Instance Attribute Details

#idString (readonly)

Returns:

  • (String)


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

def id
  @id
end

#languageString? (readonly)

Returns:

  • (String, nil)


8
9
10
# File 'lib/relaton_bib/document_identifier.rb', line 8

def language
  @language
end

#primaryObject (readonly)

Parameters:

  • type (Boolean, nil)


11
12
13
# File 'lib/relaton_bib/document_identifier.rb', line 11

def primary
  @primary
end

#scopeString? (readonly)

Returns:

  • (String, nil)


8
9
10
# File 'lib/relaton_bib/document_identifier.rb', line 8

def scope
  @scope
end

#scriptString? (readonly)

Returns:

  • (String, nil)


8
9
10
# File 'lib/relaton_bib/document_identifier.rb', line 8

def script
  @script
end

#typeString? (readonly)

Returns:

  • (String, nil)


8
9
10
# File 'lib/relaton_bib/document_identifier.rb', line 8

def type
  @type
end

Instance Method Details

#all_partsObject



45
46
47
48
49
50
51
# File 'lib/relaton_bib/document_identifier.rb', line 45

def all_parts
  if type == "URN"
    @id.sub!(%r{^(urn:iec:std(?::[^:]*){4}).*}, '\1:ser')
  else
    @id += " (all parts)"
  end
end

#remove_dateObject



36
37
38
39
40
41
42
43
# File 'lib/relaton_bib/document_identifier.rb', line 36

def remove_date
  case @type
  when "Chinese Standard" then @id.sub!(/-[12]\d\d\d/, "")
  when "URN"
    @id.sub!(/^(urn:iec:std:[^:]+:[^:]+:)[^:]*/, '\1')
  else @id.sub!(/:[12]\d\d\d/, "")
  end
end

#remove_partObject

in docid manipulations, assume ISO as the default: id-part:year



28
29
30
31
32
33
34
# File 'lib/relaton_bib/document_identifier.rb', line 28

def remove_part
  case @type
  when "Chinese Standard" then @id.sub!(/\.\d+/, "")
  when "URN" then remove_urn_part
  else @id.sub!(/-[^:]+/, "")
  end
end

#to_asciibib(prefix = "", count = 1) ⇒ String

Parameters:

  • prefix (String) (defaults to: "")
  • count (Integer) (defaults to: 1)

    number of docids

Returns:

  • (String)


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

def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
  pref = prefix.empty? ? prefix : "#{prefix}."
  return "#{pref}docid:: #{id}\n" unless type || scope

  out = count > 1 ? "#{pref}docid::\n" : ""
  out += "#{pref}docid.type:: #{type}\n" if type
  out += "#{pref}docid.scope:: #{scope}\n" if scope
  out += "#{pref}docid.primary:: #{primary}\n" if primary
  out += "#{pref}docid.language:: #{language}\n" if language
  out += "#{pref}docid.script:: #{script}\n" if script
  out + "#{pref}docid.id:: #{id}\n"
end

#to_hashHash

Returns:

  • (Hash)


73
74
75
76
77
78
79
80
81
# File 'lib/relaton_bib/document_identifier.rb', line 73

def to_hash # rubocop:disable Metrics/AbcSize
  hash = { "id" => id }
  hash["type"] = type if type
  hash["scope"] = scope if scope
  hash["primary"] = primary if primary
  hash["language"] = language if language
  hash["script"] = script if script
  hash
end

#to_xml(**opts) ⇒ Object

Add docidentifier xml element

Parameters:

  • opts (Hash)

Options Hash (**opts):

  • :builder (Nokogiri::XML::Builder)

    XML builder

  • :lang (String)

    language



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/relaton_bib/document_identifier.rb', line 59

def to_xml(**opts) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  lid = if type == "URN" && opts[:lang]
          id.sub %r{(?<=:)(?:\w{2},)*?(#{opts[:lang]})(?:,\w{2})*}, '\1'
        else id
        end
  element = opts[:builder].docidentifier { |b| b.parent.inner_html = lid }
  element[:type] = type if type
  element[:scope] = scope if scope
  element[:primary] = primary if primary
  element[:language] = language if language
  element[:script] = script if script
end