pubid-core 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8fa90c212d201a964cf2744e9c8bcba06af62d10dfe195caa6af284189b82cd
4
- data.tar.gz: 9d526bcae7afe9781b462aabd17fe3bbaafb1349e3b40c5f5fc6364468dce76d
3
+ metadata.gz: 135dbda350a5a305c463d0e56ed56303ad0a95dca7cbc6a74c38e66e37ae7780
4
+ data.tar.gz: cf8b5afc99d6d4876d1aefd8fd21d95732f6cd26fefb18a9b4f1c4c2552a25ad
5
5
  SHA512:
6
- metadata.gz: bd9862d64043e894cd11ab5a92f5e792377dc28d1986918799e809c06070504e16f1be6fcecac05da3f366e52e2266476ff4733664ba3427a03f87b5a4ba8c4d
7
- data.tar.gz: b0962ace109243f800c6c17ceacde35cc50f345c952dd75b9af42045d03dbf961a72cb4d42b3585f4c1a98ecc7a035795614c45c7e83ca48bd18160c11f573d6
6
+ metadata.gz: 3401aa6d8c5ad4ab9e82361d2ec67b84976ddb47e661afb3f54ca3538388740acba8960c948520c49b0be8e4fb6781c37cea0d016ccd2d56044d1e2bf843c328
7
+ data.tar.gz: f231d49c157d57b0d346ff9e246bd7cbe04b8ed770ac5b2c2b808f9c2ae8ff570f8d5b69649d94df35d5f239b5cdf15aa892775a85b0c0f9282a0090305ec33a
@@ -1,10 +1,18 @@
1
1
  module Pubid::Core
2
2
  class Configuration
3
- attr_accessor :stages, :default_type, :type_class, :types, :type_names, :stage_class
3
+ attr_accessor :stages, :default_type, :type_class, :types, :type_names, :stage_class, :typed_stage_class
4
4
 
5
5
  def initialize
6
6
  @type_class = Pubid::Core::Type
7
7
  @stage_class = Pubid::Core::Stage
8
+ @typed_stage_class = Pubid::Core::TypedStage
9
+ @types = []
10
+ end
11
+
12
+ def typed_stages
13
+ types.inject({}) do |res, type|
14
+ res.merge(type::TYPED_STAGES)
15
+ end
8
16
  end
9
17
  end
10
18
  end
@@ -7,6 +7,7 @@ module Pubid::Core
7
7
  class ParseTypeError < StandardError; end
8
8
  class TypeStageParseError < StandardError; end
9
9
  class WrongTypeError < StandardError; end
10
+ class TypedStageInvalidError < StandardError; end
10
11
 
11
12
  end
12
13
  end
@@ -2,20 +2,26 @@ module Pubid::Core
2
2
  class HarmonizedStageCode
3
3
  include Comparable
4
4
  # attr_accessor :stage, :substage
5
- attr_accessor :config, :stages
5
+ attr_accessor :config, :stages, :harmonized_typed_stages
6
6
 
7
7
  # @param stage_or_code [String,Array<String>] stage number or whole harmonized code with substage
8
8
  # or list or stages for fuzzy stages eg. "10", 10, "20.20", ["10.20", "20.20"]
9
9
  # or stage name eg. "proposal", "approval"
10
+ # or typed stages eg. :dtr, :fdis
10
11
  # @param substage [Integer, String] eg. "00", 0
11
12
  # or substage name eg. "registration", "start_of_main_action"
12
13
  def initialize(stage_or_code, substage = "00", config:)
13
14
  @config = config
14
15
  @stages = if stage_or_code.is_a?(Array)
15
16
  stage_or_code
16
- elsif stage_or_code.is_a?(String) && config.stages["codes_description"].key?(stage_or_code)
17
+ elsif stage_or_code.is_a?(String) && (
18
+ config.stages["codes_description"].key?(stage_or_code) ||
19
+ harmonized_typed_stages.include?(stage_or_code))
17
20
  [stage_or_code]
18
- # when stage is stage name
21
+ # when stage is typed stage
22
+ elsif stage_or_code.is_a?(Symbol) && config.typed_stages.key?(stage_or_code)
23
+ config.typed_stages[stage_or_code][:harmonized_stages]
24
+ # when stage is stage name
19
25
  elsif config.stages["stage_codes"]&.key?(stage_or_code.to_s)
20
26
  ["#{config.stages["stage_codes"][stage_or_code.to_s]}.#{config.stages["substage_codes"][substage.to_s]}"]
21
27
  else
@@ -25,10 +31,19 @@ module Pubid::Core
25
31
  validate_stages
26
32
  end
27
33
 
34
+ def harmonized_typed_stages
35
+ @harmonized_typed_stages ||= config.typed_stages.values.map { |v| v[:harmonized_stages] }.flatten
36
+ end
37
+
28
38
  def validate_stages
29
39
  @stages.each do |stage|
30
40
  # raise an error when stage is wrong
31
- raise Errors::HarmonizedStageCodeInvalidError unless config.stages["codes_description"].key?(stage)
41
+ next if config.stages["codes_description"].key?(stage)
42
+
43
+ # check typed stages if no stages in config
44
+ next if harmonized_typed_stages.include?(stage)
45
+
46
+ raise Errors::HarmonizedStageCodeInvalidError
32
47
  end
33
48
  end
34
49
 
@@ -3,7 +3,7 @@ module Pubid::Core
3
3
  class Base
4
4
  attr_accessor :number, :publisher, :copublisher, :part,
5
5
  :type, :year, :edition, :language, :amendments,
6
- :corrigendums, :stage, :typed_stage
6
+ :corrigendums, :stage
7
7
 
8
8
  TYPED_STAGES = {}.freeze
9
9
 
@@ -54,7 +54,7 @@ module Pubid::Core
54
54
  @edition = edition.to_i if edition
55
55
  @language = language.to_s if language
56
56
 
57
- @typed_stage, @stage = resolve_stage(stage) if stage
57
+ @stage = resolve_stage(stage) if stage
58
58
  end
59
59
 
60
60
  # @return [String] Rendered URN identifier
@@ -86,8 +86,8 @@ module Pubid::Core
86
86
  end
87
87
 
88
88
  def typed_stage_abbrev
89
- if self.class::TYPED_STAGES.key?(typed_stage)
90
- return self.class::TYPED_STAGES[typed_stage][:abbr]
89
+ if stage.is_a?(TypedStage)
90
+ return stage.to_s
91
91
  end
92
92
 
93
93
  stage ? "#{stage.abbr} #{self.class.type[:key].to_s.upcase}" : self.class.type[:key].to_s.upcase
@@ -95,8 +95,8 @@ module Pubid::Core
95
95
 
96
96
  # Return typed stage name, eg. "Final Draft Technical Report" for "FDTR"
97
97
  def typed_stage_name
98
- if self.class::TYPED_STAGES.key?(typed_stage)
99
- return self.class::TYPED_STAGES[typed_stage][:name]
98
+ if self.class::TYPED_STAGES.key?(stage&.typed_stage)
99
+ return self.class::TYPED_STAGES[stage.typed_stage][:name]
100
100
  end
101
101
 
102
102
  stage ? "#{stage.name} #{self.class.type[:title]}" : self.class.type[:title]
@@ -106,9 +106,14 @@ module Pubid::Core
106
106
  # @return [[nil, Stage], [Symbol, Stage]] typed stage and stage values
107
107
  def resolve_stage(stage)
108
108
  if stage.is_a?(Stage)
109
- return [nil, stage] if stage.abbr
109
+ # return [nil, stage] if stage.abbr
110
+ # return stage if stage.abbr
110
111
 
111
- return [self.class.resolve_typed_stage(stage.harmonized_code), stage]
112
+ # return [self.class.resolve_typed_stage(stage.harmonized_code), stage]
113
+ unless stage.abbr
114
+ stage.typed_stage = self.class.resolve_typed_stage(stage.harmonized_code)
115
+ end
116
+ return stage
112
117
  # @typed_stage = resolve_typed_stage(@stage.harmonized_code) unless @stage.abbr
113
118
  end
114
119
 
@@ -120,10 +125,10 @@ module Pubid::Core
120
125
  # resolve typed stage when harmonized code provided as stage
121
126
  # or stage abbreviation was not resolved
122
127
  if /\A[\d.]+\z/.match?(stage) || parsed_stage.empty_abbr?(with_prf: true)
123
- return [self.class.resolve_typed_stage(parsed_stage.harmonized_code), parsed_stage]
128
+ parsed_stage.typed_stage = self.class.resolve_typed_stage(parsed_stage.harmonized_code)
124
129
  end
125
130
 
126
- [nil, parsed_stage]
131
+ parsed_stage
127
132
 
128
133
  # from IEC
129
134
  # @typed_stage = self.class::TYPED_STAGES[@typed_stage][:abbr] if @typed_stage
@@ -239,10 +244,12 @@ module Pubid::Core
239
244
  # @return [[Symbol, Stage]] typed stage and stage with assigned harmonized codes
240
245
  def find_typed_stage(typed_stage)
241
246
  if typed_stage.is_a?(Symbol)
242
- return [typed_stage,
243
- get_identifier.build_stage(
244
- harmonized_code: get_identifier.build_harmonized_stage_code(self::TYPED_STAGES[typed_stage][:harmonized_stages])),
245
- ]
247
+ return get_identifier
248
+ .build_typed_stage(
249
+ harmonized_code:
250
+ get_identifier.build_harmonized_stage_code(self::TYPED_STAGES[typed_stage][:harmonized_stages]),
251
+ abbr: typed_stage,
252
+ )
246
253
  end
247
254
 
248
255
  typed_stage = self::TYPED_STAGES.find do |_, v|
@@ -255,9 +262,9 @@ module Pubid::Core
255
262
  end
256
263
  end
257
264
 
258
- [typed_stage.first,
259
- get_identifier.build_stage(
260
- harmonized_code: get_identifier.build_harmonized_stage_code(typed_stage[1][:harmonized_stages]))]
265
+ get_identifier.build_typed_stage(harmonized_code:
266
+ get_identifier.build_harmonized_stage_code(typed_stage[1][:harmonized_stages]),
267
+ abbr: typed_stage.first)
261
268
  end
262
269
 
263
270
  # Resolve typed stage using stage harmonized stage code
@@ -7,11 +7,6 @@ module Pubid::Core
7
7
  # @see Pubid::Identifier::Base.initialize for available options
8
8
  def create(**opts)
9
9
  resolve_identifier(opts)
10
- # resolve_identifier(
11
- # opts[:type],
12
- # opts[:stage],
13
- # opts.reject { |k, _v| [:type, :stage].include?(k) },
14
- # )
15
10
  end
16
11
 
17
12
  # @param typed_stage_or_stage [String] typed stage or stage
@@ -62,5 +57,9 @@ module Pubid::Core
62
57
  def build_harmonized_stage_code(stage_or_code, substage = "00")
63
58
  HarmonizedStageCode.new(stage_or_code, substage, config: @config)
64
59
  end
60
+
61
+ def build_typed_stage(**args)
62
+ @config.typed_stage_class.new(config: @config, **args)
63
+ end
65
64
  end
66
65
  end
@@ -1,13 +1,14 @@
1
1
  module Pubid::Core
2
2
  class Stage
3
- attr_accessor :config, :abbr, :harmonized_code
3
+ attr_accessor :config, :abbr, :harmonized_code, :typed_stage
4
4
 
5
5
  # @param abbr [String, Symbol] abbreviation eg. :PWI, :WD
6
6
  # @param harmonized_code [String, Float, HarmonizedStageCode]
7
7
  # @param config [Configuration]
8
- def initialize(config:, abbr: nil, harmonized_code: nil)
8
+ def initialize(config:, abbr: nil, harmonized_code: nil, typed_stage: nil)
9
9
  @config = config
10
10
  @abbr = abbr&.to_s
11
+ @typed_stage = typed_stage
11
12
 
12
13
  if harmonized_code
13
14
  @harmonized_code = if harmonized_code.is_a?(HarmonizedStageCode)
@@ -75,6 +76,12 @@ module Pubid::Core
75
76
 
76
77
  # Compares one stage with another
77
78
  def ==(other)
79
+ return false unless other
80
+
81
+ unless other.is_a?(self.class)
82
+ return false
83
+ # other = self.class.parse(other, config: config)
84
+ end
78
85
  other&.harmonized_code == harmonized_code
79
86
  end
80
87
 
@@ -0,0 +1,42 @@
1
+ module Pubid::Core
2
+ class TypedStage < Stage
3
+ attr_accessor :config, :abbr
4
+
5
+ # @param config [Configuration]
6
+ # @param abbr [Symbol] typed stage symbol, e.g. :dtr
7
+ # @param harmonized_code [String, Float, HarmonizedStageCode]
8
+ def initialize(config:, abbr: nil, harmonized_code: nil)
9
+ @config = config
10
+ @abbr = abbr
11
+
12
+ if harmonized_code
13
+ @harmonized_code = if harmonized_code.is_a?(HarmonizedStageCode)
14
+ harmonized_code
15
+ else
16
+ HarmonizedStageCode.new(harmonized_code, config: config)
17
+ end
18
+ # @abbr ||= lookup_abbr(@harmonized_code.stages)
19
+ end
20
+
21
+ if abbr
22
+ raise Errors::TypedStageInvalidError, "#{abbr} is not valid typed stage" unless config.typed_stages.key?(abbr)
23
+
24
+ @harmonized_code ||= HarmonizedStageCode.new(abbr, config: config)
25
+ end
26
+ end
27
+
28
+ # Compares one stage with another
29
+ # should return false if
30
+ def ==(other)
31
+ return false unless other
32
+
33
+ return abbr == other if other.is_a?(Symbol)
34
+
35
+ other&.harmonized_code == harmonized_code
36
+ end
37
+
38
+ def to_s(_opts = nil)
39
+ config.typed_stages[abbr][:abbr]
40
+ end
41
+ end
42
+ end
@@ -1,5 +1,5 @@
1
1
  module Pubid
2
2
  module Core
3
- VERSION = "1.9.0".freeze
3
+ VERSION = "1.10.0".freeze
4
4
  end
5
5
  end
data/lib/pubid/core.rb CHANGED
@@ -14,3 +14,4 @@ require_relative "core/identifier"
14
14
  require_relative "core/identifier/base"
15
15
  require_relative "core/harmonized_stage_code"
16
16
  require_relative "core/stage"
17
+ require_relative "core/typed_stage"
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: 1.9.0
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-09 00:00:00.000000000 Z
11
+ date: 2023-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -79,6 +79,7 @@ files:
79
79
  - lib/pubid/core/supplement.rb
80
80
  - lib/pubid/core/transformer.rb
81
81
  - lib/pubid/core/type.rb
82
+ - lib/pubid/core/typed_stage.rb
82
83
  - lib/pubid/core/version.rb
83
84
  homepage: https://github.com/metanorma/pubid-core
84
85
  licenses: