lib/motion-resource/associations.rb in motion-resource-0.0.1 vs lib/motion-resource/associations.rb in motion-resource-0.0.2

- old
+ new

@@ -22,21 +22,25 @@ define_method name do |&block| if block.nil? instance_variable_get("@#{name}") || [] else - cached = instance_variable_get("@#{name}") - block.call(cached) and return if cached + if cached = instance_variable_get("@#{name}") + cached_response = instance_variable_get("@#{name}_response") + MotionResource::Base.request_block_call(block, cached, cached_response) + return + end - Object.const_get(name.to_s.classify).find_all(params.call(self)) do |results| + Object.const_get(name.to_s.classify).find_all(params.call(self)) do |results, response| if results && results.first && results.first.respond_to?("#{backwards_association}=") results.each do |result| result.send("#{backwards_association}=", self) end end instance_variable_set("@#{name}", results) - block.call(results) + instance_variable_set("@#{name}_response", response) + MotionResource::Base.request_block_call(block, results, response) end end end define_method "#{name}=" do |array| @@ -57,15 +61,19 @@ def belongs_to(name, params = lambda { |o| Hash.new }) define_method name do |&block| if block.nil? instance_variable_get("@#{name}") else - cached = instance_variable_get("@#{name}") - block.call(cached) and return if cached - - Object.const_get(name.to_s.classify).find(self.send("#{name}_id"), params.call(self)) do |result| + if cached = instance_variable_get("@#{name}") + cached_response = instance_variable_get("@#{name}_response") + MotionResource::Base.request_block_call(block, cached, cached_response) + return + end + + Object.const_get(name.to_s.classify).find(self.send("#{name}_id"), params.call(self)) do |result, response| instance_variable_set("@#{name}", result) - block.call(result) + instance_variable_set("@#{name}_response", response) + MotionResource::Base.request_block_call(block, result, response) end end end define_method "#{name}=" do |value|