Class: Ridley::Resource
- Inherits:
-
Object
show all
- Includes:
- Celluloid
- Defined in:
- lib/ridley/resource.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(connection_registry) ⇒ Resource
Returns a new instance of Resource.
29
30
31
|
# File 'lib/ridley/resource.rb', line 29
def initialize(connection_registry)
@connection_registry = connection_registry
end
|
Class Method Details
.representation ⇒ Object
16
17
18
19
|
# File 'lib/ridley/resource.rb', line 16
def representation
return @representation if @representation
raise RuntimeError.new("no representation set")
end
|
.represented_by(klass) ⇒ Object
21
22
23
|
# File 'lib/ridley/resource.rb', line 21
def represented_by(klass)
@representation = klass
end
|
.resource_path ⇒ String
5
6
7
|
# File 'lib/ridley/resource.rb', line 5
def resource_path
@resource_path ||= representation.chef_type.pluralize
end
|
.set_resource_path(path) ⇒ String
12
13
14
|
# File 'lib/ridley/resource.rb', line 12
def set_resource_path(path)
@resource_path = path
end
|
Instance Method Details
#all ⇒ Array<Object>
45
46
47
48
49
|
# File 'lib/ridley/resource.rb', line 45
def all
request(:get, self.class.resource_path).collect do |identity, location|
new(self.class.representation.chef_id => identity)
end
end
|
38
39
40
|
# File 'lib/ridley/resource.rb', line 38
def connection
@connection_registry[:connection_pool]
end
|
#create(object) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/ridley/resource.rb', line 65
def create(object)
resource = new(object.to_hash)
new_attributes = request(:post, self.class.resource_path, resource.to_json)
resource.mass_assign(resource._attributes_.deep_merge(new_attributes))
resource
end
|
#delete(object) ⇒ Object?
75
76
77
78
79
80
81
|
# File 'lib/ridley/resource.rb', line 75
def delete(object)
chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
new(request(:delete, "#{self.class.resource_path}/#{chef_id}"))
rescue AbortError => ex
return nil if ex.cause.is_a?(Errors::HTTPNotFound)
abort(ex.cause)
end
|
#delete_all ⇒ Array<Object>
84
85
86
|
# File 'lib/ridley/resource.rb', line 84
def delete_all
all.collect { |resource| future(:delete, resource) }.map(&:value)
end
|
#find(object) ⇒ Object?
54
55
56
57
58
59
60
|
# File 'lib/ridley/resource.rb', line 54
def find(object)
chef_id = object.respond_to?(:chef_id) ? object.chef_id : object
new(request(:get, "#{self.class.resource_path}/#{chef_id}"))
rescue AbortError => ex
return nil if ex.cause.is_a?(Errors::HTTPNotFound)
abort(ex.cause)
end
|
#new(*args) ⇒ Object
33
34
35
|
# File 'lib/ridley/resource.rb', line 33
def new(*args)
self.class.representation.new(Actor.current, *args)
end
|
#update(object) ⇒ Object?
91
92
93
94
95
96
97
|
# File 'lib/ridley/resource.rb', line 91
def update(object)
resource = new(object.to_hash)
new(request(:put, "#{self.class.resource_path}/#{resource.chef_id}", resource.to_json))
rescue AbortError => ex
return nil if ex.cause.is_a?(Errors::HTTPConflict)
abort(ex.cause)
end
|