Skip to content

Commit

Permalink
Parse corrigenda identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
mico committed Aug 1, 2023
1 parent 1d1433d commit eb745a7
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 61 deletions.
5 changes: 3 additions & 2 deletions lib/pubid/itu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ module Itu
require_relative "itu/identifier/question"
require_relative "itu/identifier/resolution"
require_relative "itu/identifier/special_publication"
require_relative "itu/identifier/supplement"
require_relative "itu/identifier/amendment"
require_relative "itu/identifier/regulatory_publication"
require_relative "itu/identifier/implementers_guide"
require_relative "itu/identifier/supplement"
require_relative "itu/identifier/annex"
require_relative "itu/identifier/corrigendum"
require_relative "itu/transformer"
require_relative "itu/renderer/base"
require_relative "itu/renderer/amendment"
require_relative "itu/renderer/implementers_guide"
require_relative "itu/parser"
require_relative "itu/identifier"
Expand All @@ -38,6 +38,7 @@ module Itu
Pubid::Itu::Identifier::Question,
Pubid::Itu::Identifier::SpecialPublication,
Pubid::Itu::Identifier::Amendment,
Pubid::Itu::Identifier::Corrigendum,
Pubid::Itu::Identifier::RegulatoryPublication,
Pubid::Itu::Identifier::ImplementersGuide,
Pubid::Itu::Identifier::Supplement,
Expand Down
8 changes: 2 additions & 6 deletions lib/pubid/itu/identifier/amendment.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
module Pubid::Itu
module Identifier
class Amendment < Base
class Amendment < Supplement
def_delegators 'Pubid::Itu::Identifier::Amendment', :type

def self.type
{ key: :amd, title: "Amendment" }
end

def self.get_renderer_class
Renderer::Amendment
{ key: :amendment, title: "Amendment" }
end
end
end
Expand Down
13 changes: 1 addition & 12 deletions lib/pubid/itu/identifier/annex.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
module Pubid::Itu
module Identifier
class Annex < Base
class Annex < Supplement
def_delegators 'Pubid::Itu::Identifier::Annex', :type

def initialize(base: nil, **opts)
super(**opts)
@base = base
end

def to_s
self.class.get_renderer_class.new(
@base.get_params.merge({ annex: self }),
).render
end

def self.type
{ key: :annex, title: "Annex" }
end
Expand Down
28 changes: 11 additions & 17 deletions lib/pubid/itu/identifier/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,24 @@ def has_type?(type)
end
end

def transform_supplements(type, identifier_params)
Identifier.create(
type: type,
base: transform(
**identifier_params.dup.tap { |h| h.delete(type) }),
**identifier_params[type],
)
end

# Use Identifier#create to resolve identifier's type class
def transform(params)
identifier_params = params.map do |k, v|
get_transformer_class.new.apply(k => v)
end.inject({}, :merge)

if identifier_params[:supplement]
return Identifier.create(
number: identifier_params[:supplement][:number],
type: :supplement,
base: Identifier.create(
**identifier_params.dup.tap { |h| h.delete(:supplement) }),
)
%i(supplement amendment corrigendum annex).each do |type|
return transform_supplements(type, identifier_params) if identifier_params[type]
end

if identifier_params[:annex]
return Identifier.create(
number: identifier_params[:annex][:number],
type: :annex,
base: Identifier.create(
**identifier_params.dup.tap { |h| h.delete(:annex) }),
)
end

Identifier.create(**identifier_params)
end

Expand Down
11 changes: 11 additions & 0 deletions lib/pubid/itu/identifier/corrigendum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Pubid::Itu
module Identifier
class Corrigendum < Supplement
def_delegators 'Pubid::Itu::Identifier::Corrigendum', :type

def self.type
{ key: :corrigendum, title: "Corrigendum" }
end
end
end
end
8 changes: 4 additions & 4 deletions lib/pubid/itu/identifier/supplement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def initialize(base: nil, **opts)
@base = base
end

def to_s
self.class.get_renderer_class.new(
@base.get_params.merge({ supplement: self }),
).render
def get_params
# XXX: hack to render supplements using Base renderer, because we need to
# place date published after amendment, e.g. `ITU-T G.780/Y.1351 Amd 1 (2004)`
@base.get_params.merge(self.type[:key] => super)
end

def self.type
Expand Down
7 changes: 6 additions & 1 deletion lib/pubid/itu/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,14 @@ class Parser < Pubid::Core::Parser
space >> str("Annex") >> space >> (match["A-Z"] >> digits.maybe >> str("+").maybe).as(:number).as(:annex)
end

rule(:corrigendum) do
(published >> space >> str("Cor.") >> space >>
digits.as(:number)).as(:corrigendum)
end

rule(:identifier) do
str("ITU") >> (dash | space) >> sector_series_number >> supplement.maybe >>
annex.maybe >> published.maybe >> amendment.maybe >> str("-I").maybe
annex.maybe >> corrigendum.maybe >> published.maybe >> amendment.maybe >> str("-I").maybe
end

rule(:root) { identifier }
Expand Down
11 changes: 0 additions & 11 deletions lib/pubid/itu/renderer/amendment.rb

This file was deleted.

12 changes: 8 additions & 4 deletions lib/pubid/itu/renderer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def render_type_series(params)

def render_identifier(params)
"%{publisher}-%{sector} #{render_type_series(params)}%{number}%{subseries}"\
"%{part}%{second_number}%{amendment}%{supplement}%{annex}%{date}" % params
"%{part}%{second_number}%{annex}%{amendment}%{corrigendum}%{supplement}%{date}" % params
end

def render_number(number, _opts, params)
Expand Down Expand Up @@ -42,7 +42,7 @@ def render_series(series, _opts, _params)
end

def render_amendment(amendment, _opts, _params)
" #{amendment}"
" Amd #{amendment[:number]}"
end

def render_subseries(subseries, _opts, _params)
Expand All @@ -62,11 +62,15 @@ def render_second_number(second_number, _opts, _params)
end

def render_supplement(supplement, _opts, _params)
" Suppl. #{supplement.number}"
" Suppl. #{supplement[:number]}"
end

def render_annex(annex, _opts, _params)
" Annex #{annex.number}"
" Annex #{annex[:number]}"
end

def render_corrigendum(corrigendum, opts, params)
"#{render_date(corrigendum[:date], opts, params)} Cor. #{corrigendum[:number]}"
end
end
end
4 changes: 0 additions & 4 deletions lib/pubid/itu/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ class Transformer < Pubid::Core::Transformer
rule(series: "Operational Bulletin") do
{ series: "OB" }
end

rule(amendment: subtree(:amendment)) do |context|
{ amendment: Identifier::Amendment.new(**context[:amendment]) }
end
end
end
19 changes: 19 additions & 0 deletions spec/pubid_itu/identifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ module Pubid::Itu
let(:pubid) { "ITU-T G.989 Amd 1" }

it_behaves_like "converts pubid to pubid"

it { expect(subject).to be_a(Identifier::Amendment) }
end

context "ITU-T G.989.2" do
Expand Down Expand Up @@ -253,6 +255,23 @@ module Pubid::Itu
it { expect(subject).to be_a(Identifier::Annex) }
end

context "ITU-T Z.100 (1999) Cor. 1 (10/2001)" do
let(:pubid) { "ITU-T Z.100 (1999) Cor. 1 (10/2001)" }

it_behaves_like "converts pubid to pubid"

it { expect(subject).to be_a(Identifier::Corrigendum) }
end

context "ITU-T G.729 Annex E (1998) Cor. 1 (02/2000)" do
let(:pubid) { "ITU-T G.729 Annex E (1998) Cor. 1 (02/2000)" }

it_behaves_like "converts pubid to pubid"

it { expect(subject).to be_a(Identifier::Corrigendum) }
it { expect(subject.base).to be_a(Identifier::Annex) }
end

describe "parse identifiers from examples files" do
context "parses IEC identifiers from itu-r.txt" do
let(:examples_file) { "itu-r.txt" }
Expand Down

0 comments on commit eb745a7

Please sign in to comment.