Skip to content

Commit

Permalink
'Find' does not load any attributes. Consistent with 'where' (close #53)
Browse files Browse the repository at this point in the history
  • Loading branch information
msalvadores committed Jan 28, 2013
1 parent 302e9a8 commit 4024984
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/goo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,8 @@ def load(resource_id=nil)
raise ArgumentError,
"ResourceID '#{resource_id}' is an instance of type #{model_class} in the store"
end

store_attributes = Goo::Queries.get_resource_attributes(resource_id, self.class,
internals.store_name)
internals.store_name)
internal_status = @attributes[:internals]
@attributes = store_attributes
@attributes[:internals] = internal_status
Expand Down Expand Up @@ -409,24 +408,37 @@ def self.where(*args)

def self.find(param, store_name=nil)

if self.goop_settings[:unique][:fields].nil?
if (self.goop_settings[:unique][:fields].nil? or
self.goop_settings[:unique][:fields].length != 1)
mess = "The call #{self.name}.find cannot be used " +
" if the model has no `:unique => true` attributes"
raise ArgumentError, mess
end

key_attribute = goop_settings[:unique][:fields][0]

if param.kind_of? String
iri = RDF::IRI.new(self.prefix + param)
ins = self.where key_attribute => param
if ins.length > 1
raise ArgumentError,
"Inconsistent model behaviour. There are #{ins.length} instance with #{key_attribute} => #{param}"
end
return nil if ins.length == 0
return ins[0]
elsif param.kind_of? RDF::IRI
iri = param
inst = self.new
inst.internals.lazy_loaded
inst.resource_id = param
return inst
else
raise ArgumentError,
"#{self.class.name}.find only accepts String or RDF::IRI as input."
end
return self.load(iri)
return self.load(iri,store_name,false)
end

def self.load(resource_id, store_name=nil)
def self.load(resource_id, store_name=nil,load_attributes=true)
model_class = Queries.get_resource_class(resource_id, store_name)
if model_class.nil?
return nil
Expand Down
2 changes: 2 additions & 0 deletions test/test_model_find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ def test_where_with_nested
def test_find
create_toy_parts()
white = Color.find("white")
white.load unless white.loaded?
assert_instance_of Color, white
assert white.resource_id.value.end_with? "white"
assert_equal "white", white.code

iri_blue = Color.prefix + "blue"
blue = Color.find(RDF::IRI.new(iri_blue))
blue.load unless blue.loaded?
assert_instance_of Color, blue
assert blue.resource_id.value.end_with? "blue"
assert_equal "blue", blue.code
Expand Down

0 comments on commit 4024984

Please sign in to comment.