Class: Lono::Layering

Inherits:
AbstractBase show all
Extended by:
Memoist
Defined in:
lib/lono/layering.rb

Instance Method Summary collapse

Methods inherited from AbstractBase

#reinitialize, #template_path

Methods included from Blueprint::Root

#find_blueprint_root, #set_blueprint_root

Constructor Details

#initialize(config, options = {}, env = Lono.env, root = Lono.root) ⇒ Layering

Returns a new instance of Layering.



5
6
7
8
9
10
# File 'lib/lono/layering.rb', line 5

def initialize(config, options={}, env=Lono.env, root=Lono.root)
  super(options)
  # config can be params or variables
  @config, @options, @env, @root = config, options, env, root
  @requested = determine_requested
end

Instance Method Details

#alwaysObject



21
22
23
24
25
# File 'lib/lono/layering.rb', line 21

def always
  base = "#{@root}/configs/#{@blueprint}/#{@config}/base"
  env = "#{@root}/configs/#{@blueprint}/#{@config}/#{@env}"
  [base, env]
end

#determine_requestedObject



52
53
54
55
56
# File 'lib/lono/layering.rb', line 52

def determine_requested
  # param is usually set from the convention. when set from convention stack name takes higher precedence
  config_key = @config.singularize.to_sym # param or variable
  @options[config_key] || @options[:config] || @stack
end

#direct_layersObject



38
39
40
41
42
43
44
# File 'lib/lono/layering.rb', line 38

def direct_layers
  if @requested.starts_with?('/')
    [@requested] # IE: absolute full path
  else
    ["#{@root}/#{@requested}"] # IE : relative path within lono project]
  end
end

#locationsObject



12
13
14
15
16
17
18
19
# File 'lib/lono/layering.rb', line 12

def locations
  paths = always + requested
  layers = paths.map do |path|
    requested_file(path)
  end.compact
  print_layers(layers)
  layers
end


46
47
48
49
50
# File 'lib/lono/layering.rb', line 46

def print_layers(layers)
  return unless ENV["LONO_DEBUG_LAYERING"]
  puts "layers #{@config}:"
  pp layers
end

#requestedObject



27
28
29
# File 'lib/lono/layering.rb', line 27

def requested
  standard_layers + direct_layers
end

#requested_file(path) ⇒ Object



58
59
60
61
62
# File 'lib/lono/layering.rb', line 58

def requested_file(path)
  # List of paths to consider from initial path provided. Combine params and variables possible paths for simplicity.
  paths = [path, "#{path}.txt", "#{path}.sh", "#{path}.rb"].compact
  paths.find { |p| File.file?(p) }
end

#standard_layersObject



31
32
33
34
35
36
# File 'lib/lono/layering.rb', line 31

def standard_layers
  config_level = "#{@root}/configs/#{@blueprint}/#{@config}/#{@requested}"
  env_level = "#{@root}/configs/#{@blueprint}/#{@config}/#{@env}/#{@requested}"
  template_level = "#{@root}/configs/#{@blueprint}/#{@config}/#{@env}/#{@template}/#{@requested}"
  [config_level, env_level, template_level]
end