Skip to content

Commit

Permalink
updating authoritative source to add to sources (#100)
Browse files Browse the repository at this point in the history
* updating authoritative source to add to sources

* updated specs

* if we have both authoritative_source and sources then skip authoritative_source
  • Loading branch information
HassanAkbar authored Mar 28, 2024
1 parent c24bc32 commit 285f10e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/glossarist/concept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def id=(id)
# removed.
# @return [Array<ConceptSource>]
attr_reader :sources
alias :authoritative_source :sources

# return [Array<ConceptDate>]
attr_reader :dates
Expand Down Expand Up @@ -132,9 +131,13 @@ def sources=(sources)
sources&.each { |source| @sources << source }
end

def authoritative_source
@sources.select { |source| source.authoritative? }
end

def authoritative_source=(sources)
self.sources = sources&.map do |source|
source.merge({ "type" => "authoritative" })
sources&.each do |source|
@sources << source.merge({ "type" => "authoritative" })
end
end

Expand Down Expand Up @@ -205,6 +208,7 @@ def normalize_args(args)
data = arg.delete("data")

arg.merge!(data) if data
arg.delete("authoritative_source") if arg["sources"]
end
end
end
Expand Down
83 changes: 83 additions & 0 deletions spec/unit/concept_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
.to change { subject.id }.to("456")
end

it "accepts integers as ids" do
expect { subject.id = 456 }
.to change { subject.id }.to(456)
end

describe "#to_h" do
it "dumps concept definition to a hash" do
object = described_class.new(
Expand Down Expand Up @@ -119,4 +124,82 @@
expect(retval.sources.first.origin.to_h).to eq({ "ref" => "url" })
end
end

describe "#authoritative_source" do
let(:attrs) do
{
id: "123",
"sources" => [
{
"type" => "authoritative",
"status" => "identical",
"origin" => { "text" => "url" },
},
{
"type" => "lineage",
"status" => "identical",
"origin" => { "text" => "url" },
},
],
}
end

let(:authoritative_source) do
[
{
"type" => "authoritative",
"status" => "identical",
"origin" => {
"ref" => "url",
},
},
]
end

it "should return only authoritative_sources" do
expect(subject.authoritative_source.map(&:to_h))
.to eq(authoritative_source)
end
end

describe "#authoritative_source=" do
let(:sources) do
[
{
"type" => "authoritative",
"status" => "identical",
"origin" => { "ref" => "url" },
},
{
"type" => "lineage",
"status" => "identical",
"origin" => { "ref" => "url" },
},
]
end

let(:attrs) do
{
id: "123",
"sources" => sources,
}
end

let(:authoritative_source) do
{
"status" => "identical",
"origin" => {
"ref" => "new url",
},
}
end

it "should add to sources hash" do

expect { subject.authoritative_source = [authoritative_source] }
.to change { subject.sources.map(&:to_h) }
.from(sources)
.to(sources + [authoritative_source.merge("type" => "authoritative")])
end
end
end

0 comments on commit 285f10e

Please sign in to comment.