Class: Syntropy::Module
- Inherits:
-
Object
- Object
- Syntropy::Module
- Defined in:
- lib/syntropy/module.rb
Overview
The Syntropy::Module class implements a reloadable 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.
Instance Attribute Summary collapse
-
#__export_value__ ⇒ Object
readonly
Returns the value of attribute export_value.
Class Method Summary collapse
-
.load(env, code, fn) ⇒ Object
Loads a module, returning the module instance.
Instance Method Summary collapse
-
#__dependencies__ ⇒ Array
Returns the list of module references imported by the module.
-
#app(**env) ⇒ Object
Creates and returns a Syntropy app for the given environment.
-
#export(v) ⇒ void
Exports the given value.
-
#import(ref) ⇒ any
Imports the module corresponding to the given reference.
-
#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.
-
#template(proc = nil, &block) ⇒ P2::Template
Creates and returns a P2 template created with the given block.
Constructor Details
#initialize(**env) ⇒ void
Initializes a module with the given environment hash.
154 155 156 157 158 159 160 161 |
# File 'lib/syntropy/module.rb', line 154 def initialize(**env) @env = env @machine = env[:machine] @module_loader = env[:module_loader] @app = env[:app] @ref = env[:ref] singleton_class.const_set(:MODULE, self) end |
Instance Attribute Details
#__export_value__ ⇒ Object (readonly)
Returns the value of attribute export_value.
163 164 165 |
# File 'lib/syntropy/module.rb', line 163 def __export_value__ @__export_value__ end |
Class Method Details
.load(env, code, fn) ⇒ Object
Loads a module, returning the module instance
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/syntropy/module.rb', line 137 def self.load(env, code, fn) m = new(**env) m.instance_eval(code, fn) env[:logger]&.info(message: "Loaded module at #{fn}") m rescue StandardError => e env[:logger]&.error( message: "Error while loading module #{fn}", error: e ) raise end |
Instance Method Details
#__dependencies__ ⇒ Array
Returns the list of module references imported by the module.
178 179 180 |
# File 'lib/syntropy/module.rb', line 178 def __dependencies__ @__dependencies__ ||= [] end |
#app(**env) ⇒ Object
Creates and returns a Syntropy app for the given environment.
216 217 218 |
# File 'lib/syntropy/module.rb', line 216 def app(**env) Syntropy::App.new(**(@env.merge(env))) end |
#export(v) ⇒ void
This method returns an undefined value.
Exports the given value. This value will be used as the module’s entrypoint. It can be any Ruby value, but for a route module would normally be a proc.
171 172 173 |
# File 'lib/syntropy/module.rb', line 171 def export(v) @__export_value__ = v end |
#import(ref) ⇒ any
Imports the module corresponding to the given reference. The return value is the module’s export value.
187 188 189 190 191 |
# File 'lib/syntropy/module.rb', line 187 def import(ref) @module_loader.load(ref).tap { __dependencies__ << ref } end |
#page_list(ref) ⇒ Array
Returns a list of pages found at the given ref.
209 210 211 |
# File 'lib/syntropy/module.rb', line 209 def page_list(ref) Syntropy.page_list(@env, ref) end |
#template(proc = nil, &block) ⇒ P2::Template
Creates and returns a P2 template created with the given block.
198 199 200 201 202 203 |
# File 'lib/syntropy/module.rb', line 198 def template(proc = nil, &block) proc ||= block raise "No template block/proc given" if !proc P2::Template.new(proc) end |