Class: Ridley::Resource

Inherits:
Object
  • 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



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

def initialize(connection_registry)
  @connection_registry = connection_registry
end

Class Method Details

.representationObject

Raises:

  • (RuntimeError)


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_pathString



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

def resource_path
  @resource_path ||= representation.chef_type
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

#allArray<Object>



65
66
67
68
69
# File 'lib/ridley/resource.rb', line 65

def all
  request(:get, self.class.resource_path).collect do |identity, location|
    new(self.class.representation.chef_id => identity)
  end
end

#connectionRidley::Connection



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

def connection
  @connection_registry[:connection_pool]
end

#create(object) ⇒ Object



85
86
87
88
89
90
# File 'lib/ridley/resource.rb', line 85

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?



95
96
97
98
99
100
101
# File 'lib/ridley/resource.rb', line 95

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_allArray<Object>



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

def delete_all
  all.collect { |resource| future(:delete, resource) }.map(&:value)
end

#find(object) ⇒ Object?



74
75
76
77
78
79
80
# File 'lib/ridley/resource.rb', line 74

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

#from_file(filename) ⇒ representation.class

Used to build a representation from a file with the current Actor’s resource



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

def from_file(filename)
  from_json(File.read(filename))
end

#from_json(json) ⇒ representation.class

Used to build a representation from a serialized json string with the current Actor’s resource



53
54
55
# File 'lib/ridley/resource.rb', line 53

def from_json(json)
  new(JSON.parse(json))
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?



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

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