Skip to content

Commit

Permalink
Allow PersistenceStrategy set by property config
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Johnson authored and elrayle committed Apr 7, 2016
1 parent 52f14c1 commit e57b5cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/active_triples/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ def make_node(value)
value = RDF::Node.new if value.nil?
node = node_cache[value] if node_cache[value]
node ||= klass.from_uri(value,parent)
node.set_persistence_strategy(property_config[:persist_to]) if
is_property? && property_config[:persist_to]
return nil if (is_property? && property_config[:class_name]) && (class_for_value(value) != class_for_property)
self.node_cache[value] ||= node
node
Expand Down
39 changes: 29 additions & 10 deletions spec/active_triples/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,24 @@ class WithTitle
expect(subject.result.map(&:rdf_subject))
.to contain_exactly(*values)
end

context 'and persistence_strategy is configured' do
before do
reflections
.property :moomin,
predicate: RDF::URI('http://example.org/moomin'),
persist_to: ActiveTriples::RepositoryStrategy
end

it 'assigns persistence strategy' do
subject.result.each do |node|
expect(node.persistence_strategy)
.to be_a ActiveTriples::RepositoryStrategy
end
end
end

context 'when #cast? is false' do
context 'and #cast? is false' do
let(:values) do
[uri, RDF::URI('http://ex.org/too-ticky'), RDF::Node.new,
'moomin', Date.today]
Expand Down Expand Up @@ -478,16 +494,19 @@ class WithTitle
include_context 'with symbol property' do
let(:parent_resource) { ActiveTriples::Resource.new }
end

context 'and persistence config' do
before do
reflections
.property :moomin,
predicate: RDF::URI('http://example.org/moomin'),
persist_to: ActiveTriples::RepositoryStrategy
end

it 'sets a value' do
expect { subject.set(:moomin) }
.to change { subject.to_a }.to contain_exactly(:moomin)
end

it 'sets mulitple values' do
values = [:moomin, :snork]
expect { subject.set(values) }
.to change { subject.to_a }.to contain_exactly(*values)
it 'returns values with persistence strategy set' do
expect(subject.set(RDF::Node.new).map(&:persistence_strategy))
.to contain_exactly(an_instance_of(ActiveTriples::RepositoryStrategy))
end
end
end
end
Expand Down

0 comments on commit e57b5cf

Please sign in to comment.