Class: Syntropy::ModuleContext

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ void

Initializes a module with the given environment hash.

Parameters:

  • env (Hash)

    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.

Parameters:



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

Parameters:

  • env (Hash)

    app environment

  • code (String)

    module source code

  • fn (String)

    module file name

  • extensions (Module, Array<Module>)

    extension module(s)

Returns:



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!

Returns:

  • (bool)


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.

Parameters:

  • ref (String)

    directory reference

Returns:

  • (Array)

    array of pages found in directory



250
251
252
# File 'lib/syntropy/module_loader.rb', line 250

def page_list(ref)
  Syntropy.page_list(@env, ref)
end