Module: Docker::Model::ClassMethods

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

Overview

This defines the DSL for the including Classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#create_requestObject (readonly)

Returns the value of attribute create_request.



34
35
36
# File 'lib/docker/model.rb', line 34

def create_request
  @create_request
end

#resource_prefixObject (readonly)

Returns the value of attribute resource_prefix.



34
35
36
# File 'lib/docker/model.rb', line 34

def resource_prefix
  @resource_prefix
end

Instance Method Details

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

Retrieve every Instance of a model for the given server.



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

def all(options = {}, connection = Docker.connection)
  path = "#{resource_prefix}/json"
  hashes = Docker::Util.parse_json(connection.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.



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

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

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

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



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

def request(method, action, opts = {}, &outer_block)
  define_method(action) do |query = nil, &block|
    new_opts = {
      :path => "#{self.class.resource_prefix}/#{self.id}/#{action}",
      :json => true
    }.merge(opts)
    body = connection.request(method, new_opts[:path], query,
                              new_opts[:excon], &block)
    body = Docker::Util.parse_json(body) if new_opts[:json]
    outer_block.nil? ? body : instance_exec(body, &outer_block)
  end
end

#set_create_request(&block) ⇒ Object

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



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

def set_create_request(&block)
  @create_request = block
end

#set_resource_prefix(val) ⇒ Object

Define the Model’s prefix for all requests.



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

def set_resource_prefix(val)
  @resource_prefix = val
end