Module: Kernel

Defined in:
lib/asgard/kernel_methods.rb

Class Method Summary collapse

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


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

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.



13
14
15
16
# File 'lib/asgard/kernel_methods.rb', line 13

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)


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

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



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

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



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

def loki_up(name = ".loki")
  dir = Pathname.new(Dir.pwd)
  loop do
    candidate = dir + name
    return candidate if candidate.exist?
    parent = dir.parent
    break if parent == dir
    dir = parent
  end
  nil
end

.verbose?Boolean

Returns:

  • (Boolean)


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

def verbose? = $VERBOSE