Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make "IS" type default #160

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/pubid/iso/identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ def initialize(publisher: "ISO", number: nil, stage: nil, iteration: nil, supple
TypedStage.new
end

if @typed_stage.type == :is && iteration
raise Errors::IsStageIterationError, "IS stage document cannot have iteration"
end

if stage
provided_type = @typed_stage.type

@typed_stage.parse_stage(stage.is_a?(Parslet::Slice) ? stage.to_s : stage)
if !provided_type.nil? && @typed_stage.type != provided_type
if type && @typed_stage.type != type
raise Errors::StageInvalidError,
"cannot assign typed stage for document with different type (#{provided_type} vs #{@typed_stage.type})"
"cannot assign typed stage for document with different type (#{type} vs #{@typed_stage.type})"
end
elsif @typed_stage.type == :is && iteration
raise Errors::IsStageIterationError, "IS stage document cannot have iteration"
end

elsif iteration
raise Errors::IterationWithoutStageError, "Document without stage cannot have iteration"
end

# Assign typed stage to apply default type
@typed_stage = TypedStage.new unless @typed_stage

@iteration = iteration.to_i if iteration
@supplement = supplement if supplement
@joint_document = joint_document if joint_document
Expand Down
2 changes: 1 addition & 1 deletion lib/pubid/iso/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Type

# Create new type
# @param type [Symbol]
def initialize(type)
def initialize(type = :is)
raise Errors::WrongTypeError, "#{type} type is not available" unless TYPE_NAMES.key?(type)

@type = type
Expand Down
10 changes: 7 additions & 3 deletions lib/pubid/iso/typed_stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ class TypedStage
# @param type [Symbol,Type] eg. :tr, Type.new(:tr)
# @param stage [Symbol,Stage] eg. :CD, Stage.new(abbr: :CD)
def initialize(abbr: nil, type: nil, stage: nil)
@type = type.is_a?(Type) ? type : Type.new(type) if type
@type = if type
type.is_a?(Type) ? type : Type.new(type)
else
Type.new
end
@stage = stage.is_a?(Stage) ? stage : Stage.new(abbr: stage) if stage

if abbr
raise Errors::TypeStageInvalidError, "#{abbr} is not valid typed stage" unless TYPED_STAGES.key?(abbr)
assign_abbreviation(abbr)
elsif [email protected]?
elsif [email protected]? && [email protected]
# lookup for typed stage
@typed_stage = lookup_typed_stage
end
Expand Down Expand Up @@ -186,7 +190,7 @@ def parse_stage(stage_or_typed_stage)
else
raise Errors::TypeStageParseError, "cannot parse typed stage or stage"
end
@typed_stage = lookup_typed_stage
@typed_stage = lookup_typed_stage unless @stage&.abbr
end

# Parse stage or typed stage
Expand Down
4 changes: 4 additions & 0 deletions spec/pubid_iso/identifier/create_new_identifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ module Pubid::Iso
expect(subject.to_s).to eq("ISO #{number}")
end

it "assigns default type" do
expect(subject.type).to eq(:is)
end

context "when have joint document" do
let(:params) { { joint_document: "IDF #{number}" } }

Expand Down
4 changes: 4 additions & 0 deletions spec/pubid_iso/identifier/identifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ module Pubid::Iso

it_behaves_like "converts pubid to pubid"
it_behaves_like "converts pubid to urn"

it "has assigned IS type" do
expect(subject.type).to eq(:is)
end
end

context "ISO/TS 19115-3:2016" do
Expand Down
8 changes: 8 additions & 0 deletions spec/pubid_iso/type_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module Pubid::Iso
RSpec.describe Type do
context "when don't have type" do
subject { described_class.new }

it "assigns IS type by default" do
expect(subject.type).to eq(:is)
end
end

context "when using symbol" do
subject { described_class.new(type) }
let(:type) { :tr }
Expand Down
4 changes: 4 additions & 0 deletions spec/pubid_iso/typed_stage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module Pubid::Iso
let(:type) { nil }
let(:stage) { nil }

it "assigns type by default" do
expect(subject.type).to be_a(Type)
end

describe "#has_typed_stage?" do
subject { described_class.has_typed_stage?(typed_stage) }

Expand Down