lib/ridley/resource.rb in ridley-0.7.0.beta vs lib/ridley/resource.rb in ridley-0.7.0.rc1

- old
+ new

@@ -50,98 +50,92 @@ def set_chef_json_class(klass) @chef_json_class = klass attribute(:json_class, default: klass) end - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # # @return [Array<Object>] - def all(connection) - connection.get(self.resource_path).body.collect do |identity, location| - new(connection, self.chef_id => identity) + def all(client) + client.connection.get(self.resource_path).body.collect do |identity, location| + new(client, self.chef_id => identity) end end - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # @param [String, #chef_id] object # # @return [nil, Object] - def find(connection, object) - find!(connection, object) + def find(client, object) + find!(client, object) rescue Errors::HTTPNotFound nil end - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # @param [String, #chef_id] object # # @raise [Errors::HTTPNotFound] # if a resource with the given chef_id is not found # # @return [Object] - def find!(connection, object) + def find!(client, object) chef_id = object.respond_to?(:chef_id) ? object.chef_id : object - new(connection, connection.get("#{self.resource_path}/#{chef_id}").body) + new(client, client.connection.get("#{self.resource_path}/#{chef_id}").body) end - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # @param [#to_hash] object # # @return [Object] - def create(connection, object) - resource = new(connection, object.to_hash) - new_attributes = connection.post(self.resource_path, resource.to_json).body + def create(client, object) + resource = new(client, object.to_hash) + new_attributes = client.connection.post(self.resource_path, resource.to_json).body resource.attributes = resource.attributes.deep_merge(new_attributes) resource end - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # @param [String, #chef_id] object # # @return [Object] - def delete(connection, object) + def delete(client, object) chef_id = object.respond_to?(:chef_id) ? object.chef_id : object - new(connection, connection.delete("#{self.resource_path}/#{chef_id}").body) + new(client, client.connection.delete("#{self.resource_path}/#{chef_id}").body) end - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # # @return [Array<Object>] - def delete_all(connection) + def delete_all(client) mutex = Mutex.new deleted = [] - resources = all(connection) - connection.thread_count.times.collect do - Thread.new(connection, resources, deleted) do |connection, resources, deleted| - while resource = mutex.synchronize { resources.pop } - result = delete(connection, resource) - mutex.synchronize { deleted << result } - end - end - end.each(&:join) - - deleted + all(client).collect do |resource| + Celluloid::Future.new { + delete(client, resource) + } + end.map(&:value) end - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # @param [#to_hash] object # # @return [Object] - def update(connection, object) - resource = new(connection, object.to_hash) - new(connection, connection.put("#{self.resource_path}/#{resource.chef_id}", resource.to_json).body) + def update(client, object) + resource = new(client, object.to_hash) + new(client, client.connection.put("#{self.resource_path}/#{resource.chef_id}", resource.to_json).body) end end include Chozo::VariaModel include Comparable - # @param [Ridley::Connection] connection + # @param [Ridley::Client] client # @param [Hash] new_attrs - def initialize(connection, new_attrs = {}) - @connection = connection + def initialize(client, new_attrs = {}) + @client = client mass_assign(new_attrs) end # Creates a resource on the target remote or updates one if the resource # already exists. @@ -151,11 +145,11 @@ # # @return [Boolean] def save raise Errors::InvalidResource.new(self.errors) unless valid? - mass_assign(self.class.create(connection, self).attributes) + mass_assign(self.class.create(client, self).attributes) true rescue Errors::HTTPConflict self.update true end @@ -168,19 +162,19 @@ # # @return [Boolean] def update raise Errors::InvalidResource.new(self.errors) unless valid? - mass_assign(self.class.update(connection, self).attributes) + mass_assign(self.class.update(client, self).attributes) true end # Reload the attributes of the instantiated resource # # @return [Object] def reload - mass_assign(self.class.find(connection, self).attributes) + mass_assign(self.class.find(client, self).attributes) self end # @return [String] def chef_id @@ -213,8 +207,8 @@ self.chef_id.hash end private - attr_reader :connection + attr_reader :client end end