Module: Ridley::Resource::ClassMethods
- Defined in:
- lib/ridley/resource.rb
Instance Method Summary collapse
- #all(connection) ⇒ Array<Object>
- #attribute(name, options = {}) ⇒ Set
- #attribute_defaults ⇒ Hash
- #attributes ⇒ Set
- #chef_id ⇒ String?
- #chef_json_class ⇒ String?
- #chef_type ⇒ String
- #create(connection, object) ⇒ Object
- #delete(connection, object) ⇒ Object
- #delete_all(connection) ⇒ Array<Object>
- #find(connection, object) ⇒ nil, Object
- #find!(connection, object) ⇒ Object
- #resource_path ⇒ String
- #set_chef_id(identifier) ⇒ String
- #set_chef_json_class(klass) ⇒ String
- #set_chef_type(type) ⇒ String
- #set_resource_path(path) ⇒ String
- #update(connection, object) ⇒ Object
Instance Method Details
#all(connection) ⇒ Array<Object>
90 91 92 93 94 |
# File 'lib/ridley/resource.rb', line 90 def all(connection) connection.get(self.resource_path).body.collect do |identity, location| new(connection, self.chef_id => identity) end end |
#attribute(name, options = {}) ⇒ Set
79 80 81 82 83 84 85 |
# File 'lib/ridley/resource.rb', line 79 def attribute(name, = {}) if .has_key?(:default) default_for_attribute(name, [:default]) end define_attribute_method(name) attributes << name.to_sym end |
#attribute_defaults ⇒ Hash
70 71 72 |
# File 'lib/ridley/resource.rb', line 70 def attribute_defaults @attribute_defaults ||= Hash.new end |
#attributes ⇒ Set
65 66 67 |
# File 'lib/ridley/resource.rb', line 65 def attributes @attributes ||= Set.new end |
#chef_id ⇒ String?
15 16 17 |
# File 'lib/ridley/resource.rb', line 15 def chef_id @chef_id end |
#chef_json_class ⇒ String?
52 53 54 |
# File 'lib/ridley/resource.rb', line 52 def chef_json_class @chef_json_class end |
#chef_type ⇒ String
39 40 41 |
# File 'lib/ridley/resource.rb', line 39 def chef_type @chef_type ||= self.class.name.underscore end |
#create(connection, object) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/ridley/resource.rb', line 122 def create(connection, object) resource = new(connection, object.to_hash) new_attributes = connection.post(self.resource_path, resource.to_json).body resource.attributes = resource.attributes.merge(new_attributes) resource end |
#delete(connection, object) ⇒ Object
133 134 135 136 |
# File 'lib/ridley/resource.rb', line 133 def delete(connection, object) chef_id = object.respond_to?(:chef_id) ? object.chef_id : object new(connection, connection.delete("#{self.resource_path}/#{chef_id}").body) end |
#delete_all(connection) ⇒ Array<Object>
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ridley/resource.rb', line 141 def delete_all(connection) 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 end |
#find(connection, object) ⇒ nil, Object
100 101 102 103 104 |
# File 'lib/ridley/resource.rb', line 100 def find(connection, object) find!(connection, object) rescue Errors::HTTPNotFound nil end |
#find!(connection, object) ⇒ Object
113 114 115 116 |
# File 'lib/ridley/resource.rb', line 113 def find!(connection, object) chef_id = object.respond_to?(:chef_id) ? object.chef_id : object new(connection, connection.get("#{self.resource_path}/#{chef_id}").body) end |
#resource_path ⇒ String
27 28 29 |
# File 'lib/ridley/resource.rb', line 27 def resource_path @resource_path ||= self.chef_type.pluralize end |
#set_chef_id(identifier) ⇒ String
22 23 24 |
# File 'lib/ridley/resource.rb', line 22 def set_chef_id(identifier) @chef_id = identifier.to_sym end |
#set_chef_json_class(klass) ⇒ String
59 60 61 62 |
# File 'lib/ridley/resource.rb', line 59 def set_chef_json_class(klass) @chef_json_class = klass attribute(:json_class, default: klass) end |
#set_chef_type(type) ⇒ String
46 47 48 49 |
# File 'lib/ridley/resource.rb', line 46 def set_chef_type(type) @chef_type = type.to_s attribute(:chef_type, default: type) end |
#set_resource_path(path) ⇒ String
34 35 36 |
# File 'lib/ridley/resource.rb', line 34 def set_resource_path(path) @resource_path = path end |
#update(connection, object) ⇒ Object
162 163 164 165 |
# File 'lib/ridley/resource.rb', line 162 def update(connection, object) resource = new(connection, object.to_hash) new(connection, connection.put("#{self.resource_path}/#{resource.chef_id}", resource.to_json).body) end |