Class: Environmenter::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/environmenter/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Loader

Returns a new instance of Loader.



5
6
7
# File 'lib/environmenter/loader.rb', line 5

def initialize(config = nil)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/environmenter/loader.rb', line 3

def config
  @config
end

Instance Method Details

#load!Object



9
10
11
12
13
# File 'lib/environmenter/loader.rb', line 9

def load!
  set_environment
  load_rails
  require_from_config
end

#load_railsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/environmenter/loader.rb', line 20

def load_rails
  begin
    require 'rails'
    if ::Rails.application.respond_to?(:eager_load)
      require File.expand_path('config/environment.rb')
      ::Rails.application.eager_load!
    else
      require File.expand_path('config/application.rb')
      ::Rails::Application.initializer "lapine.load_rails" do
        ::Rails.application.config.eager_load = true
      end
      require File.expand_path('config/environment.rb')
    end
  rescue LoadError
  end
end

#require_from_configObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/environmenter/loader.rb', line 37

def require_from_config
  return unless config

  if config.respond_to?(:require) && config.require
    return config.require.each do |file|
      require File.expand_path(file)
    end
  end

  if config.respond_to?(:[])
    if config['require']
      return config['require'].each do |file|
        require File.expand_path(file)
      end
    end

    if config[:require]
      return config[:require].each do |file|
        require File.expand_path(file)
      end
    end
  end
end

#set_environmentObject



15
16
17
18
# File 'lib/environmenter/loader.rb', line 15

def set_environment
  ENV['RAILS_ENV'] ||= 'development'
  ENV['RACK_ENV'] ||= ENV['RAILS_ENV']
end