Class: Ufo::Layering::Layer

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Ufo::Layering, Utils::Logging, Utils::Pretty
Defined in:
lib/ufo/layering/layer.rb

Constant Summary collapse

@@shown_layers =
false

Instance Method Summary collapse

Methods included from Utils::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Utils::Logging

#logger

Methods included from Ufo::Layering

#layers, #post_layers, #pre_layers

Constructor Details

#initialize(task_definition) ⇒ Layer

Returns a new instance of Layer.



21
22
23
# File 'lib/ufo/layering/layer.rb', line 21

def initialize(task_definition)
  @task_definition = task_definition
end

Instance Method Details

#add_ext!(paths) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/ufo/layering/layer.rb', line 64

def add_ext!(paths)
  ext = "rb"
  paths.map! do |path|
    path = path.sub(/\/$/,'') if path.ends_with?('/')
    "#{path}.rb"
  end
  paths
end

#full_layeringObject



35
36
37
38
39
40
41
42
# File 'lib/ufo/layering/layer.rb', line 35

def full_layering
  # layers defined in Lono::Layering module
  all = layers.map { |layer| layer.sub(/\/$/,'') } # strip trailing slash
  all.inject([]) do |sum, layer|
    sum += layer_levels(layer) unless layer.nil?
    sum
  end
end

#full_layers(dir) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/ufo/layering/layer.rb', line 73

def full_layers(dir)
  layers = full_layering.map do |layer|
    "#{dir}/#{layer}"
  end
  role_layers = full_layering.map do |layer|
    "#{dir}/#{@task_definition.role}/#{layer}" # Note: layer can be '' will clean up
  end
  layers += role_layers
  layers.map { |l| l.gsub('//','/') } # cleanup // if layer is ''
end

#layer_levels(prefix = nil) ⇒ Object

adds prefix and to each layer pair that has base and Ufo.env. IE:

"#{prefix}/base"
"#{prefix}/#{Ufo.env}"


54
55
56
57
58
59
60
61
62
# File 'lib/ufo/layering/layer.rb', line 54

def layer_levels(prefix=nil)
  levels = ["base", Ufo.env]
  levels.map! do |i|
    # base layer has prefix of '', reject with blank so it doesnt produce '//'
    [prefix, i].reject(&:blank?).join('/')
  end
  levels.unshift(prefix) # unless prefix.blank? # IE: params/us-west-2.txt
  levels
end

#main_layersObject

interface method



45
46
47
# File 'lib/ufo/layering/layer.rb', line 45

def main_layers
  ['']
end

#pathsObject



25
26
27
28
29
30
31
32
33
# File 'lib/ufo/layering/layer.rb', line 25

def paths
  core = full_layers(".ufo/vars")
  app = full_layers(".ufo/vars/#{Ufo.app}")
  paths = core + app
  add_ext!(paths)
  paths.map! { |p| "#{Ufo.root}/#{p}" }
  show_layers(paths)
  paths
end

#show_layers(paths) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/ufo/layering/layer.rb', line 85

def show_layers(paths)
  return if @@shown_layers
  logger.info "Vars Layers:" if ENV['UFO_SHOW_ALL_LAYERS']
  paths.each do |path|
    show_layer = File.exist?(path) && logger.level <= Logger::DEBUG
    logger.info "    #{pretty_path(path)}" if show_layer || ENV['UFO_SHOW_ALL_LAYERS']
  end
  @@shown_layers = true
end