Module: Docker::Model::ClassMethods

Includes:
Error
Defined in:
lib/docker/model.rb

Overview

This defines the DSL for the including Classes.

Instance Method Summary collapse

Instance Method Details

#all(options = {}, connection = Docker.connection) ⇒ Object

Retrieve every Instance of a model for the given server.



72
73
74
75
76
# File 'lib/docker/model.rb', line 72

def all(options = {}, connection = Docker.connection)
  path = "#{resource_prefix}/json"
  hashes = connection.json_request(:get, path, options) || []
  hashes.map { |hash| new(:id => hash['Id'], :connection => connection) }
end

#create(opts = {}, conn = Docker.connection) ⇒ Object

Create a Model with the specified body. Raises a Docker::Error::ArgumentError if the argument is not a Hash. Otherwise, instances execs the Class’s #create_request method with the single argument.



66
67
68
69
# File 'lib/docker/model.rb', line 66

def create(opts = {}, conn = Docker.connection)
  raise Docker::Error::ArgumentError, 'Expected a Hash' if !opts.is_a?(Hash)
  new(:connection => conn).instance_exec(opts, &create_request)
end

#create_request(&block) ⇒ Object

Define how the Model should send a create request to the server.



43
44
45
# File 'lib/docker/model.rb', line 43

def create_request(&block)
  block.nil? ? @create_request : (@create_request = block)
end

#request(method, action, opts = {}, &outer_block) ⇒ Object

Define a method named action that sends an http method request to the Docker Server.



49
50
51
52
53
54
55
56
# File 'lib/docker/model.rb', line 49

def request(method, action, opts = {}, &outer_block)
  define_method(action) do |query = nil, &block|
    path = opts[:path]
    path ||= "#{self.class.send(:resource_prefix)}/#{self.id}/#{action}"
    body = self.connection.json_request(method, path, query, &block)
    outer_block.nil? ? body : instance_exec(body, &outer_block)
  end
end

#resource_prefix(val = nil) ⇒ Object

Define the Model’s prefix for all requests.



38
39
40
# File 'lib/docker/model.rb', line 38

def resource_prefix(val = nil)
  val.nil? ? @resource_prefix : (@resource_prefix = val)
end