class Utopia::Setup

Used for setting up a Utopia web application, typically via `config/environment.rb`

Attributes

root[R]

Public Class Methods

new(root, **options) click to toggle source
# File lib/utopia/setup.rb, line 33
def initialize(root, **options)
        @root = root
end

Public Instance Methods

apply!() click to toggle source
# File lib/utopia/setup.rb, line 77
def apply!
        add_load_path('lib')
        
        apply_environment
        
        require_relative '../utopia'
end
config_root() click to toggle source
# File lib/utopia/setup.rb, line 39
def config_root
        File.expand_path("config", @root)
end
development?() click to toggle source
# File lib/utopia/setup.rb, line 55
def development?
        Variant.for(:utopia) == :development
end
production?() click to toggle source
# File lib/utopia/setup.rb, line 47
def production?
        Variant.for(:utopia) == :production
end
secret_for(key) click to toggle source
# File lib/utopia/setup.rb, line 63
def secret_for(key)
        secret = ENV["UTOPIA_#{key.upcase}_SECRET"]
        
        if secret.nil? || secret.empty?
                secret = SecureRandom.hex(32)
                
                Utopia.logger.warn(self) do
                        "Generating transient #{key} secret: #{secret.inspect}"
                end
        end
        
        return secret
end
site_root() click to toggle source
# File lib/utopia/setup.rb, line 43
def site_root
        @root
end
staging?() click to toggle source
# File lib/utopia/setup.rb, line 51
def staging?
        Variant.for(:utopia) == :staging
end
testing?() click to toggle source
# File lib/utopia/setup.rb, line 59
def testing?
        Variant.for(:utopia) == :testing
end

Private Instance Methods

add_load_path(path) click to toggle source

Add the given path to $LOAD_PATH. If it's relative, make it absolute relative to `site_path`.

# File lib/utopia/setup.rb, line 99
def add_load_path(path)
        # Allow loading library code from lib directory:
        $LOAD_PATH << File.expand_path(path, site_root)
end
apply_environment() click to toggle source
# File lib/utopia/setup.rb, line 87
def apply_environment
        # First try to load `config/environment.yaml` if it exists:
        load_environment(:environment)
        
        # NOTE: loading the environment above MAY set `VARIANT`. So, it's important to look up the variant AFTER loading that environment.
        
        # Then, try to load the variant specific environment:
        variant = Variant.for(:utopia)
        load_environment(variant)
end
environment_path(variant) click to toggle source
# File lib/utopia/setup.rb, line 104
def environment_path(variant)
        File.expand_path("config/#{variant}.yaml", @root)
end
load_environment(variant) click to toggle source

Load the named configuration file from the `config_root` directory.

# File lib/utopia/setup.rb, line 109
def load_environment(variant)
        path = environment_path(variant)
        
        if File.exist?(path)
                Utopia.logger.debug(self) {"Loading environment at path: #{path.inspect}"}
                
                # Load the YAML environment file:
                if environment = YAML.load_file(path)
                        # We update ENV but only when it's not already set to something:
                        ENV.update(environment) do |name, old_value, new_value|
                                old_value || new_value
                        end
                end
                
                return true
        else
                Utopia.logger.debug(self) {"Ignoring environment at path: #{path.inspect} (file not found)"}
                
                return false
        end
end