pubid-core 1.8.7 → 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: 65c153405bb8ae343ef26b9943f013864d2db8fd2abfd27e14d2db1f83de4d0a
4
- data.tar.gz: 1ebdf5cef55a9c09111f18245a9d6767ee8107d7039398e8aaa9ceb8f5e0e9d5
3
+ metadata.gz: 135dbda350a5a305c463d0e56ed56303ad0a95dca7cbc6a74c38e66e37ae7780
4
+ data.tar.gz: cf8b5afc99d6d4876d1aefd8fd21d95732f6cd26fefb18a9b4f1c4c2552a25ad
5
5
  SHA512:
6
- metadata.gz: ff849c54be9d3e4b49a54c48fd58413b66fe0f2aae40f7735c62820fe064e08fdb77b9e197b21a3471dd14570b4875223dce065ae3e5333b614beb520c171bae
7
- data.tar.gz: d649815ce64e1654b64681d1a183c6309759403738fff8d270ca01effe4a87d4a9e0e7e82f8468bbc7fe15c9717d2e0024e6268a16571eaba71620f0398ead6b
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,15 +54,16 @@ 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
61
61
  def urn
62
- Renderer::Urn.new(get_params).render
62
+ Renderer::Urn.new(to_h).render
63
63
  end
64
64
 
65
- def get_params
65
+ # @return [Hash] Identifier's parameters
66
+ def to_h
66
67
  instance_variables.map do |var|
67
68
  # XXX: temporary hack to prepare typed_stage for rendering
68
69
  # probably need to convert typed_stage to class, now we store
@@ -76,17 +77,17 @@ module Pubid::Core
76
77
  end
77
78
 
78
79
  def ==(other)
79
- get_params == other.get_params
80
+ to_h == other.to_h
80
81
  end
81
82
 
82
83
  # Render identifier using default renderer
83
84
  def to_s
84
- self.class.get_renderer_class.new(get_params).render
85
+ self.class.get_renderer_class.new(to_h).render
85
86
  end
86
87
 
87
88
  def typed_stage_abbrev
88
- if self.class::TYPED_STAGES.key?(typed_stage)
89
- return self.class::TYPED_STAGES[typed_stage][:abbr]
89
+ if stage.is_a?(TypedStage)
90
+ return stage.to_s
90
91
  end
91
92
 
92
93
  stage ? "#{stage.abbr} #{self.class.type[:key].to_s.upcase}" : self.class.type[:key].to_s.upcase
@@ -94,8 +95,8 @@ module Pubid::Core
94
95
 
95
96
  # Return typed stage name, eg. "Final Draft Technical Report" for "FDTR"
96
97
  def typed_stage_name
97
- if self.class::TYPED_STAGES.key?(typed_stage)
98
- 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]
99
100
  end
100
101
 
101
102
  stage ? "#{stage.name} #{self.class.type[:title]}" : self.class.type[:title]
@@ -105,9 +106,14 @@ module Pubid::Core
105
106
  # @return [[nil, Stage], [Symbol, Stage]] typed stage and stage values
106
107
  def resolve_stage(stage)
107
108
  if stage.is_a?(Stage)
108
- return [nil, stage] if stage.abbr
109
+ # return [nil, stage] if stage.abbr
110
+ # return stage if stage.abbr
109
111
 
110
- 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
111
117
  # @typed_stage = resolve_typed_stage(@stage.harmonized_code) unless @stage.abbr
112
118
  end
113
119
 
@@ -119,10 +125,10 @@ module Pubid::Core
119
125
  # resolve typed stage when harmonized code provided as stage
120
126
  # or stage abbreviation was not resolved
121
127
  if /\A[\d.]+\z/.match?(stage) || parsed_stage.empty_abbr?(with_prf: true)
122
- 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)
123
129
  end
124
130
 
125
- [nil, parsed_stage]
131
+ parsed_stage
126
132
 
127
133
  # from IEC
128
134
  # @typed_stage = self.class::TYPED_STAGES[@typed_stage][:abbr] if @typed_stage
@@ -238,10 +244,12 @@ module Pubid::Core
238
244
  # @return [[Symbol, Stage]] typed stage and stage with assigned harmonized codes
239
245
  def find_typed_stage(typed_stage)
240
246
  if typed_stage.is_a?(Symbol)
241
- return [typed_stage,
242
- get_identifier.build_stage(
243
- harmonized_code: get_identifier.build_harmonized_stage_code(self::TYPED_STAGES[typed_stage][:harmonized_stages])),
244
- ]
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
+ )
245
253
  end
246
254
 
247
255
  typed_stage = self::TYPED_STAGES.find do |_, v|
@@ -254,9 +262,9 @@ module Pubid::Core
254
262
  end
255
263
  end
256
264
 
257
- [typed_stage.first,
258
- get_identifier.build_stage(
259
- 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)
260
268
  end
261
269
 
262
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
@@ -44,9 +44,9 @@ module Pubid::Core::Renderer
44
44
  def render_supplement(supplement_params, **args)
45
45
  base = if supplement_params[:base].type == :amd
46
46
  # render inner amendment (cor -> amd -> base)
47
- render_supplement(supplement_params[:base].get_params, **args)
47
+ render_supplement(supplement_params[:base].to_h, **args)
48
48
  else
49
- self.class.new(supplement_params[:base].get_params).render_base_identifier(
49
+ self.class.new(supplement_params[:base].to_h).render_base_identifier(
50
50
  # always render year for identifiers with supplement
51
51
  **args.merge({ with_date: true }),
52
52
  )
@@ -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.8.7".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.8.7
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-08-25 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: