Class: Utopia::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/setup.rb

Overview

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

Constant Summary collapse

ENVIRONMENT_KEY =
'UTOPIA_ENV'.freeze
DEFAULT_ENVIRONMENT =
'environment'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_root, external_encoding: Encoding::UTF_8) ⇒ Setup

Returns a new instance of Setup.



26
27
28
29
30
31
32
# File 'lib/utopia/setup.rb', line 26

def initialize(config_root, external_encoding: Encoding::UTF_8)
	@config_root = config_root
	
	@external_encoding = external_encoding
	
	@environment = nil
end

Instance Attribute Details

#config_rootObject (readonly)

Returns the value of attribute config_root.



34
35
36
# File 'lib/utopia/setup.rb', line 34

def config_root
  @config_root
end

#environmentObject (readonly)

Returns the value of attribute environment.



36
37
38
# File 'lib/utopia/setup.rb', line 36

def environment
  @environment
end

#external_encodingObject (readonly)

Returns the value of attribute external_encoding.



35
36
37
# File 'lib/utopia/setup.rb', line 35

def external_encoding
  @external_encoding
end

Instance Method Details

#add_load_path(path) ⇒ Object

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



96
97
98
99
# File 'lib/utopia/setup.rb', line 96

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

#applyObject



49
50
51
52
53
54
55
56
57
# File 'lib/utopia/setup.rb', line 49

def apply
	set_external_encoding
	
	apply_environment
	
	add_load_path('lib')
	
	require_relative '../utopia'
end

#apply_environmentObject



59
60
61
62
63
64
65
# File 'lib/utopia/setup.rb', line 59

def apply_environment
	if name = explicit_environment_name
		load_environment(name)
	else
		load_environment(DEFAULT_ENVIRONMENT)
	end
end

#environment_path(name, root = @config_root) ⇒ Object



67
68
69
# File 'lib/utopia/setup.rb', line 67

def environment_path(name, root = @config_root)
	File.expand_path("#{name}.yaml", root)
end

#explicit_environment_nameObject



41
42
43
# File 'lib/utopia/setup.rb', line 41

def explicit_environment_name
	ENV[ENVIRONMENT_KEY]
end

#load_environment(*args) ⇒ Object

Load the named configuration file from the config_root directory.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/utopia/setup.rb', line 81

def load_environment(*args)
	path = environment_path(*args)
	
	if File.exist?(path)
		# Load the YAML environment file:
		@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
end

#set_external_encoding(encoding = Encoding::UTF_8) ⇒ Object

If you don't specify these, it's possible to have issues when encodings mismatch on the server.



72
73
74
75
76
77
78
# File 'lib/utopia/setup.rb', line 72

def set_external_encoding(encoding = Encoding::UTF_8)
	# TODO: Deprecate and remove this setup - it should be the responsibility of the server to set this correctly.
	if Encoding.default_external != encoding
		warn "Updating Encoding.default_external from #{Encoding.default_external} to #{encoding}" if $VERBOSE
		Encoding.default_external = encoding
	end
end

#site_rootObject



45
46
47
# File 'lib/utopia/setup.rb', line 45

def site_root
	File.dirname(@config_root)
end