Class: Ocran::RuntimeEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/ocran/runtime_environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntimeEnvironment

Returns a new instance of RuntimeEnvironment.



15
16
17
18
19
20
# File 'lib/ocran/runtime_environment.rb', line 15

def initialize
  @env = ENV.to_hash.freeze
  @load_path = $LOAD_PATH.dup.freeze
  @loaded_features = $LOADED_FEATURES.dup.freeze
  @pwd = Dir.pwd.freeze
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



13
14
15
# File 'lib/ocran/runtime_environment.rb', line 13

def env
  @env
end

#load_pathObject (readonly)

Returns the value of attribute load_path.



13
14
15
# File 'lib/ocran/runtime_environment.rb', line 13

def load_path
  @load_path
end

#loaded_featuresObject (readonly)

Returns the value of attribute loaded_features.



13
14
15
# File 'lib/ocran/runtime_environment.rb', line 13

def loaded_features
  @loaded_features
end

#pwdObject (readonly)

Returns the value of attribute pwd.



13
14
15
# File 'lib/ocran/runtime_environment.rb', line 13

def pwd
  @pwd
end

Instance Method Details

#expand_path(path) ⇒ Object

Expands the given path using the working directory stored in this instance as the base. This method resolves relative paths to absolute paths, ensuring they are fully qualified based on the working directory stored within this instance.



26
27
28
# File 'lib/ocran/runtime_environment.rb', line 26

def expand_path(path)
  File.expand_path(path, @pwd)
end

#find_load_path(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ocran/runtime_environment.rb', line 30

def find_load_path(path)
  path = Pathname.new(path) unless path.is_a?(Pathname)

  if path.absolute?
    # For an absolute path feature, find the load path that contains the feature
    # and determine the longest matching path (most specific path).
    @load_path.select { |load_path| path.subpath?(expand_path(load_path)) }
              .max_by { |load_path| expand_path(load_path).length }
  else
    # For a relative path feature, find the load path where the expanded feature exists
    # and select the longest load path (most specific path).
    @load_path.select { |load_path| path.expand_path(load_path).exist? }
              .max_by { |load_path| load_path.length }
  end
end