From 40249848d6bde949e4ef925d57668442c2c2c299 Mon Sep 17 00:00:00 2001 From: msalvadores Date: Mon, 28 Jan 2013 12:32:20 -0800 Subject: [PATCH] 'Find' does not load any attributes. Consistent with 'where' (close #53) --- lib/goo/base/resource.rb | 24 ++++++++++++++++++------ test/test_model_find.rb | 2 ++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/goo/base/resource.rb b/lib/goo/base/resource.rb index 2775ab56..f833d2d7 100644 --- a/lib/goo/base/resource.rb +++ b/lib/goo/base/resource.rb @@ -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 @@ -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 diff --git a/test/test_model_find.rb b/test/test_model_find.rb index db602fba..d61cccfd 100644 --- a/test/test_model_find.rb +++ b/test/test_model_find.rb @@ -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