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>
91 92 93 94 95 |
# File 'lib/ridley/resource.rb', line 91 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
80 81 82 83 84 85 86 |
# File 'lib/ridley/resource.rb', line 80 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
71 72 73 |
# File 'lib/ridley/resource.rb', line 71 def attribute_defaults @attribute_defaults ||= HashWithIndifferentAccess.new end |
#attributes ⇒ Set
66 67 68 |
# File 'lib/ridley/resource.rb', line 66 def attributes @attributes ||= Set.new end |
#chef_id ⇒ String?
16 17 18 |
# File 'lib/ridley/resource.rb', line 16 def chef_id @chef_id end |
#chef_json_class ⇒ String?
53 54 55 |
# File 'lib/ridley/resource.rb', line 53 def chef_json_class @chef_json_class end |
#chef_type ⇒ String
40 41 42 |
# File 'lib/ridley/resource.rb', line 40 def chef_type @chef_type ||= self.class.name.underscore end |
#create(connection, object) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/ridley/resource.rb', line 123 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.deep_merge(new_attributes) resource end |
#delete(connection, object) ⇒ Object
134 135 136 137 |
# File 'lib/ridley/resource.rb', line 134 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>
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ridley/resource.rb', line 142 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
101 102 103 104 105 |
# File 'lib/ridley/resource.rb', line 101 def find(connection, object) find!(connection, object) rescue Errors::HTTPNotFound nil end |
#find!(connection, object) ⇒ Object
114 115 116 117 |
# File 'lib/ridley/resource.rb', line 114 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
28 29 30 |
# File 'lib/ridley/resource.rb', line 28 def resource_path @resource_path ||= self.chef_type.pluralize end |
#set_chef_id(identifier) ⇒ String
23 24 25 |
# File 'lib/ridley/resource.rb', line 23 def set_chef_id(identifier) @chef_id = identifier.to_sym end |
#set_chef_json_class(klass) ⇒ String
60 61 62 63 |
# File 'lib/ridley/resource.rb', line 60 def set_chef_json_class(klass) @chef_json_class = klass attribute(:json_class, default: klass) end |
#set_chef_type(type) ⇒ String
47 48 49 50 |
# File 'lib/ridley/resource.rb', line 47 def set_chef_type(type) @chef_type = type.to_s attribute(:chef_type, default: type) end |
#set_resource_path(path) ⇒ String
35 36 37 |
# File 'lib/ridley/resource.rb', line 35 def set_resource_path(path) @resource_path = path end |
#update(connection, object) ⇒ Object
163 164 165 166 |
# File 'lib/ridley/resource.rb', line 163 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 |