Skip to content

Commit

Permalink
Retain Literal equality when setting a relation
Browse files Browse the repository at this point in the history
Fixes the bug in #245 causing literal languges to be lost when setting
values to an existing `Relation` (including self).
  • Loading branch information
Tom Johnson committed Aug 19, 2016
1 parent 5fad51d commit 67e542f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/active_triples/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def <=>(other)
#
# @return [Relation] a relation containing the set values; i.e. `self`
def <<(values)
values = to_a | Array.wrap(values)
values = values.objects.to_a if values.is_a?(Relation)
values = objects.to_a | Array.wrap(values)
self.set(values)
end
alias_method :push, :<<
Expand Down Expand Up @@ -380,7 +381,7 @@ def property
# non-{RDF::Resource} values.
def set(values)
raise UndefinedPropertyError.new(property, reflections) if predicate.nil?
values = values.to_a if values.is_a? Relation
values = values.objects.to_a if values.is_a? Relation
values = [values].compact unless values.kind_of?(Array)

clear
Expand Down
47 changes: 47 additions & 0 deletions spec/active_triples/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,38 @@ class WithTitle
expect { subject << values }
.to change { subject.to_a }.to contain_exactly(*values)
end

it 'keeps datatypes' do
values = [RDF::Literal(Date.today), RDF::Literal(:moomin)]

expect { values.each { |v| subject << v } }
.to change { subject.send(:objects).to_a }
.to contain_exactly(*values)
end

it 'keeps languages' do
values = [RDF::Literal("Moomin", language: :en),
RDF::Literal("Mummi", language: :fi)]

expect { values.each { |v| subject << v } }
.to change { subject.send(:objects).to_a }
.to contain_exactly(*values)
end

context 'when given a Relation' do
it 'keeps datatypes and languages of values' do
values = [RDF::Literal(Date.today),
RDF::Literal(:moomin),
RDF::Literal("Moomin", language: :en),
RDF::Literal("Mummi", language: :fi)]

subject.set(values)
expect(subject.send(:objects)).to contain_exactly(*values)

expect { subject << subject }
.not_to change { subject.send(:objects).to_a }
end
end
end

describe '#predicate' do
Expand Down Expand Up @@ -714,6 +746,21 @@ class WithTitle
.to change { subject.to_a }.to contain_exactly(*values)
end

context 'when given a Relation' do
it 'keeps datatypes and languages of values' do
values = [RDF::Literal(Date.today),
RDF::Literal(:moomin),
RDF::Literal("Moomin", language: :en),
RDF::Literal("Mummi", language: :fi)]

subject.set(values)
expect(subject.send(:objects)).to contain_exactly(*values)

expect { subject.set(subject) }
.not_to change { subject.send(:objects).to_a }
end
end

context 'and persistence config' do
before do
reflections
Expand Down

0 comments on commit 67e542f

Please sign in to comment.