pubid-core 1.9.0 → 1.10.2

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: c8fa90c212d201a964cf2744e9c8bcba06af62d10dfe195caa6af284189b82cd
4
- data.tar.gz: 9d526bcae7afe9781b462aabd17fe3bbaafb1349e3b40c5f5fc6364468dce76d
3
+ metadata.gz: ebe928af0a08e530bc616c88526fa076841d66f32fdb075640f67b7a72847b83
4
+ data.tar.gz: 438ade1f287d204a01aea76ea44e2bbc6cac186fd73bf5332bc1974dcec45df6
5
5
  SHA512:
6
- metadata.gz: bd9862d64043e894cd11ab5a92f5e792377dc28d1986918799e809c06070504e16f1be6fcecac05da3f366e52e2266476ff4733664ba3427a03f87b5a4ba8c4d
7
- data.tar.gz: b0962ace109243f800c6c17ceacde35cc50f345c952dd75b9af42045d03dbf961a72cb4d42b3585f4c1a98ecc7a035795614c45c7e83ca48bd18160c11f573d6
6
+ metadata.gz: b4d60fc2b2dc1f146a31f3b03275522d598b18adc8e86c9b229fd9cea4375c851fea88b02e96ff74e2c55d6e215e849ec7f026f53fa34d18a77a47d7e2312a81
7
+ data.tar.gz: 8d7e1fffb7cf18ff56524be8187fb4895a2e1043ccc1941c9a6500f834bf566461758cca8aac34be8c64bc637610602d65129e4b658e76a973ac91ea4a7a5541
data/README.adoc CHANGED
@@ -1,2 +1,31 @@
1
1
  = Publication identifiers parsing library
2
2
 
3
+ == Usage
4
+
5
+ === Exclude attributes when compare
6
+
7
+ [source,ruby]
8
+ ----
9
+ require "pubid-core"
10
+
11
+ pubid_first = Identifier.parse("ISO 1:1999")
12
+ pubid_second = Identifier.parse("ISO 1")
13
+
14
+ pubid_first == pubid_second
15
+ => false
16
+
17
+ pubid_first.exclude(:year) == pubid_second
18
+ => true
19
+ ----
20
+
21
+ === Using #to_h to convert identifier to hash
22
+
23
+
24
+ [source,ruby]
25
+ ----
26
+ require "pubid-core"
27
+
28
+ pubid = Identifier.parse("ISO 1:1999")
29
+ pubid.to_h
30
+ => { publisher: "ISO", number: 1, year: 1999 }
31
+ ----
@@ -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
@@ -65,14 +65,15 @@ module Pubid::Core
65
65
  # @return [Hash] Identifier's parameters
66
66
  def to_h
67
67
  instance_variables.map do |var|
68
- # XXX: temporary hack to prepare typed_stage for rendering
69
- # probably need to convert typed_stage to class, now we store
70
- # typed_stage as key to typed stage (Symbol)
71
- if var.to_s == "@typed_stage" && @typed_stage
72
- [:typed_stage, self.class::TYPED_STAGES[@typed_stage][:abbr]]
73
- else
74
- [var.to_s.gsub("@", "").to_sym, instance_variable_get(var)]
75
- end
68
+ value = instance_variable_get(var)
69
+
70
+ [var.to_s.gsub("@", "").to_sym,
71
+ if value.is_a?(Array)
72
+ value.map { |v| v.respond_to?(:to_h) ? v.to_h : v }
73
+ else
74
+ value.respond_to?(:to_h) ? value.to_h : value
75
+ end
76
+ ]
76
77
  end.to_h
77
78
  end
78
79
 
@@ -85,9 +86,13 @@ module Pubid::Core
85
86
  self.class.get_renderer_class.new(to_h).render
86
87
  end
87
88
 
89
+ def exclude(*attrs)
90
+ self.class.new(**to_h.reject { |k| attrs.include?(k) })
91
+ end
92
+
88
93
  def typed_stage_abbrev
89
- if self.class::TYPED_STAGES.key?(typed_stage)
90
- return self.class::TYPED_STAGES[typed_stage][:abbr]
94
+ if stage.is_a?(TypedStage)
95
+ return stage.to_s
91
96
  end
92
97
 
93
98
  stage ? "#{stage.abbr} #{self.class.type[:key].to_s.upcase}" : self.class.type[:key].to_s.upcase
@@ -95,8 +100,8 @@ module Pubid::Core
95
100
 
96
101
  # Return typed stage name, eg. "Final Draft Technical Report" for "FDTR"
97
102
  def typed_stage_name
98
- if self.class::TYPED_STAGES.key?(typed_stage)
99
- return self.class::TYPED_STAGES[typed_stage][:name]
103
+ if self.class::TYPED_STAGES.key?(stage&.typed_stage)
104
+ return self.class::TYPED_STAGES[stage.typed_stage][:name]
100
105
  end
101
106
 
102
107
  stage ? "#{stage.name} #{self.class.type[:title]}" : self.class.type[:title]
@@ -106,9 +111,14 @@ module Pubid::Core
106
111
  # @return [[nil, Stage], [Symbol, Stage]] typed stage and stage values
107
112
  def resolve_stage(stage)
108
113
  if stage.is_a?(Stage)
109
- return [nil, stage] if stage.abbr
114
+ # return [nil, stage] if stage.abbr
115
+ # return stage if stage.abbr
110
116
 
111
- return [self.class.resolve_typed_stage(stage.harmonized_code), stage]
117
+ # return [self.class.resolve_typed_stage(stage.harmonized_code), stage]
118
+ unless stage.abbr
119
+ stage.typed_stage = self.class.resolve_typed_stage(stage.harmonized_code)
120
+ end
121
+ return stage
112
122
  # @typed_stage = resolve_typed_stage(@stage.harmonized_code) unless @stage.abbr
113
123
  end
114
124
 
@@ -120,10 +130,10 @@ module Pubid::Core
120
130
  # resolve typed stage when harmonized code provided as stage
121
131
  # or stage abbreviation was not resolved
122
132
  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]
133
+ parsed_stage.typed_stage = self.class.resolve_typed_stage(parsed_stage.harmonized_code)
124
134
  end
125
135
 
126
- [nil, parsed_stage]
136
+ parsed_stage
127
137
 
128
138
  # from IEC
129
139
  # @typed_stage = self.class::TYPED_STAGES[@typed_stage][:abbr] if @typed_stage
@@ -239,10 +249,12 @@ module Pubid::Core
239
249
  # @return [[Symbol, Stage]] typed stage and stage with assigned harmonized codes
240
250
  def find_typed_stage(typed_stage)
241
251
  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
- ]
252
+ return get_identifier
253
+ .build_typed_stage(
254
+ harmonized_code:
255
+ get_identifier.build_harmonized_stage_code(self::TYPED_STAGES[typed_stage][:harmonized_stages]),
256
+ abbr: typed_stage,
257
+ )
246
258
  end
247
259
 
248
260
  typed_stage = self::TYPED_STAGES.find do |_, v|
@@ -255,9 +267,9 @@ module Pubid::Core
255
267
  end
256
268
  end
257
269
 
258
- [typed_stage.first,
259
- get_identifier.build_stage(
260
- harmonized_code: get_identifier.build_harmonized_stage_code(typed_stage[1][:harmonized_stages]))]
270
+ get_identifier.build_typed_stage(harmonized_code:
271
+ get_identifier.build_harmonized_stage_code(typed_stage[1][:harmonized_stages]),
272
+ abbr: typed_stage.first)
261
273
  end
262
274
 
263
275
  # 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
 
@@ -94,5 +101,9 @@ module Pubid::Core
94
101
  def to_s(opts = {})
95
102
  empty_abbr?(**opts) ? "" : abbr
96
103
  end
104
+
105
+ def to_h
106
+ abbr
107
+ end
97
108
  end
98
109
  end
@@ -33,5 +33,10 @@ module Pubid::Core
33
33
  ":#{@number}:v1"
34
34
  end
35
35
  end
36
+
37
+ def to_h
38
+ { number: number,
39
+ year: year }
40
+ end
36
41
  end
37
42
  end
@@ -0,0 +1,40 @@
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:, 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
+ raise Errors::TypedStageInvalidError, "#{abbr} is not valid typed stage" unless config.typed_stages.key?(abbr)
22
+
23
+ @harmonized_code ||= HarmonizedStageCode.new(abbr, config: config)
24
+ end
25
+
26
+ # Compares one stage with another
27
+ # should return false if
28
+ def ==(other)
29
+ return false unless other.is_a?(self.class)
30
+
31
+ # return abbr == other if other.is_a?(Symbol)
32
+
33
+ other&.harmonized_code == harmonized_code
34
+ end
35
+
36
+ def to_s(_opts = nil)
37
+ config.typed_stages[abbr][:abbr]
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  module Pubid
2
2
  module Core
3
- VERSION = "1.9.0".freeze
3
+ VERSION = "1.10.2".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.2
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-04 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: