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

Returns a new instance of Resource.

Parameters:

  • connection_registry (Celluloid::Registry)


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

Returns:

  • (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

Parameters:

  • path (String)

Returns:

  • (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>

Parameters:

Returns:

  • (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

#connectionRidley::Connection

Returns:



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

def connection
  @connection_registry[:connection_pool]
end

#create(object) ⇒ Object

Parameters:

  • object (#to_hash)

Returns:

  • (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?

Parameters:

  • object (String, #chef_id)

Returns:

  • (Object, nil)


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

Returns:

  • (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?

Parameters:

  • object (String, #chef_id)

Returns:

  • (Object, nil)


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?

Parameters:

  • object (#to_hash)

Returns:

  • (Object, nil)


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