Class: Rum::Docker::Image

Inherits:
Object
  • Object
show all
Extended by:
AttrCallable
Includes:
Enumerable
Defined in:
lib/rumrunner/docker.rb

Overview

Docker image object.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttrCallable

attr_method_accessor

Constructor Details

#initialize(name:, registry: nil, username: nil, tag: nil, &block) ⇒ Image

Initialize image by reference component. Evaluates &block if given.


242
243
244
245
246
247
248
# File 'lib/rumrunner/docker.rb', line 242

def initialize(name:, registry:nil, username:nil, tag:nil, &block)
  @registry = registry
  @username = username
  @name     = name
  @tag      = tag
  instance_eval(&block) if block_given?
end

Class Method Details

.parse(string_or_symbol) ⇒ Object

Parse a string as a Docker image reference

Example:

Image.parse("image")
Image.parse("image:tag")
Image.parse("username/image")
Image.parse("username/image:tag")
Image.parse("registry:5000/username/image")
Image.parse("registry:5000/username/image:tag")

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rumrunner/docker.rb', line 281

def parse(string_or_symbol)
  string      = string_or_symbol.to_s
  slash_count = string.count("/")

  # image[:tag]
  if slash_count.zero?
    name, tag = string.split(/:/)
    new name: name, tag: tag

  # username/image[:tag]
  elsif slash_count == 1
    username, name_tag = string.split(/\//)
    name, tag          = name_tag.split(/:/)
    new name: name, username: username, tag: tag

  # registry/username/image[:tag]
  else
    registry, username, name_tag = string.split(/\//)
    name, tag                    = name_tag.split(/:/)
    new name: name, registry: registry, username: username, tag: tag
  end
end

Instance Method Details

#eachObject

Yield each non-nil component of the image reference in order.


252
253
254
# File 'lib/rumrunner/docker.rb', line 252

def each
  [@registry, @username, @name, @tag || :latest].compact.each{|x| yield x.to_s }
end

#familyObject

Get the image reference without the @tag component.


258
259
260
# File 'lib/rumrunner/docker.rb', line 258

def family
  File.join(*[@registry, @username, @name].compact.map(&:to_s))
end

#registryObject

Access components of the image reference by method.


237
# File 'lib/rumrunner/docker.rb', line 237

attr_method_accessor :registry, :username, :name, :tag

#to_sObject

Convert the image reference to string.


264
265
266
# File 'lib/rumrunner/docker.rb', line 264

def to_s
  "#{family}:#{@tag || :latest}"
end