Class: Pubid::Core::Identifier
- Inherits:
-
Object
- Object
- Pubid::Core::Identifier
- Defined in:
- lib/pubid/core/identifier.rb
Instance Attribute Summary collapse
-
#amendments ⇒ Object
Returns the value of attribute amendments.
-
#copublisher ⇒ Object
Returns the value of attribute copublisher.
-
#corrigendums ⇒ Object
Returns the value of attribute corrigendums.
-
#edition ⇒ Object
Returns the value of attribute edition.
-
#language ⇒ Object
Returns the value of attribute language.
-
#number ⇒ Object
Returns the value of attribute number.
-
#part ⇒ Object
Returns the value of attribute part.
-
#publisher ⇒ Object
Returns the value of attribute publisher.
-
#type ⇒ Object
Returns the value of attribute type.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
- .get_amendment_class ⇒ Object
- .get_corrigendum_class ⇒ Object
- .get_renderer_class ⇒ Object
- .get_transformer_class ⇒ Object
- .parse(code_or_params) ⇒ Object
Instance Method Summary collapse
- #get_params ⇒ Object
-
#initialize(amendments: nil, corrigendums: nil, **opts) ⇒ Identifier
constructor
A new instance of Identifier.
- #to_s ⇒ Object
- #urn ⇒ Object
Constructor Details
#initialize(amendments: nil, corrigendums: nil, **opts) ⇒ Identifier
Returns a new instance of Identifier.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pubid/core/identifier.rb', line 7 def initialize(amendments: nil, corrigendums: nil, **opts) 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 opts.each { |key, value| send("#{key}=", value.is_a?(Parslet::Slice) && value.to_s || value) } end |
Instance Attribute Details
#amendments ⇒ Object
Returns the value of attribute amendments.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def amendments @amendments end |
#copublisher ⇒ Object
Returns the value of attribute copublisher.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def copublisher @copublisher end |
#corrigendums ⇒ Object
Returns the value of attribute corrigendums.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def corrigendums @corrigendums end |
#edition ⇒ Object
Returns the value of attribute edition.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def edition @edition end |
#language ⇒ Object
Returns the value of attribute language.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def language @language end |
#number ⇒ Object
Returns the value of attribute number.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def number @number end |
#part ⇒ Object
Returns the value of attribute part.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def part @part end |
#publisher ⇒ Object
Returns the value of attribute publisher.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def publisher @publisher end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/pubid/core/identifier.rb', line 3 def type @type end |
#year ⇒ Object
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_class ⇒ Object
43 44 45 |
# File 'lib/pubid/core/identifier.rb', line 43 def get_amendment_class Amendment end |
.get_corrigendum_class ⇒ Object
47 48 49 |
# File 'lib/pubid/core/identifier.rb', line 47 def get_corrigendum_class Corrigendum end |
.get_renderer_class ⇒ Object
51 52 53 |
# File 'lib/pubid/core/identifier.rb', line 51 def get_renderer_class Renderer::Base end |
.get_transformer_class ⇒ Object
55 56 57 |
# File 'lib/pubid/core/identifier.rb', line 55 def get_transformer_class Transformer end |
.parse(code_or_params) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pubid/core/identifier.rb', line 59 def parse(code_or_params) params = code_or_params.is_a?(String) ? get_parser_class.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| 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 Pubid::Iso::Errors::ParseError, "#{failure.message}\ncause: #{failure.parse_failure_cause.ascii_tree}" end |
Instance Method Details
#get_params ⇒ Object
34 35 36 |
# File 'lib/pubid/core/identifier.rb', line 34 def get_params instance_variables.map { |var| [var.to_s.gsub("@", "").to_sym, instance_variable_get(var)] }.to_h end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/pubid/core/identifier.rb', line 38 def to_s self.class.get_renderer_class.new(get_params).render end |
#urn ⇒ Object
30 31 32 |
# File 'lib/pubid/core/identifier.rb', line 30 def urn Renderer::Urn.new(get_params).render end |