Class: Pubid::Iso::Identifier

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

Direct Known Subclasses

French, Russian, Urn

Constant Summary collapse

LANGUAGES =
{
  "ru" => "R",
  "fr" => "F",
  "en" => "E",
  "ar" => "A",
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Identifier

Returns a new instance of Identifier.



18
19
20
# File 'lib/pubid/iso/identifier.rb', line 18

def initialize(**opts)
  opts.each { |key, value| send("#{key}=", value.is_a?(Array) && value || value.to_s) }
end

Instance Attribute Details

#amendmentObject

Returns the value of attribute amendment.



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

def amendment
  @amendment
end

#amendment_numberObject

Returns the value of attribute amendment_number.



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

def amendment_number
  @amendment_number
end

#amendment_stageObject

Returns the value of attribute amendment_stage.



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

def amendment_stage
  @amendment_stage
end

#amendment_versionObject

Returns the value of attribute amendment_version.



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

def amendment_version
  @amendment_version
end

#copublisherObject

Returns the value of attribute copublisher.



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

def copublisher
  @copublisher
end

#corrigendumObject

Returns the value of attribute corrigendum.



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

def corrigendum
  @corrigendum
end

#corrigendum_numberObject

Returns the value of attribute corrigendum_number.



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

def corrigendum_number
  @corrigendum_number
end

#corrigendum_stageObject

Returns the value of attribute corrigendum_stage.



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

def corrigendum_stage
  @corrigendum_stage
end

#corrigendum_versionObject

Returns the value of attribute corrigendum_version.



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

def corrigendum_version
  @corrigendum_version
end

#editionObject

Returns the value of attribute edition.



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

def edition
  @edition
end

#iterationObject

Returns the value of attribute iteration.



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

def iteration
  @iteration
end

#joint_documentObject

Returns the value of attribute joint_document.



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

def joint_document
  @joint_document
end

#language(with_language_code = :iso) ⇒ Object

Returns the value of attribute language.



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

def language
  @language
end

#numberObject

Returns the value of attribute number.



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

def number
  @number
end

#partObject

Returns the value of attribute part.



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

def part
  @part
end

#publisherObject

Returns the value of attribute publisher.



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

def publisher
  @publisher
end

#scnumberObject

Returns the value of attribute scnumber.



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

def scnumber
  @scnumber
end

#sctypeObject

TC 184/SC/WG 4 - no wg number TC 184/SC 4/WG 12 - separate sc and wg number



97
98
99
# File 'lib/pubid/iso/identifier.rb', line 97

def sctype
  @sctype
end

#stageObject

Returns the value of attribute stage.



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

def stage
  @stage
end

#substageObject

Returns the value of attribute substage.



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

def substage
  @substage
end

#supplementsObject

Returns the value of attribute supplements.



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

def supplements
  @supplements
end

#tcnumberObject

Returns the value of attribute tcnumber.



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

def tcnumber
  @tcnumber
end

#tctypeObject

Returns the value of attribute tctype.



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

def tctype
  @tctype
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#urn_stageObject

Returns the value of attribute urn_stage.



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

def urn_stage
  @urn_stage
end

#wgnumberObject

Returns the value of attribute wgnumber.



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

def wgnumber
  @wgnumber
end

#wgtypeObject

Returns the value of attribute wgtype.



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

def wgtype
  @wgtype
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Class Method Details

.parse(code_or_params) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pubid/iso/identifier.rb', line 39

def self.parse(code_or_params)
  params = code_or_params.is_a?(String) ? Parser.new.parse(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| Transformer.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|
      Transformer.new.apply(k => v).to_a.first
    end.to_h)
  end
  # merge values repeating keys into array (for copublishers)


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

.parse_from_title(title) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/pubid/iso/identifier.rb', line 30

def self.parse_from_title(title)
  title.split.reverse.inject(title) do |acc, part|
    return parse(acc)
  rescue Pubid::Iso::Errors::ParseError
    # delete parts from the title until it's parseable
    acc.reverse.sub(part.reverse, "").reverse.strip
  end
end

Instance Method Details

#get_paramsObject



22
23
24
# File 'lib/pubid/iso/identifier.rb', line 22

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

#identifier(with_date, with_language_code) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/pubid/iso/identifier.rb', line 80

def identifier(with_date, with_language_code)
  if @tctype
    "#{originator} #{tctype} #{tcnumber}#{sctype}#{wgtype} N#{number}"
  else
    "#{originator}#{type}#{stage} #{number}#{part}#{iteration}"\
    "#{with_date && rendered_year || ''}#{edition}#{supplements}#{language(with_language_code)}"
  end
end

#originatorObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pubid/iso/identifier.rb', line 123

def originator
  if copublisher
    # @copublisher = [@copublisher] unless @copublisher.is_a?(Array)
    # @copublisher.map! { |copublisher| copublisher.sub("IEC", "CEI") } if @french
    publisher + copublisher.map(&:to_s).sort.map do |copublisher|
      "/#{copublisher.gsub('-', '/')}"
    end.join
  else
    publisher
  end
end

#rendered_yearObject



143
144
145
# File 'lib/pubid/iso/identifier.rb', line 143

def rendered_year
  @year && ":#{@year}"
end

#to_s(lang: nil, with_date: true, with_language_code: :iso) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pubid/iso/identifier.rb', line 68

def to_s(lang: nil, with_date: true, with_language_code: :iso)
  # @pubid_language = lang
  case lang
  when :french
    French.new(**get_params)
  when :russian
    Russian.new(**get_params)
  else
    self
  end.identifier(with_date, with_language_code) + (@joint_document && "|#{@joint_document}").to_s
end

#urnObject



26
27
28
# File 'lib/pubid/iso/identifier.rb', line 26

def urn
  Urn.new(**get_params)
end