Class: Ridley::Resource

Inherits:
Object
  • Object
show all
Includes:
Chozo::VariaModel, Comparable
Defined in:
lib/ridley/resource.rb

Overview

Author:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, new_attrs = {}) ⇒ Resource


137
138
139
140
# File 'lib/ridley/resource.rb', line 137

def initialize(client, new_attrs = {})
  @client = client
  mass_assign(new_attrs)
end

Class Method Details

.all(client) ⇒ Array<Object>


58
59
60
61
62
# File 'lib/ridley/resource.rb', line 58

def all(client)
  client.connection.get(self.resource_path).body.collect do |identity, location|
    new(client, self.chef_id => identity)
  end
end

.chef_idString?


6
7
8
# File 'lib/ridley/resource.rb', line 6

def chef_id
  @chef_id
end

.chef_json_classString?


43
44
45
# File 'lib/ridley/resource.rb', line 43

def chef_json_class
  @chef_json_class
end

.chef_typeString


30
31
32
# File 'lib/ridley/resource.rb', line 30

def chef_type
  @chef_type ||= self.class.name.underscore
end

.create(client, object) ⇒ Object


92
93
94
95
96
97
# File 'lib/ridley/resource.rb', line 92

def create(client, object)
  resource = new(client, object.to_hash)
  new_attributes = client.connection.post(self.resource_path, resource.to_json).body
  resource.mass_assign(resource._attributes_.deep_merge(new_attributes))
  resource
end

.delete(client, object) ⇒ Object


103
104
105
106
# File 'lib/ridley/resource.rb', line 103

def delete(client, object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(client, client.connection.delete("#{self.resource_path}/#{chef_id}").body)
end

.delete_all(client) ⇒ Array<Object>


111
112
113
114
115
116
117
118
119
120
# File 'lib/ridley/resource.rb', line 111

def delete_all(client)
  mutex = Mutex.new
  deleted = []

  all(client).collect do |resource|
    Celluloid::Future.new {
      delete(client, resource)
    }
  end.map(&:value)
end

.find(client, object) ⇒ nil, Object


68
69
70
71
72
# File 'lib/ridley/resource.rb', line 68

def find(client, object)
  find!(client, object)
rescue Errors::ResourceNotFound
  nil
end

.find!(client, object) ⇒ Object

Raises:


81
82
83
84
85
86
# File 'lib/ridley/resource.rb', line 81

def find!(client, object)
  chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
  new(client, client.connection.get("#{self.resource_path}/#{chef_id}").body)
rescue Errors::HTTPNotFound => ex
  raise Errors::ResourceNotFound, ex
end

.resource_pathString


18
19
20
# File 'lib/ridley/resource.rb', line 18

def resource_path
  @resource_path ||= self.chef_type.pluralize
end

.set_chef_id(identifier) ⇒ String


13
14
15
# File 'lib/ridley/resource.rb', line 13

def set_chef_id(identifier)
  @chef_id = identifier.to_sym
end

.set_chef_json_class(klass) ⇒ String


50
51
52
53
# File 'lib/ridley/resource.rb', line 50

def set_chef_json_class(klass)
  @chef_json_class = klass
  attribute(:json_class, default: klass)
end

.set_chef_type(type) ⇒ String


37
38
39
40
# File 'lib/ridley/resource.rb', line 37

def set_chef_type(type)
  @chef_type = type.to_s
  attribute(:chef_type, default: type)
end

.set_resource_path(path) ⇒ String


25
26
27
# File 'lib/ridley/resource.rb', line 25

def set_resource_path(path)
  @resource_path = path
end

.update(client, object) ⇒ Object


126
127
128
129
# File 'lib/ridley/resource.rb', line 126

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

Instance Method Details

#<=>(other) ⇒ Boolean


193
194
195
# File 'lib/ridley/resource.rb', line 193

def <=>(other)
  self.chef_id <=> other.chef_id
end

#==(other) ⇒ Object


197
198
199
# File 'lib/ridley/resource.rb', line 197

def ==(other)
  self.chef_id == other.chef_id
end

#chef_idString


182
183
184
# File 'lib/ridley/resource.rb', line 182

def chef_id
  get_attribute(self.class.chef_id)
end

#eql?(other) ⇒ Boolean


204
205
206
# File 'lib/ridley/resource.rb', line 204

def eql?(other)
  self.class == other.class && self == other
end

#hashObject


208
209
210
# File 'lib/ridley/resource.rb', line 208

def hash
  self.chef_id.hash
end

#reloadObject

Reload the attributes of the instantiated resource


176
177
178
179
# File 'lib/ridley/resource.rb', line 176

def reload
  mass_assign(self.class.find(client, self)._attributes_)
  self
end

#saveBoolean

Creates a resource on the target remote or updates one if the resource already exists.

Raises:


149
150
151
152
153
154
155
156
157
# File 'lib/ridley/resource.rb', line 149

def save
  raise Errors::InvalidResource.new(self.errors) unless valid?

  mass_assign(self.class.create(client, self)._attributes_)
  true
rescue Errors::HTTPConflict
  self.update
  true
end

#to_sObject


186
187
188
# File 'lib/ridley/resource.rb', line 186

def to_s
  "#{self.chef_id}: #{self._attributes_}"
end

#updateBoolean

Updates the instantiated resource on the target remote with any changes made to self

Raises:


166
167
168
169
170
171
# File 'lib/ridley/resource.rb', line 166

def update
  raise Errors::InvalidResource.new(self.errors) unless valid?

  mass_assign(self.class.update(client, self)._attributes_)
  true
end