Module: Kernel
- Defined in:
- lib/asgard/kernel_methods.rb
Class Method Summary collapse
- .debug? ⇒ Boolean
-
.env(name, default = nil) ⇒ Object
Fetch an environment variable by symbol or string name.
-
.import(path, from: caller_locations(1, 1).first) ⇒ Object
fromis the call-site location used to resolve a relativepath. - .import_up(name = ".loki") ⇒ Object
- .loki_up(name = ".loki") ⇒ Object
- .verbose? ⇒ Boolean
Class Method Details
.debug? ⇒ 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, from: caller_locations(1, 1).first) ⇒ Object
from is the call-site location used to resolve a relative path. It
defaults to the immediate caller, but is exposed as a keyword so a
wrapper (e.g. Asgard::Doctor's import tracer, prepended via super)
can thread through the real caller instead of its own frame.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/asgard/kernel_methods.rb', line 36 def import(path, from: caller_locations(1, 1).first) 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(from.absolute_path) path = File.(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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/asgard/kernel_methods.rb', line 58 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
7 |
# File 'lib/asgard/kernel_methods.rb', line 7 def verbose? = $VERBOSE |