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 collapse
- 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.
28 29 30 31 32 33 34 |
# File 'lib/utopia/setup.rb', line 28 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.
36 37 38 |
# File 'lib/utopia/setup.rb', line 36 def config_root @config_root end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
38 39 40 |
# File 'lib/utopia/setup.rb', line 38 def environment @environment end |
#external_encoding ⇒ Object (readonly)
Returns the value of attribute external_encoding.
37 38 39 |
# File 'lib/utopia/setup.rb', line 37 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
.
98 99 100 101 |
# File 'lib/utopia/setup.rb', line 98 def add_load_path(path) # Allow loading library code from lib directory: $LOAD_PATH << File.(path, site_root) end |
#apply ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/utopia/setup.rb', line 51 def apply set_external_encoding apply_environment add_load_path('lib') require_relative '../utopia' end |
#apply_environment ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/utopia/setup.rb', line 61 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
69 70 71 |
# File 'lib/utopia/setup.rb', line 69 def environment_path(name, root = @config_root) File.("#{name}.yaml", root) end |
#explicit_environment_name ⇒ Object
43 44 45 |
# File 'lib/utopia/setup.rb', line 43 def explicit_environment_name ENV[ENVIRONMENT_KEY] end |
#load_environment(*args) ⇒ Object
Load the named configuration file from the config_root
directory.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/utopia/setup.rb', line 83 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.
74 75 76 77 78 79 80 |
# File 'lib/utopia/setup.rb', line 74 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
47 48 49 |
# File 'lib/utopia/setup.rb', line 47 def site_root File.dirname(@config_root) end |