Class: QB::Docker::Image::Name

Inherits:
QB::Data::Immutable show all
Extended by:
MethodDecorators
Includes:
NRSER::Log::Mixin
Defined in:
lib/qb/docker/image/name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameString (readonly)

For lack of a better name, the part of the name that's not anything else. It's also the only required part.

Returns:

  • (String)

    Can't be empty.



206
207
# File 'lib/qb/docker/image/name.rb', line 206

prop  :name,
type: t.non_empty_str

#portInteger? (readonly)

Registry server port, if any.

Returns:

  • (Integer?)

    In range [1, 2**16 - 1]



235
236
# File 'lib/qb/docker/image/name.rb', line 235

prop  :port,
type: t.port?

#registry_serverString? (readonly)

Registry server, if any.

Returns:

  • (String?)

    String is non-empty.



225
226
227
# File 'lib/qb/docker/image/name.rb', line 225

prop  :registry_server,
aliases: [ :reg ],
type: t.non_empty_str?

#repositoryString? (readonly)

The repository name, if any.

Returns:

  • (String?)

    String is non-empty.



215
216
217
# File 'lib/qb/docker/image/name.rb', line 215

prop  :repository,
aliases: [ :repo ],
type: t.non_empty_str?

#sourceString? (readonly)

Source string this name was loaded from, if any.

Returns:

  • (String?)


195
196
# File 'lib/qb/docker/image/name.rb', line 195

prop  :source,
type: t.non_empty_str?

Class Method Details

.allObject



172
173
174
# File 'lib/qb/docker/image/name.rb', line 172

def self.all
  QB::Docker::CLI.image_names load: true, only_named: true
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



167
168
169
# File 'lib/qb/docker/image/name.rb', line 167

def self.exists? name
  QB::Docker::CLI.image_named? name
end

.from(source) ⇒ self

Get an instance from a source.

Parameters:

  • source (self | String | Hash)

Returns:

  • (self)


154
155
156
157
158
159
# File 'lib/qb/docker/image/name.rb', line 154

def self.from source
  t.match source,
    self,     source,
    t.str,    method( :from_s ),
    t.hash_,  method( :from_data )
end

.from_s(string) ⇒ self

Load from a String.

Parameters:

  • string (String)

Returns:

  • (self)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/qb/docker/image/name.rb', line 58

def self.from_s string
  strings = {}
  segments = string.split '/'
  
  if segments[-1].include? ':'
    rest, _, tag = segments[-1].rpartition ':'
    strings[:tag] = tag
    segments[-1] = rest
  else
    rest = string
  end
  
  case segments.length
  when 0
    # Pass - construction will error
  when 1
    # Just a name
    strings[:name] = segments[0]
  else
    if segments[0].include? ':'
      # segments = [s_0, s_1, ... s_n]
      #   =>  repository = s_0
      #       segments = [s_1, s_2, ... s_n]
      # 
      # like
      # 
      # segments = ['docker.beiarea.com:8888', 'beiarea', 'wall']
      #   =>  registry_server = 'docker.beiarea.com'
      #       port            = '8888'
      #       segments        = ['beiarea', 'wall']
      # 
      registry_server, _, port = segments.shift.rpartition ':'
      strings[:registry_server] = registry_server
      strings[:port] = port
    end
    
    if segments.length > 1
      # segments = [s_0, s_1, ... s_m]
      #   =>  repository  = s_0
      #       segments    = [s_1, s_2, ... s_m]
      # 
      # like
      # 
      # segments = ['beiarea', 'wall']
      #   =>  repository  = 'beiarea'
      #       segments    = ['wall']
      # 
      repository = segments.shift
      strings[:repository] = repository
    end
    
    # I think Docker image names *can* have more than just a repo and name
    # segment, though it's poorly supported from what I recall... though
    # we will handle it by just re-joining whatever's left into the name.
    # 
    # segments = [s_0, s_1, ... s_p]
    #   =>  name = "s_0/s_1/.../s_p"
    # 
    # like
    # 
    # segments = ['wall']
    #   =>  name = 'wall'
    # 
    # or
    # 
    # segments = ['www_rails', 'web']
    #   =>  name = 'www_rails/web'
    # 
    strings[:name] = segments.join '/'
  end
  
  logger.debug "strings", strings
  
  # Now turn them into value using their prop types
  values = strings.transform_values_with_keys { |name, string|
    prop = [name]
    
    if prop.type.respond_to? :from_s
      prop.type.from_s string
    else
      string
    end
  }
  
  logger.debug "values", values
  
  # And construct!
  new source: string, **values
end

.list(**attrs) ⇒ Object

See Also:



179
180
181
182
183
184
# File 'lib/qb/docker/image/name.rb', line 179

def self.list **attrs
  return all if attrs.empty?
  
  type = t.attrs attrs
  all.select { |name| type === name }
end

Instance Method Details

#dirty?return_type

TODO:

Document dirty? method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



275
276
277
# File 'lib/qb/docker/image/name.rb', line 275

def dirty?
  !!tag.try( :dirty? )
end

#exists?Boolean Also known as: exist?

Does the name exist in the local daemon?

Returns:

  • (Boolean)

See Also:



260
261
262
# File 'lib/qb/docker/image/name.rb', line 260

def exists?
  QB::Docker::CLI.image_named? self
end

#formattedObject



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/qb/docker/image/name.rb', line 291

def formatted
  [
    host,
    repository,
    name,
  ].compact.join( '/' ).thru { |without_tag|
    if tag
      "#{ without_tag }:#{ tag }"
    else
      without_tag
    end
  }
end

#hostObject



280
281
282
283
284
285
286
287
288
# File 'lib/qb/docker/image/name.rb', line 280

def host
  return unless registry_server
  
  if port
    "#{ registry_server }:#{ port }"
  else
    registry_server
  end
end

#inspectObject



311
312
313
# File 'lib/qb/docker/image/name.rb', line 311

def inspect
  "#<#{ self.class.safe_name } #{ to_s }>"
end

#pretty_print(q) ⇒ Object



316
317
318
# File 'lib/qb/docker/image/name.rb', line 316

def pretty_print q
  q.text inspect
end

#to_sObject



306
307
308
# File 'lib/qb/docker/image/name.rb', line 306

def to_s
  formatted
end