Class: Syntropy::Module

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

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



167
168
169
170
171
172
173
174
175
# File 'lib/syntropy/module.rb', line 167

def initialize(**env)
  @env = env
  @machine = env[:machine]
  @module_loader = env[:module_loader]
  @app = env[:app]
  @ref = env[:ref]
  @logger = env[:logger]
  singleton_class.const_set(:MODULE, self)
end

Instance Attribute Details

#__export_value__Object (readonly)

Returns the value of attribute export_value.



177
178
179
# File 'lib/syntropy/module.rb', line 177

def __export_value__
  @__export_value__
end

Class Method Details

.load(env, code, fn) ⇒ Object

Loads a module, returning the module instance



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/syntropy/module.rb', line 150

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.

Returns:

  • (Array)

    array of module references



192
193
194
# File 'lib/syntropy/module.rb', line 192

def __dependencies__
  @__dependencies__ ||= []
end

#app(**env) ⇒ Object

Creates and returns a Syntropy app for the given environment.

Parameters:

  • env (Hash)

    environment



246
247
248
# File 'lib/syntropy/module.rb', line 246

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.

Parameters:

  • v (any)

    export value



185
186
187
# File 'lib/syntropy/module.rb', line 185

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.

Parameters:

  • ref (String)

    module reference

Returns:

  • (any)

    loaded dependency’s export value



201
202
203
204
205
# File 'lib/syntropy/module.rb', line 201

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.

Parameters:

  • ref (String)

    directory reference

Returns:

  • (Array)

    array of pages found in directory



239
240
241
# File 'lib/syntropy/module.rb', line 239

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

#template(proc = nil, &block) ⇒ Papercraft::Template

Creates and returns a Papercraft template created with the given block.

Parameters:

  • proc (Proc, nil) (defaults to: nil)

    template proc or nil

  • block (Proc)

    template block

Returns:

  • (Papercraft::Template)

    template



212
213
214
215
216
217
# File 'lib/syntropy/module.rb', line 212

def template(proc = nil, &block)
  proc ||= block
  raise "No template block/proc given" if !proc

  Papercraft::Template.new(proc)
end

#template_xml(proc = nil, &block) ⇒ Papercraft::Template

Creates and returns a Papercraft XML template created with the given block.

Parameters:

  • proc (Proc, nil) (defaults to: nil)

    template proc or nil

  • block (Proc)

    template block

Returns:

  • (Papercraft::Template)

    template



224
225
226
227
228
229
230
231
232
233
# File 'lib/syntropy/module.rb', line 224

def template_xml(proc = nil, &block)
  proc ||= block
  raise "No template block/proc given" if !proc

  Papercraft::Template.new(proc, mode: :xml)
rescue => e
  p e
  p e.backtrace
  raise
end