Class: Lono::ConfigLocation

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

Constant Summary collapse

@@using_message_displayed =
{}

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) ⇒ ConfigLocation

Returns a new instance of ConfigLocation.



5
6
7
8
9
10
# File 'lib/lono/config_location.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

#determine_requestedObject

Some switching logic between variable and param below



68
69
70
71
72
# File 'lib/lono/config_location.rb', line 68

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_levelsObject



34
35
36
37
38
39
# File 'lib/lono/config_location.rb', line 34

def direct_levels
  [
    @requested, # IE: absolute full path
    "#{@root}/#{@requested}", # IE : relative path within lono project
  ]
end

#lookupObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lono/config_location.rb', line 12

def lookup
  levels = []
  levels += direct_levels
  # Standard lookup paths
  template_level = "#{@root}/configs/#{@blueprint}/#{@config}/#{@env}/#{@template}/#{@requested}"
  env_level = "#{@root}/configs/#{@blueprint}/#{@config}/#{@env}/#{@requested}"
  config_level = "#{@root}/configs/#{@blueprint}/#{@config}/#{@requested}"
  generic_env = "#{@root}/configs/#{@blueprint}/#{@config}/#{@env}"
  levels += [template_level, env_level, config_level, generic_env]

  print_levels(levels)

  found = levels.find do |level|
    requested_file(level)
  end
  if found
    file = requested_file(found)
    using_message(file, @env)
    file
  end
end

#lookup_baseObject



41
42
43
44
45
46
47
48
# File 'lib/lono/config_location.rb', line 41

def lookup_base
  base = "#{@root}/configs/#{@blueprint}/#{@config}/base"
  file = requested_file(base)
  if file
    using_message(file, "base")
    file
  end
end


50
51
52
53
54
# File 'lib/lono/config_location.rb', line 50

def print_levels(levels)
  return unless ENV["LONO_DEBUG_CONFIG"]
  puts "levels #{@config}:"
  pp levels
end

#requested_file(path) ⇒ Object



74
75
76
77
78
# File 'lib/lono/config_location.rb', line 74

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

#using_message(file, type) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/lono/config_location.rb', line 57

def using_message(file, type)
  return if @@using_message_displayed[file]

  pretty_file = file.sub("#{Lono.root}/", "")
  puts "Using #{@config} for #{type}: #{pretty_file}"

  @@using_message_displayed[file] = true
end