Class: Utopia::Setup
- Inherits:
-
Object
- Object
- Utopia::Setup
- Defined in:
- lib/utopia/setup.rb
Overview
Used for setting up a Utopia web application, typically via config/environment.rb
Constant Summary
- ENVIRONMENT_KEY =
'UTOPIA_ENV'.freeze
- DEFAULT_ENVIRONMENT =
'environment'.freeze
Instance Attribute Summary collapse
-
#config_root ⇒ Object
readonly
Returns the value of attribute config_root.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#external_encoding ⇒ Object
readonly
Returns the value of attribute external_encoding.
Instance Method Summary collapse
-
#add_load_path(path) ⇒ Object
Add the given path to $LOAD_PATH.
-
#apply ⇒ Object
-
#apply_environment ⇒ Object
-
#environment_path(name, root = @config_root) ⇒ Object
-
#explicit_environment_name ⇒ Object
-
#initialize(config_root, external_encoding: Encoding::UTF_8) ⇒ Setup
constructor
A new instance of Setup.
-
#load_environment(*args) ⇒ Object
Load the named configuration file from the
config_root
directory. -
#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.
-
#site_root ⇒ Object
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_root ⇒ Object (readonly)
Returns the value of attribute config_root
34 35 36 |
# File 'lib/utopia/setup.rb', line 34 def config_root @config_root end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment
36 37 38 |
# File 'lib/utopia/setup.rb', line 36 def environment @environment end |
#external_encoding ⇒ Object (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.(path, site_root) end |
#apply ⇒ Object
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_environment ⇒ Object
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.("#{name}.yaml", root) end |
#explicit_environment_name ⇒ Object
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_root ⇒ Object
45 46 47 |
# File 'lib/utopia/setup.rb', line 45 def site_root File.dirname(@config_root) end |