Class: Pubid::Core::Identifier

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher:, number:, copublisher: nil, part: nil, type: nil, year: nil, edition: nil, language: nil, amendments: nil, corrigendums: nil) ⇒ Identifier

Creates new identifier from options provided:

Parameters:

  • publisher (String)

    document’s publisher, eg. “ISO”

  • copublisher (String, Array<String>) (defaults to: nil)

    document’s copublisher, eg. “IEC”

  • number (Integer)

    document’s number, eg. “1234”

  • part (String) (defaults to: nil)

    document’s part and subparts, eg. “1”, “1-1A”, “2-3D”

  • type (String) (defaults to: nil)

    document’s type, eg. “TR”, “TS”

  • year (Integer) (defaults to: nil)

    document’s year, eg. “2020”

  • edition (Integer) (defaults to: nil)

    document’s edition, eg. “1”

  • language (String) (defaults to: nil)

    document’s translation language (available languages: “ru”, “fr”, “en”, “ar”)

  • amendments (Array<Amendment>) (defaults to: nil)

    document’s amendments

  • corrigendums (Array<Corrigendum>) (defaults to: nil)

    document’s corrigendums

See Also:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pubid/core/identifier.rb', line 21

def initialize(publisher:, number:, copublisher: nil, part: nil, type: nil,
               year: nil, edition: nil, language: nil, amendments: nil,
               corrigendums: nil)
  if amendments
    @amendments = if amendments.is_a?(Array)
                    amendments.map do |amendment|
                      self.class.get_amendment_class.new(**amendment)
                    end
                  else
                    [self.class.get_amendment_class.new(**amendments)]
                  end
  end
  if corrigendums
    @corrigendums = if corrigendums.is_a?(Array)
                      corrigendums.map do |corrigendum|
                        self.class.get_corrigendum_class.new(**corrigendum)
                      end
                    else
                      [self.class.get_corrigendum_class.new(**corrigendums)]
                    end
  end

  @publisher = publisher.to_s
  @number = number
  @copublisher = copublisher if copublisher
  @part = part.to_s if part
  @type = type.to_s if type
  @year = year.to_i if year
  @edition = edition.to_i if edition
  @language = language.to_s if language
end

Instance Attribute Details

#amendmentsObject

Returns the value of attribute amendments.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def amendments
  @amendments
end

#copublisherObject

Returns the value of attribute copublisher.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def copublisher
  @copublisher
end

#corrigendumsObject

Returns the value of attribute corrigendums.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def corrigendums
  @corrigendums
end

#editionObject

Returns the value of attribute edition.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def edition
  @edition
end

#languageObject

Returns the value of attribute language.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def language
  @language
end

#numberObject

Returns the value of attribute number.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def number
  @number
end

#partObject

Returns the value of attribute part.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def part
  @part
end

#publisherObject

Returns the value of attribute publisher.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def publisher
  @publisher
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def type
  @type
end

#yearObject

Returns the value of attribute year.



3
4
5
# File 'lib/pubid/core/identifier.rb', line 3

def year
  @year
end

Class Method Details

.get_amendment_classObject



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

def get_amendment_class
  Amendment
end

.get_corrigendum_classObject



104
105
106
# File 'lib/pubid/core/identifier.rb', line 104

def get_corrigendum_class
  Corrigendum
end

.get_renderer_classObject



108
109
110
# File 'lib/pubid/core/identifier.rb', line 108

def get_renderer_class
  Renderer::Base
end

.get_transformer_classObject



112
113
114
# File 'lib/pubid/core/identifier.rb', line 112

def get_transformer_class
  Transformer
end

.get_update_codesHash?

Returns replacement patterns.

Returns:

  • (Hash, nil)

    replacement patterns



117
118
119
# File 'lib/pubid/core/identifier.rb', line 117

def get_update_codes
  nil
end

.parse(code_or_params) ⇒ Pubid::Core::Identifier

Parses given identifier

Parameters:

  • code_or_params (String, Hash)

    code or hash from parser eg. “ISO 1234”, { }

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pubid/core/identifier.rb', line 72

def parse(code_or_params)
  params = code_or_params.is_a?(String) ?
             get_parser_class.new.parse(update_old_code(code_or_params)) : code_or_params
  # Parslet returns an array when match any copublisher
  # otherwise it's hash
  if params.is_a?(Array)
    new(
      **(
        params.inject({}) do |r, i|
          result = r
          i.map {|k, v| get_transformer_class.new.apply(k => v).to_a.first }.each do |k, v|
            result = result.merge(k => r.key?(k) ? [v, r[k]] : v)
          end
          result
        end
      )
    )
  else
    new(**params.map do |k, v|
      get_transformer_class.new.apply(k => v).to_a.first
    end.to_h)
  end
  # merge values repeating keys into array (for copublishers)

rescue Parslet::ParseFailed => failure
  raise Errors::ParseError, "#{failure.message}\ncause: #{failure.parse_failure_cause.ascii_tree}"
end

.update_old_code(code) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/pubid/core/identifier.rb', line 121

def update_old_code(code)
  return code unless get_update_codes

  get_update_codes.each do |from, to|
    code = code.gsub(from.match?(/^\/.*\/$/) ? Regexp.new(from[1..-2]) : /^#{Regexp.escape(from)}$/, to)
  end
  code
end

Instance Method Details

#get_paramsObject



58
59
60
# File 'lib/pubid/core/identifier.rb', line 58

def get_params
  instance_variables.map { |var| [var.to_s.gsub("@", "").to_sym, instance_variable_get(var)] }.to_h
end

#to_sObject

Render identifier using default renderer



63
64
65
# File 'lib/pubid/core/identifier.rb', line 63

def to_s
  self.class.get_renderer_class.new(get_params).render
end

#urnString

Returns Rendered URN identifier.

Returns:

  • (String)

    Rendered URN identifier



54
55
56
# File 'lib/pubid/core/identifier.rb', line 54

def urn
  Renderer::Urn.new(get_params).render
end