Skip to content

Commit

Permalink
Don't reload on initialize. Fixes #87
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Mar 6, 2015
1 parent 91dc9c0 commit 354e9e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/active_triples/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def type_registry
#
# @return [ActiveTriples::Resource] a Resource with
def from_uri(uri,vals=nil)
new(uri, vals)
new(uri, vals).tap do |o|
o.reload
end
end

def property(*)
Expand Down Expand Up @@ -83,8 +85,8 @@ def initialize(*args, &block)
self.parent = args.shift unless args.first.is_a?(Hash)
set_subject!(resource_uri) if resource_uri
super(*args, &block)
reload
# Append type to graph if necessary.
@loaded = false
self.get_values(:type) << self.class.type if self.class.type.kind_of?(RDF::URI) && type.empty?
end

Expand Down Expand Up @@ -265,6 +267,7 @@ def persisted?
#
# @return [true, false]
def reload
@loaded = true
@term_cache ||= {}
return false unless repository
self << repository.query(subject: rdf_subject)
Expand All @@ -286,6 +289,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 @@ -294,6 +298,10 @@ def set_value(*args)
get_term(args).set(values)
end

def loaded?
@loaded
end

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


def get_term(args)
reload unless loaded?
@term_cache ||= {}
term = Term.new(self, args)
@term_cache["#{term.send(:rdf_subject)}/#{term.property}/#{term.term_args}"] ||= term
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

0 comments on commit 354e9e7

Please sign in to comment.