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
-
.array_to_hash(params) ⇒ Object
Converts array of hashes into single hash array like [{ publisher: “ISO” }, { number: 1 }] to hash { publisher: “ISO”, number: 1 }.
- .get_amendment_class ⇒ Object
- .get_corrigendum_class ⇒ Object
- .get_renderer_class ⇒ Object
- .get_transformer_class ⇒ Object
-
.get_update_codes ⇒ Hash?
Replacement patterns.
-
.parse(code_or_params) ⇒ Pubid::Core::Identifier
Parses given identifier.
-
.transform(params) ⇒ Object
Transform parameters hash or array or hashes to identifier.
- .update_old_code(code) ⇒ Object
Instance Method Summary collapse
- #get_params ⇒ Object
-
#initialize(publisher:, number:, copublisher: nil, part: nil, type: nil, year: nil, edition: nil, language: nil, amendments: nil, corrigendums: nil) ⇒ Identifier
constructor
Creates new identifier from options provided:.
-
#to_s ⇒ Object
Render identifier using default renderer.
-
#urn ⇒ String
Rendered URN identifier.
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:
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 52 53 |
# 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 = amendments.map do |amendment| if amendment.is_a?(Hash) self.class.get_transformer_class.new.apply(:amendments => [amendment])[:amendments].first else amendment end end end if corrigendums @corrigendums = corrigendums.map do |corrigendum| if corrigendum.is_a?(Hash) self.class.get_transformer_class.new.apply(:corrigendums => [corrigendum])[:corrigendums].first else corrigendum end 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
#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
.array_to_hash(params) ⇒ Object
Converts array of hashes into single hash array like [{ publisher: “ISO” }, { number: 1 }] to hash { publisher: “ISO”, number: 1 }
85 86 87 88 89 90 91 92 93 |
# File 'lib/pubid/core/identifier.rb', line 85 def array_to_hash(params) params.inject({}) do |r, i| result = r i.each do |k, v| result = result.merge(k => r.key?(k) ? [v, r[k]].flatten : v) end result end end |
.get_amendment_class ⇒ Object
110 111 112 |
# File 'lib/pubid/core/identifier.rb', line 110 def get_amendment_class Amendment end |
.get_corrigendum_class ⇒ Object
114 115 116 |
# File 'lib/pubid/core/identifier.rb', line 114 def get_corrigendum_class Corrigendum end |
.get_renderer_class ⇒ Object
118 119 120 |
# File 'lib/pubid/core/identifier.rb', line 118 def get_renderer_class Renderer::Base end |
.get_transformer_class ⇒ Object
122 123 124 |
# File 'lib/pubid/core/identifier.rb', line 122 def get_transformer_class Transformer end |
.get_update_codes ⇒ Hash?
Returns replacement patterns.
127 128 129 |
# File 'lib/pubid/core/identifier.rb', line 127 def get_update_codes nil end |
.parse(code_or_params) ⇒ Pubid::Core::Identifier
Parses given identifier
74 75 76 77 78 79 80 |
# File 'lib/pubid/core/identifier.rb', line 74 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 transform(params.is_a?(Array) ? array_to_hash(params) : params) rescue Parslet::ParseFailed => failure raise Errors::ParseError, "#{failure.}\ncause: #{failure.parse_failure_cause.ascii_tree}" end |
.transform(params) ⇒ Object
Transform parameters hash or array or hashes to identifier
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pubid/core/identifier.rb', line 96 def transform(params) # run transform through each element, # like running transformer.apply(number: 1) and transformer.apply(year: 1999) # instead of running transformer on whole hash, like running transformer.apply({ number: 1, year: 1999 }) # where rule for number or year only will be not applied # transformation only applied to rules matching the whole hash identifier_params = params.map do |k, v| get_transformer_class.new.apply(k => v).to_a.first end.to_h new(**identifier_params) end |
.update_old_code(code) ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/pubid/core/identifier.rb', line 131 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_params ⇒ Object
60 61 62 |
# File 'lib/pubid/core/identifier.rb', line 60 def get_params instance_variables.map { |var| [var.to_s.gsub("@", "").to_sym, instance_variable_get(var)] }.to_h end |
#to_s ⇒ Object
Render identifier using default renderer
65 66 67 |
# File 'lib/pubid/core/identifier.rb', line 65 def to_s self.class.get_renderer_class.new(get_params).render end |