Class: Syntropy::ModuleContext
- Inherits:
-
Object
- Object
- Syntropy::ModuleContext
- Defined in:
- lib/syntropy/module_loader.rb
Overview
The Syntropy::ModuleContext class provides a context for loading a module. A module is a ‘.rb` source file that implements a route endpoint, a template, utility methods or any other functionality needed by the web app.
The following instance variables are available to modules:
-
‘@env`: the app environment hash
-
‘@machine`: a reference to the UringMachine instance
-
‘@module_loader`: a reference to the module loader
-
‘@app`: a reference to the app
-
‘@ref`: the module’s logical path (path relative to the app root)
-
‘@logger`: a reference to the app’s logger
In addition, the module code also has access to the ‘MODULE` constant which is set to `self`, and may be used to refer to various methods defined in the module.
Instance Attribute Summary collapse
-
#__dependencies__ ⇒ Object
readonly
Returns the value of attribute __dependencies__.
-
#__export_value__ ⇒ Object
readonly
Returns the value of attribute export_value.
Class Method Summary collapse
-
.apply_extensions(mod, extensions) ⇒ Object
Applies the given extension(s) to the given module context.
-
.load(env, code, fn, extensions) ⇒ Syntropy::ModuleContext
Loads a module, returning the module instance.
Instance Method Summary collapse
-
#collection_module? ⇒ bool
Returns true if the module is a collection module.
-
#initialize(env) ⇒ void
constructor
Initializes a module with the given environment hash.
-
#page_list(ref) ⇒ Array
Returns a list of pages found at the given ref.
Constructor Details
#initialize(env) ⇒ void
Initializes a module with the given environment hash.
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/syntropy/module_loader.rb', line 233 def initialize(env) @env = env @machine = env[:machine] @module_loader = env[:module_loader] @app = env[:app] @ref = env[:ref] @logger = env[:logger] @__dependencies__ = [] singleton_class.const_set(:MODULE, self) end |
Instance Attribute Details
#__dependencies__ ⇒ Object (readonly)
Returns the value of attribute __dependencies__.
244 245 246 |
# File 'lib/syntropy/module_loader.rb', line 244 def __dependencies__ @__dependencies__ end |
#__export_value__ ⇒ Object (readonly)
Returns the value of attribute export_value.
244 245 246 |
# File 'lib/syntropy/module_loader.rb', line 244 def __export_value__ @__export_value__ end |
Class Method Details
.apply_extensions(mod, extensions) ⇒ Object
Applies the given extension(s) to the given module context.
217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/syntropy/module_loader.rb', line 217 def self.apply_extensions(mod, extensions) case extensions when Array extensions.each { mod.extend(it) } when Module mod.extend(extensions) when nil # return else raise Syntropy::Error, "Invalid module extensions: #{extensions.inspect}" end end |
.load(env, code, fn, extensions) ⇒ Syntropy::ModuleContext
Loads a module, returning the module instance
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/syntropy/module_loader.rb', line 202 def self.load(env, code, fn, extensions) mod = new(env) apply_extensions(mod, extensions) mod.instance_eval(code, fn) env[:logger]&.info(message: "Loaded module at #{fn}") mod rescue StandardError, SyntaxError => e env[:logger]&.error(message: "Error while loading module at #{fn}", error: e) e.is_a?(SyntaxError) ? handle_syntax_error(env, e) : (raise e) end |
Instance Method Details
#collection_module? ⇒ bool
Returns true if the module is a collection module. See also #collection_module!
258 259 260 |
# File 'lib/syntropy/module_loader.rb', line 258 def collection_module? @collection_module_p end |
#page_list(ref) ⇒ Array
Returns a list of pages found at the given ref.
250 251 252 |
# File 'lib/syntropy/module_loader.rb', line 250 def page_list(ref) Syntropy.page_list(@env, ref) end |