Skip to content

Commit

Permalink
Merge 5dc4f06 into 6812899
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 18, 2015
2 parents 6812899 + 5dc4f06 commit cac8710
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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

0 comments on commit cac8710

Please sign in to comment.