From 513e4fef9c2cd98f0b8f6114d9ec576dbdf8ad51 Mon Sep 17 00:00:00 2001 From: HassanAkbar <hassanakbar2@gmail.com> Date: Mon, 26 Feb 2024 16:52:49 +0500 Subject: [PATCH] fixing config bugs (#94) --- lib/glossarist/config.rb | 4 +- lib/glossarist/localized_concept.rb | 2 +- lib/glossarist/model.rb | 7 ++- .../relaton_cache/iso/iso_ts_14812_2022.xml | 8 +-- spec/fixtures/relaton_cache/iso/version | 2 +- spec/unit/config_spec.rb | 53 +++++++++++++++++++ 6 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 spec/unit/config_spec.rb diff --git a/lib/glossarist/config.rb b/lib/glossarist/config.rb index d8c9580..2eeb7e6 100644 --- a/lib/glossarist/config.rb +++ b/lib/glossarist/config.rb @@ -28,7 +28,7 @@ def class_for(name) end def register_class(class_name, klass) - @registered_classes[class_name] = klass + @registered_classes[class_name.to_sym] = klass end def extension_attributes @@ -53,7 +53,7 @@ def register_class(class_name, klass) end def register_extension_attributes(attributes) - self.register_extension_attributes(attributes) + self.instance.register_extension_attributes(attributes) end end end diff --git a/lib/glossarist/localized_concept.rb b/lib/glossarist/localized_concept.rb index 2a44c02..c0b012a 100644 --- a/lib/glossarist/localized_concept.rb +++ b/lib/glossarist/localized_concept.rb @@ -54,7 +54,7 @@ def to_h # rubocop:disable Metrics/MethodLength, Metrics/AbcSize "review_date" => review_date, "review_decision_date" => review_decision_date, "review_decision_event" => review_decision_event, - }.compact).merge(@extension_attributes) + }.compact).merge!(@extension_attributes) hash end diff --git a/lib/glossarist/model.rb b/lib/glossarist/model.rb index de047b7..e87973c 100644 --- a/lib/glossarist/model.rb +++ b/lib/glossarist/model.rb @@ -18,12 +18,11 @@ def initialize(attributes = {}) def set_attribute(name, value) public_send("#{name}=", value) rescue NoMethodError - # adding support for camel case - if name.match?(/[A-Z]/) + if Config.extension_attributes.include?(name) + extension_attributes[name] = value + elsif name.match?(/[A-Z]/) # adding support for camel case name = snake_case(name.to_s).to_sym retry - elsif Config.extension_attributes.include?(name) - extension_attributes[name] = value else raise ArgumentError, "#{self.class.name} does not have " + "attribute #{name} defined or the attribute is read only." diff --git a/spec/fixtures/relaton_cache/iso/iso_ts_14812_2022.xml b/spec/fixtures/relaton_cache/iso/iso_ts_14812_2022.xml index a24c432..fb8f61b 100644 --- a/spec/fixtures/relaton_cache/iso/iso_ts_14812_2022.xml +++ b/spec/fixtures/relaton_cache/iso/iso_ts_14812_2022.xml @@ -1,5 +1,5 @@ -<bibdata type="standard" schema-version="v1.2.7"> - <fetched>2023-12-12</fetched> +<bibdata type="standard" schema-version="v1.2.8"> + <fetched>2024-02-26</fetched> <title type="title-intro" format="text/plain" language="en" script="Latn">Intelligent transport systems</title> <title type="title-main" format="text/plain" language="en" script="Latn">Vocabulary</title> <title type="main" format="text/plain" language="en" script="Latn">Intelligent transport systems - Vocabulary</title> @@ -10,8 +10,8 @@ <uri type="obp">https://www.iso.org/obp/ui/en/#!iso:std:79779:en</uri> <uri type="rss">https://www.iso.org/contents/data/standard/07/97/79779.detail.rss</uri> <docidentifier type="ISO" primary="true">ISO/TS 14812:2022</docidentifier> - <docidentifier type="iso-reference">ISO 14812:2022(E)</docidentifier> - <docidentifier type="URN">urn:iso:std:iso:ts:14812:stage-90.92:ed-1</docidentifier> + <docidentifier type="iso-reference">ISO/TS 14812:2022(E)</docidentifier> + <docidentifier type="URN">urn:iso:std:iso:ts:14812:stage-90.92</docidentifier> <docnumber>14812</docnumber> <date type="published"> <on>2022-04</on> diff --git a/spec/fixtures/relaton_cache/iso/version b/spec/fixtures/relaton_cache/iso/version index b3b95af..ddac12f 100644 --- a/spec/fixtures/relaton_cache/iso/version +++ b/spec/fixtures/relaton_cache/iso/version @@ -1 +1 @@ -a03026b6e0501d55c0b281625ab5eb34 \ No newline at end of file +5c0adcc653acd420404ff8332dee304a \ No newline at end of file diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb new file mode 100644 index 0000000..261e2cd --- /dev/null +++ b/spec/unit/config_spec.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +# (c) Copyright 2021 Ribose Inc. +# + +RSpec.describe Glossarist::Config do + subject { described_class.instance } + + it "is a singleton class" do + expect(subject).to eq(described_class.instance) + end + + context ".register_extension_attributes" do + before(:each) do + @extension_attributes = subject.extension_attributes.dup + end + + after(:each) do + described_class.register_extension_attributes(@extension_attributes) + end + + it "registers extension attributes" do + expect { described_class.register_extension_attributes(["foo", "bar"]) } + .to change { described_class.extension_attributes } + .from([]) + .to(["foo", "bar"]) + end + end + + context ".register_class" do + before(:each) do + @registered_classes = subject.registered_classes.dup + end + + after(:each) do + subject.instance_variable_set(:@registered_classes, @registered_classes) + end + + it "registers custom classes with string names" do + expect { described_class.register_class("foo", Array) } + .to change { described_class.class_for("foo") } + .from(nil) + .to(Array) + end + + it "registers custom classes with symbol names" do + expect { described_class.register_class(:foo, Array) } + .to change { described_class.class_for(:foo) } + .from(nil) + .to(Array) + end + end +end