pubid-core 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1b28bf047f8339b5d96fb9ef3ea58aede3156ef1f2379e360afcdb96137c5f3
4
- data.tar.gz: 557dd8e1e246b9caf278abcf439001696890f8594116232be357f614b1ae93bd
3
+ metadata.gz: 572e76f914359171e1ebd2e4894cd02fcfcc519311f687a5f616b701bf177a44
4
+ data.tar.gz: 8e0328ff09f797795747a1df3de3484a37dc717c2e657e782c5d4310784044d0
5
5
  SHA512:
6
- metadata.gz: 78378ed3583f405d843f655ae5e9aee45e071506db0c6a88705ac65f28c2f69b6fcd3304e87578f14742f2fdcca16a32b320e0b2ea51851438115e1dc7aae66d
7
- data.tar.gz: a8422326efe996a0d00aea25b3eda737e598a3e32c7136c47a4112cc57ef1576cc9f967b05b260b8851d2c76290fa75418f0b4f4274be0fbe52eb32911b85e9f
6
+ metadata.gz: 8a62af066a0517b83ba5440f26437bdea0251f485de3d3062c38921fbe9271490aec6c90aa2bacdda7ed5772f44113ca248dbd68035b1223c83a0a075476f4ae
7
+ data.tar.gz: ab780a3f9b8e17abbb3554615ecb7f2c642573c6399fed11c6d93e76aa6459cc9980f433b9d4f904da306a3d6afbefede51b4c3021fa9ce70905be424c17f368
@@ -0,0 +1,14 @@
1
+ module Pubid::Core
2
+ class Amendment < Supplement
3
+
4
+ def render_pubid
5
+ stage = render_pubid_stage
6
+ stage += " " unless stage.size == 0
7
+ "/#{stage}Amd #{render_pubid_number}"
8
+ end
9
+
10
+ def render_urn
11
+ "#{render_urn_stage}:amd#{render_urn_number}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Pubid::Core
2
+ class Corrigendum < Supplement
3
+
4
+ def render_pubid
5
+ stage = render_pubid_stage
6
+ stage += " " unless stage.size == 0
7
+ "/#{stage}Cor #{render_pubid_number}"
8
+ end
9
+
10
+ def render_urn
11
+ "#{render_urn_stage}:cor#{render_urn_number}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Pubid::Core
2
+ module Errors
3
+ class ParseError < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,79 @@
1
+ module Pubid::Core
2
+ class Identifier
3
+ attr_accessor :number, :publisher, :copublisher, :part,
4
+ :type, :year, :edition, :language, :amendments,
5
+ :corrigendums
6
+
7
+ def initialize(amendments: nil, corrigendums: nil, **opts)
8
+ if amendments
9
+ @amendments = if amendments.is_a?(Array)
10
+ amendments.map do |amendment|
11
+ self.class.get_amendment_class.new(**amendment)
12
+ end
13
+ else
14
+ [self.class.get_amendment_class.new(**amendments)]
15
+ end
16
+ end
17
+ if corrigendums
18
+ @corrigendums = if corrigendums.is_a?(Array)
19
+ corrigendums.map do |corrigendum|
20
+ self.class.get_corrigendum_class.new(**corrigendum)
21
+ end
22
+ else
23
+ [self.class.get_corrigendum_class.new(**corrigendums)]
24
+ end
25
+ end
26
+
27
+ opts.each { |key, value| send("#{key}=", value.is_a?(Array) && value || value.to_s) }
28
+ end
29
+
30
+ def get_params
31
+ instance_variables.map { |var| [var.to_s.gsub("@", "").to_sym, instance_variable_get(var)] }.to_h
32
+ end
33
+
34
+ def to_s
35
+ self.class.get_renderer.new(get_params).render
36
+ end
37
+
38
+ class << self
39
+ def get_amendment_class
40
+ Amendment
41
+ end
42
+
43
+ def get_corrigendum_class
44
+ Corrigendum
45
+ end
46
+
47
+ def get_renderer
48
+ Renderer
49
+ end
50
+
51
+ def parse(code_or_params)
52
+ params = code_or_params.is_a?(String) ? get_parser_class.new.parse(code_or_params) : code_or_params
53
+ # Parslet returns an array when match any copublisher
54
+ # otherwise it's hash
55
+ if params.is_a?(Array)
56
+ new(
57
+ **(
58
+ params.inject({}) do |r, i|
59
+ result = r
60
+ i.map {|k, v| Transformer.new.apply(k => v).to_a.first }.each do |k, v|
61
+ result = result.merge(k => r.key?(k) ? [v, r[k]] : v)
62
+ end
63
+ result
64
+ end
65
+ )
66
+ )
67
+ else
68
+ new(**params.map do |k, v|
69
+ Transformer.new.apply(k => v).to_a.first
70
+ end.to_h)
71
+ end
72
+ # merge values repeating keys into array (for copublishers)
73
+
74
+ rescue Parslet::ParseFailed => failure
75
+ raise Pubid::Iso::Errors::ParseError, "#{failure.message}\ncause: #{failure.parse_failure_cause.ascii_tree}"
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,15 @@
1
+ module Pubid::Core
2
+ class Renderer
3
+ attr_accessor :params
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ @params.default = ""
8
+ end
9
+ # render from hash keys
10
+ def render
11
+ "%{publisher}%{type}%{stage} %{number}%{part}%{iteration}"\
12
+ "%{edition}%{supplements}%{language}" % @params
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module Pubid::Core
2
+ class Supplement
3
+ attr_accessor :version, :number, :stage
4
+
5
+ def initialize(version:, number: nil, stage: nil)
6
+ @version, @number, @stage = version, number, stage
7
+ end
8
+
9
+ def ==(other)
10
+ other.version == version && (number.nil? || other.number == number) && (stage.nil? || other.stage == stage)
11
+ end
12
+
13
+ def render_pubid_number
14
+ if @number
15
+ "#{@version}:#{@number}"
16
+ else
17
+ "#{@version}"
18
+ end
19
+ end
20
+
21
+ def render_urn_number
22
+ if @number
23
+ ":#{@number}:v#{@version}"
24
+ else
25
+ ":#{@version}:v1"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ module Pubid::Core
2
+ class Transformer < Parslet::Transform
3
+ end
4
+ end
@@ -1,5 +1,5 @@
1
1
  module Pubid
2
2
  module Core
3
- VERSION = "0.1.4".freeze
3
+ VERSION = "0.1.5".freeze
4
4
  end
5
5
  end
data/lib/pubid/core.rb CHANGED
@@ -1,3 +1,10 @@
1
1
  require "parslet"
2
2
 
3
+ require_relative "core/errors"
3
4
  require_relative "core/parser"
5
+ require_relative "core/supplement"
6
+ require_relative "core/amendment"
7
+ require_relative "core/corrigendum"
8
+ require_relative "core/transformer"
9
+ require_relative "core/renderer"
10
+ require_relative "core/identifier"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubid-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-04 00:00:00.000000000 Z
11
+ date: 2022-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -65,7 +65,14 @@ files:
65
65
  - README.adoc
66
66
  - lib/pubid-core.rb
67
67
  - lib/pubid/core.rb
68
+ - lib/pubid/core/amendment.rb
69
+ - lib/pubid/core/corrigendum.rb
70
+ - lib/pubid/core/errors.rb
71
+ - lib/pubid/core/identifier.rb
68
72
  - lib/pubid/core/parser.rb
73
+ - lib/pubid/core/renderer.rb
74
+ - lib/pubid/core/supplement.rb
75
+ - lib/pubid/core/transformer.rb
69
76
  - lib/pubid/core/version.rb
70
77
  homepage: https://github.com/metanorma/pubid-core
71
78
  licenses: