UtopiaSourceUtopiaSetup

class Setup

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

Definitions

def initialize(root, **options)

Initialize application setup for the given root.

Signature

parameter root String

The root directory.

parameter options Hash

Reserved setup options.

Implementation

def initialize(root, **options)
	@root = root
end

def config_root

Resolve the application's configuration directory.

Signature

returns String

The absolute configuration path.

Implementation

def config_root
	File.expand_path("config", @root)
end

def site_root

Return the application root.

Signature

returns String

The application root path.

Implementation

def site_root
	@root
end

def production?

Check whether the application is running in production.

Signature

returns Boolean

Whether the active Utopia variant is production.

Implementation

def production?
	Variant.for(:utopia) == :production
end

def staging?

Check whether the application is running in staging.

Signature

returns Boolean

Whether the active Utopia variant is staging.

Implementation

def staging?
	Variant.for(:utopia) == :staging
end

def development?

Check whether the application is running in development.

Signature

returns Boolean

Whether the active Utopia variant is development.

Implementation

def development?
	Variant.for(:utopia) == :development
end

def testing?

Check whether the application is running in testing.

Signature

returns Boolean

Whether the active Utopia variant is testing.

Implementation

def testing?
	Variant.for(:utopia) == :testing
end

def secret_for(key)

Fetch the configured secret for the given key.

Signature

parameter key String | Symbol

The lookup key.

returns String

The configured secret, or a newly generated transient secret when none is configured.

Implementation

def secret_for(key)
	secret = ENV["UTOPIA_#{key.upcase}_SECRET"]
	
	if secret.nil? || secret.empty?
		secret = SecureRandom.hex(32)
		
		Console.warn(self) do
			"Generating transient #{key} secret: #{secret.inspect}"
		end
	end
	
	return secret
end

def apply!

Apply.

Signature

returns self

This object.

Implementation

def apply!
	add_load_path("lib")
	
	apply_environment
	
	require_relative "../utopia"
end

def add_load_path(path)

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

Implementation

def add_load_path(path)
	# Allow loading library code from lib directory:
	$LOAD_PATH << File.expand_path(path, site_root)
end

def load_environment(variant)

Load the named configuration file from the config_root directory.

Implementation

def load_environment(variant)
	path = environment_path(variant)
	
	if File.exist?(path)
		Console.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
		Console.debug(self){"Ignoring environment at path: #{path.inspect} (file not found)"}
		
		return false
	end
end