Class: Lono::ConfigLocation

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Conventions
Defined in:
lib/lono/config_location.rb

Constant Summary collapse

@@using_message_displayed =
{}

Instance Method Summary collapse

Methods included from Conventions

#template_param_convention

Constructor Details

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

Returns a new instance of ConfigLocation.



6
7
8
9
10
11
12
13
14
15
# File 'lib/lono/config_location.rb', line 6

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

  @stack = options[:stack]
  @blueprint = options[:blueprint] || @stack
  @template, @param = template_param_convention(options)

  @requested = determine_requested
end

Instance Method Details

#determine_requestedObject

Some switching logic between variable and param below



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

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] || @options[:stack]
end

#direct_levelsObject



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

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

#lookupObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lono/config_location.rb', line 17

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



46
47
48
49
50
51
52
53
# File 'lib/lono/config_location.rb', line 46

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


55
56
57
58
59
# File 'lib/lono/config_location.rb', line 55

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

#requested_file(path) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/lono/config_location.rb', line 79

def requested_file(path)
  # List of paths to consider from initial path provided
  paths = @config == "params" ?
            [path, "#{path}.txt", "#{path}.sh"] :
            [path, "#{path}.rb"]
  paths.find { |p| File.file?(p) }
end

#using_message(file, type) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/lono/config_location.rb', line 62

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