Class: Kuby::Docker::LayerStack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kuby/docker/layer_stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLayerStack

T::Sig::WithoutRuntime.sig { void }



20
21
22
23
# File 'lib/kuby/docker/layer_stack.rb', line 20

def initialize
  @stack = []
  @layers = {}
end

Instance Attribute Details

#layersObject (readonly)

T::Sig::WithoutRuntime.sig { returns(T::Hash[Symbol, Kuby::Docker::Layer]) }



17
18
19
# File 'lib/kuby/docker/layer_stack.rb', line 17

def layers
  @layers
end

#stackObject (readonly)

T::Sig::WithoutRuntime.sig { returns(T::Array) }



14
15
16
# File 'lib/kuby/docker/layer_stack.rb', line 14

def stack
  @stack
end

Instance Method Details

#delete(name) ⇒ Object

T::Sig::WithoutRuntime.sig { params(name: Symbol).void }



107
108
109
110
# File 'lib/kuby/docker/layer_stack.rb', line 107

def delete(name)
  stack.delete(name)
  layers.delete(name)
end

#each(&block) ⇒ Object

T::Sig::WithoutRuntime.sig

override.params(
  block: T.nilable(T.proc.params(layer: Kuby::Docker::Layer).void)
)
.void



31
32
33
34
# File 'lib/kuby/docker/layer_stack.rb', line 31

def each(&block)
  return to_enum(__method__) unless block_given?
  @stack.each { |name| yield layers[name] }
end

#includes?(name) ⇒ Boolean

T::Sig::WithoutRuntime.sig { params(name: Symbol).returns(T::Boolean) }

Returns:

  • (Boolean)


113
114
115
# File 'lib/kuby/docker/layer_stack.rb', line 113

def includes?(name)
  layers.include?(name)
end

#insert(name, layer = nil, options = {}, &block) ⇒ Object

T::Sig::WithoutRuntime.sig

params(
  name: Symbol,
  layer: T.nilable(T.any(Layer, T::Hash[Symbol, T.untyped])),
  options: T::Hash[Symbol, T.untyped],
  block: T.nilable(T.proc.params(df: Kuby::Docker::Dockerfile).void)
)
.void



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
# File 'lib/kuby/docker/layer_stack.rb', line 65

def insert(name, layer = nil, options = {}, &block)
  # this is truly gross but it's the only way I can think of to be able
  # to call insert these two ways:
  #
  # insert :foo, FooLayer.new, before: :bundler_phase
  # insert :foo, before: :bundler_phase do
  #   ...
  # end
  if layer.is_a?(Hash)
    insert(name, nil, options.merge(layer), &block)
    return
  end

  existing_name = options[:before] || options[:after]
  idx = stack.index(existing_name)

  unless idx
    raise ArgumentError, "Could not find existing layer '#{existing_name}'"
  end

  idx += 1 if options[:after]
  stack.insert(idx, name)

  if layer
    layers[name] = layer
  elsif block_given?
    layers[name] = InlineLayer.new(block)
  else
    raise "Must either pass a layer object or a block to `#{__method__}'"
  end
end

#replace(name, layer) ⇒ Object

T::Sig::WithoutRuntime.sig { params(name: Symbol, layer: Layer).void }



98
99
100
101
102
103
104
# File 'lib/kuby/docker/layer_stack.rb', line 98

def replace(name, layer)
  unless layers.include?(name)
    raise ArgumentError, "Could not find existing layer '#{name}'"
  end

  layers[name] = layer
end

#use(name, layer = nil, &block) ⇒ Object

T::Sig::WithoutRuntime.sig

params(
  name: Symbol,
  layer: T.nilable(Layer),
  block: T.nilable(T.proc.params(df: Kuby::Docker::Dockerfile).void)
)
.void



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kuby/docker/layer_stack.rb', line 44

def use(name, layer = nil, &block)
  stack << name

  if layer
    layers[name] = layer
  elsif block_given?
    layers[name] = InlineLayer.new(block)
  else
    raise "Must either pass a layer object or a block to `#{__method__}'"
  end
end