pubid-iso 0.2.1 → 0.3.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.
@@ -0,0 +1,204 @@
1
+ module Pubid::Iso
2
+ class TypedStage
3
+ attr_accessor :type, :stage, :typed_stage
4
+
5
+ TYPED_STAGES = {
6
+ dtr: {
7
+ abbr: "DTR",
8
+ type: :tr,
9
+ name: "Draft Technical Report",
10
+ harmonized_stages: %w[40.00 40.20 40.60 40.92 40.93 50.00 50.20 50.60 50.92],
11
+ },
12
+ dis: {
13
+ abbr: "DIS",
14
+ type: :is,
15
+ name: "Draft International Standard",
16
+ harmonized_stages: %w[40.00 40.20 40.60 40.92 40.93],
17
+ },
18
+ dts: {
19
+ abbr: "DTS",
20
+ type: :ts,
21
+ name: "Draft Technical Specification",
22
+ harmonized_stages: %w[40.00 40.20 40.60 40.92 40.93 50.00 50.20 50.60 50.92],
23
+ },
24
+ fdts: {
25
+ abbr: "FDTS",
26
+ type: :ts,
27
+ name: "Final Draft Technical Specification",
28
+ harmonized_stages: %w[50.00 50.20 50.60 50.92],
29
+ },
30
+ fdtr: {
31
+ abbr: "FDTR",
32
+ type: :tr,
33
+ name: "Final Draft Technical Report",
34
+ harmonized_stages: %w[50.00 50.20 50.60 50.92],
35
+ },
36
+ fdis: {
37
+ abbr: "FDIS",
38
+ type: :is,
39
+ name: "Final Draft International Standard",
40
+ harmonized_stages: %w[50.00 50.20 50.60 50.92],
41
+ },
42
+ dpas: {
43
+ abbr: "DPAS",
44
+ type: :pas,
45
+ name: "Publicly Available Specification Draft",
46
+ harmonized_stages: %w[40.00 40.20 40.60 40.92 40.93 50.00 50.20 50.60 50.92],
47
+ },
48
+ damd: {
49
+ abbr: { short: "DAM", long: "DAmd" },
50
+ type: :amd,
51
+ name: "Draft Amendment",
52
+ harmonized_stages: %w[40.00 40.20 40.60 40.92 40.93 50.00 50.20 50.60 50.92],
53
+ },
54
+ dcor: {
55
+ abbr: { short: "DCOR", long: "DCor" },
56
+ type: :cor,
57
+ name: "Draft Corrigendum",
58
+ harmonized_stages: %w[40.00 40.20 40.60 40.92 40.93 50.00 50.20 50.60 50.92],
59
+ },
60
+ fdamd: {
61
+ abbr: { short: "FDAM", long: "FDAmd" },
62
+ type: :amd,
63
+ name: "Final Draft Amendment",
64
+ harmonized_stages: %w[50.00 50.20 50.60 50.92],
65
+ },
66
+ fdcor: {
67
+ abbr: { short: "FDCOR", long: "FDCor" },
68
+ type: :cor,
69
+ name: "Final Draft Corrigendum",
70
+ harmonized_stages: %w[50.00 50.20 50.60 50.92],
71
+ },
72
+ }.freeze
73
+
74
+ # @param type [Symbol,Type] eg. :tr, Type.new(:tr)
75
+ # @param stage [Symbol,Stage] eg. :CD, Stage.new(abbr: :CD)
76
+ def initialize(abbr: nil, type: nil, stage: nil)
77
+ @type = type.is_a?(Type) ? type : Type.new(type) if type
78
+ @stage = stage.is_a?(Stage) ? stage : Stage.new(abbr: stage) if stage
79
+
80
+ if abbr
81
+ raise Errors::TypeStageInvalidError, "#{abbr} is not valid typed stage" unless TYPED_STAGES.key?(abbr)
82
+ assign_abbreviation(abbr)
83
+ elsif !@stage.nil?
84
+ # lookup for typed stage
85
+ @typed_stage = lookup_typed_stage
86
+ end
87
+ end
88
+
89
+ def lookup_typed_stage
90
+ return nil unless @stage
91
+
92
+ TYPED_STAGES.each do |typed_stage, values|
93
+ if values[:harmonized_stages].include?(@stage.harmonized_code.to_s) && values[:type] == @type&.type
94
+ return typed_stage
95
+ end
96
+ end
97
+ nil
98
+ end
99
+
100
+ # Assigns type and stage according to provided typed stage abbreviation
101
+ # @param abbr [Symbol] eg. :dtr, :damd, :dis
102
+ def assign_abbreviation(abbr)
103
+ @typed_stage = if TYPED_STAGES.key?(abbr.downcase.to_sym)
104
+ abbr.downcase.to_sym
105
+ else
106
+ TYPED_STAGES.select do |_, v|
107
+ if v[:abbr].is_a?(Hash)
108
+ v[:abbr].values.include?(abbr)
109
+ else
110
+ v[:abbr] == abbr
111
+ end
112
+ end.keys.first
113
+ end
114
+
115
+ @type = Type.new(TYPED_STAGES[@typed_stage][:type])
116
+ @stage = Stage.new(harmonized_code: HarmonizedStageCode.new(TYPED_STAGES[@typed_stage][:harmonized_stages]))
117
+ end
118
+
119
+ # Render typed stage
120
+ # @param stage_format_long [Boolean] render stage in long or short format
121
+ def to_s(stage_format_long = true)
122
+ # return "" if @type == :amd || @type == :cor
123
+ if @typed_stage
124
+ if TYPED_STAGES[@typed_stage][:abbr].is_a?(Hash)
125
+ return TYPED_STAGES[@typed_stage][:abbr][stage_format_long ? :long : :short]
126
+ else
127
+ return TYPED_STAGES[@typed_stage][:abbr]
128
+ end
129
+ end
130
+
131
+ result = (@stage && @stage.abbr != "IS") ? "#{@stage.abbr}" : ""
132
+ result += " " if !result.empty? && @type && stage_format_long
133
+ result + if stage_format_long
134
+ "#{@type&.to_s}"
135
+ else
136
+ if @type == :amd
137
+ "AM"
138
+ elsif @type == :cor
139
+ "COR"
140
+ else
141
+ "#{@type&.to_s}"
142
+ end
143
+ end
144
+ end
145
+
146
+ # Check if typed stage listed in TYPED_STAGES constant
147
+ # @param typed_stage [String,Symbol] typed stage abbreviation, eg. "DTS", :dts, "DAmd", :damd
148
+ def self.has_typed_stage?(typed_stage)
149
+ return true if TYPED_STAGES.key?(typed_stage)
150
+
151
+ TYPED_STAGES.any? do |_, v|
152
+ if v[:abbr].is_a?(Hash)
153
+ v[:abbr].values.include?(typed_stage)
154
+ else
155
+ v[:abbr] == typed_stage
156
+ end
157
+ end
158
+ end
159
+
160
+ # Assigns stage or type or typed stage or stage and type depending on provided string
161
+ # @param stage_or_typed_stage [String, Stage] eg. "DTR", "CD", Stage.new(:CD), "TR", "CD Amd", :dtr
162
+ def parse_stage(stage_or_typed_stage)
163
+ if self.class.has_typed_stage?(stage_or_typed_stage)
164
+ return assign_abbreviation(stage_or_typed_stage)
165
+ end
166
+
167
+ if stage_or_typed_stage.is_a?(Stage)
168
+ @stage = stage_or_typed_stage
169
+ elsif stage_or_typed_stage.is_a?(String) && stage_or_typed_stage.split.count == 2 &&
170
+ Stage.has_stage?(stage_or_typed_stage.split.first)
171
+ # stage and type ("CD Amd")
172
+ @stage = Stage.parse(stage_or_typed_stage.split.first)
173
+ @type = Type.parse(stage_or_typed_stage.split.last)
174
+ elsif Type.has_type?(stage_or_typed_stage)
175
+ @type = Type.parse(stage_or_typed_stage)
176
+ elsif Stage.has_stage?(stage_or_typed_stage)
177
+ @stage = Stage.parse(stage_or_typed_stage.to_s)
178
+ else
179
+ raise Errors::TypeStageParseError, "cannot parse typed stage or stage"
180
+ end
181
+ @typed_stage = lookup_typed_stage
182
+ end
183
+
184
+ # Parse stage or typed stage
185
+ # @return [TypedStage] typed stage object with parsed stage and typed stage
186
+ def self.parse(stage_or_typed_stage)
187
+ return stage_or_typed_stage if stage_or_typed_stage.is_a?(TypedStage)
188
+
189
+ typed_stage = new
190
+ typed_stage.parse_stage(stage_or_typed_stage)
191
+ typed_stage
192
+ end
193
+
194
+ def name
195
+ TYPED_STAGES[@typed_stage][:name]
196
+ end
197
+
198
+ def ==(other)
199
+ return false if other.nil?
200
+
201
+ type == other.type && typed_stage == other.typed_stage && stage == other.stage
202
+ end
203
+ end
204
+ end
@@ -1,5 +1,5 @@
1
1
  module Pubid
2
2
  module Iso
3
- VERSION = "0.2.1".freeze
3
+ VERSION = "0.3.0".freeze
4
4
  end
5
5
  end
data/lib/pubid/iso.rb CHANGED
@@ -11,6 +11,8 @@ end
11
11
  require "pubid-core"
12
12
  require_relative "iso/errors"
13
13
  require_relative "iso/stage"
14
+ require_relative "iso/type"
15
+ require_relative "iso/typed_stage"
14
16
  require_relative "iso/harmonized_stage_code"
15
17
  require_relative "iso/parser"
16
18
  require_relative "iso/transformer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubid-iso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.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: 2022-09-29 00:00:00.000000000 Z
11
+ date: 2022-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 1.2.0
103
+ version: 1.3.0
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: 1.2.0
110
+ version: 1.3.0
111
111
  description: Library to generate, parse and manipulate ISO PubID.
112
112
  email:
113
113
  - open.source@ribose.com
@@ -138,6 +138,8 @@ files:
138
138
  - lib/pubid/iso/stage.rb
139
139
  - lib/pubid/iso/supplement.rb
140
140
  - lib/pubid/iso/transformer.rb
141
+ - lib/pubid/iso/type.rb
142
+ - lib/pubid/iso/typed_stage.rb
141
143
  - lib/pubid/iso/version.rb
142
144
  homepage: https://github.com/metanorma/pubid-iso
143
145
  licenses: