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

Don't reload on initialize. Fixes #87 #152

Open
wants to merge 1 commit into
base: v0.7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/active_triples/rdf_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def initialize(*args, &block)
@graph = RDF::Graph.new(*args, &block)
set_subject!(resource_uri) if resource_uri

reload
@loaded = false
# Append type to graph if necessary.
Array(self.class.type).each do |type|
unless self.get_values(:type).include?(type)
Expand Down Expand Up @@ -285,8 +285,10 @@ def persisted?
def reload
@relation_cache ||= {}
return false unless repository
@loaded = true
self << repository.query(subject: rdf_subject)
unless empty?
# Default types are added on initialize, so check to see if it's more than just types
if has_statements_besides_default_types?
@persisted = true
end
true
Expand All @@ -304,6 +306,7 @@ def reload
#
# @note This method will delete existing statements with the correct subject and predicate from the graph
def set_value(*args)
reload unless loaded?
# Add support for legacy 3-parameter syntax
if args.length > 3 || args.length < 2
raise ArgumentError, "wrong number of arguments (#{args.length} for 2-3)"
Expand All @@ -312,6 +315,12 @@ def set_value(*args)
get_relation(args).set(values)
end

##
# Returns true if the object has been loaded from the repository
def loaded?
@loaded
end

##
# Adds or updates a property with supplied values.
#
Expand Down Expand Up @@ -345,6 +354,7 @@ def [](uri_or_term_property)


def get_relation(args)
reload unless loaded?
@relation_cache ||= {}
rel = Relation.new(self, args)
@relation_cache["#{rel.send(:rdf_subject)}/#{rel.property}/#{rel.rel_args}"] ||= rel
Expand Down Expand Up @@ -436,6 +446,12 @@ def erase_old_resource

private

##
# Returns true if the graph has more than the default types added on initialize
def has_statements_besides_default_types?
count > query(subject: rdf_subject, predicate: RDF.type).count
end

##
# Returns the properties registered and their configurations.
#
Expand Down Expand Up @@ -543,12 +559,14 @@ module ClassMethods
#
# @return [ActiveTriples::Entity] a Resource with the given uri
def from_uri(uri, vals = nil)
new(uri, vals)
new(uri, vals).tap do |o|
o.reload
end
end

##
# Apply a predicate mapping using a given strategy.
#
#
# @param [ActiveTriples::Schema, #properties] schema A schema to apply.
# @param [#apply!] strategy A strategy for applying. Defaults
# to ActiveTriples::ExtensionStrategy
Expand Down
2 changes: 1 addition & 1 deletion spec/active_triples/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class DummyResourceWithBaseURI < ActiveTriples::Resource

it "should warn when the repo doesn't exist" do
allow(DummyLicense).to receive(:repository).and_return('repo2')
expect { subject }.to raise_error ActiveTriples::RepositoryNotFoundError, 'The class DummyLicense expects a repository called repo2, but none was declared'
expect { subject.title }.to raise_error ActiveTriples::RepositoryNotFoundError, 'The class DummyLicense expects a repository called repo2, but none was declared'
end
end

Expand Down