Module: Kernel

Defined in:
lib/asgard/kernel_methods.rb

Class Method Summary collapse

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


4
# File 'lib/asgard/kernel_methods.rb', line 4

def debug?   = $DEBUG

.env(name, default = nil) ⇒ Object

Fetch an environment variable by symbol or string name. The name is converted to an uppercase string automatically. Raises KeyError when the variable is missing and no default is given.



11
12
13
14
# File 'lib/asgard/kernel_methods.rb', line 11

def env(name, default = nil)
  key = name.to_s.upcase
  default.nil? ? ENV.fetch(key) : ENV.fetch(key, default)
end

.import(path) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/asgard/kernel_methods.rb', line 30

def import(path)
  path = path.to_s
  raise ArgumentError, "import: path must end with .loki (got #{path.inspect})" unless path.end_with?(".loki")
  unless File.absolute_path?(path)
    caller_dir = File.dirname(caller_locations(1, 1).first.absolute_path)
    path = File.expand_path(path, caller_dir)
  end
  paths = path =~ /[*?\[{]/ ? Dir.glob(path) : [path]
  loaded = paths.map do |p|
    if $LOADED_FEATURES.include?(p)
      warn "import: skip #{p} (already loaded)" if debug?
      next false
    end
    warn "import: #{p}" if verbose? || debug?
    load p
    $LOADED_FEATURES << p
    true
  end
  loaded.any?
end

.import_up(name = ".loki") ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/asgard/kernel_methods.rb', line 52

def import_up(name = ".loki")
  if name =~ /[*?\[{]/
    dir = Dir.pwd
    loop do
      matches = Dir.glob(File.join(dir, name))
      unless matches.empty?
        warn "import_up: #{name}#{dir}" if verbose? || debug?
        return matches.map { |p| import(p) }.any?
      end
      parent = File.dirname(dir)
      break if parent == dir
      dir = parent
    end
    warn "import_up: #{name} not found" if debug?
    return false
  end
  path = loki_up(name)
  unless path
    warn "import_up: #{name} not found" if debug?
    return false
  end
  warn "import_up: #{name}#{path}" if verbose? || debug?
  import path
end

.loki_up(name = ".loki") ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/asgard/kernel_methods.rb', line 17

def loki_up(name = ".loki")
  dir = Dir.pwd
  loop do
    candidate = File.join(dir, name)
    return candidate if File.exist?(candidate)
    parent = File.dirname(dir)
    break if parent == dir
    dir = parent
  end
  nil
end

.verbose?Boolean

Returns:

  • (Boolean)


5
# File 'lib/asgard/kernel_methods.rb', line 5

def verbose? = $VERBOSE