Module: LoadRails

Defined in:
lib/load_rails.rb,
lib/load_rails/version.rb,
lib/load_rails/exceptions/rails_not_found.rb

Defined Under Namespace

Classes: RailsNotFound

Constant Summary collapse

RAILS_ENVIRONMENT_PATH =
::File.join("config", "environment.rb").freeze
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.environment?Boolean

Determines if the current directory is the root of a Rails project.

Returns:

  • (Boolean)


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

def self.environment?
  File.exist?(RAILS_ENVIRONMENT_PATH)
end

.loadnil

Finds the closes Rails application and requires it.

Returns:

  • (nil)


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

def self.load
  loop do
    if environment?
      require_rails!
      break
    else
      parent!
    end
  end
end

.parent!nil

Attempts to change directories to the parent of the current directory

Returns:

  • (nil)

Raises:

  • (RailsNotFound)

    if root directory reached and a Rails environment file was not found



35
36
37
38
39
40
# File 'lib/load_rails.rb', line 35

def self.parent!
  current_directory = Dir.pwd
  FileUtils.cd("..")

  raise RailsNotFound if current_directory == Dir.pwd
end

.require_rails!nil

Uses Ruby require to require the Rails environment for the current directory.

Returns:

  • (nil)


45
46
47
48
# File 'lib/load_rails.rb', line 45

def self.require_rails!
  require(::File.expand_path(RAILS_ENVIRONMENT_PATH, Dir.pwd))
  Rails.application.eager_load!
end