Class: Kaal::RuntimeContext

Inherits:
Object
  • Object
show all
Defined in:
lib/kaal/runtime/runtime_context.rb,
sig/kaal/runtime/runtime_context.rbs

Overview

Resolves environment and path information for plain-Ruby runtime loading.

Constant Summary collapse

DEFAULT_ENVIRONMENT_NAME =

Returns:

  • ("development")
'development'
ENVIRONMENT_KEYS =

Returns:

  • (::Array["KAAL_ENV" | "RAILS_ENV" | "APP_ENV" | "RACK_ENV"])
%w[KAAL_ENV RAILS_ENV APP_ENV RACK_ENV].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path:, environment_name:) ⇒ RuntimeContext

Returns a new instance of RuntimeContext.

Parameters:

  • root_path: (Kaal::rbs_any)
  • environment_name: (Kaal::rbs_any)


30
31
32
33
# File 'lib/kaal/runtime/runtime_context.rb', line 30

def initialize(root_path:, environment_name:)
  @root_path = Pathname.new(root_path)
  @environment_name = environment_name.to_s
end

Instance Attribute Details

#environment_name ⇒ Kaal::rbs_any (readonly)

Returns the value of attribute environment_name.

Returns:

  • (Kaal::rbs_any)


15
16
17
# File 'lib/kaal/runtime/runtime_context.rb', line 15

def environment_name
  @environment_name
end

#root_path ⇒ Kaal::rbs_any (readonly)

Returns the value of attribute root_path.

Returns:

  • (Kaal::rbs_any)


15
16
17
# File 'lib/kaal/runtime/runtime_context.rb', line 15

def root_path
  @root_path
end

Class Method Details

.default(env: ENV, root_path: Dir.pwd) ⇒ Kaal::rbs_any

Parameters:

  • env: (Kaal::rbs_any) (defaults to: ENV)
  • root_path: (Kaal::rbs_any) (defaults to: Dir.pwd)

Returns:

  • (Kaal::rbs_any)


17
18
19
# File 'lib/kaal/runtime/runtime_context.rb', line 17

def self.default(env: ENV, root_path: Dir.pwd)
  new(root_path: root_path, environment_name: environment_name_from(env))
end

.environment_name_from(env) ⇒ Kaal::rbs_any

Parameters:

  • env (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


21
22
23
24
25
26
27
28
# File 'lib/kaal/runtime/runtime_context.rb', line 21

def self.environment_name_from(env)
  ENVIRONMENT_KEYS.each do |key|
    value = env[key].to_s.strip
    return value unless value.empty?
  end

  DEFAULT_ENVIRONMENT_NAME
end

Instance Method Details

#resolve_path(path) ⇒ Kaal::rbs_any

Parameters:

  • path (Kaal::rbs_any)

Returns:

  • (Kaal::rbs_any)


35
36
37
38
39
# File 'lib/kaal/runtime/runtime_context.rb', line 35

def resolve_path(path)
  return path.to_s if Pathname.new(path).absolute?

  root_path.join(path).to_s
end