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.

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



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.

Returns:

  • (Array)

    array of module references



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.

Parameters:

  • env (Hash)

    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.

Parameters:

  • v (any)

    export value



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.

Parameters:

  • ref (String)

    module reference

Returns:

  • (any)

    loaded dependency’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.

Parameters:

  • ref (String)

    directory reference

Returns:

  • (Array)

    array of pages found in directory



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.

Parameters:

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

    template proc or nil

  • block (Proc)

    template block

Returns:

  • (P2::Template)

    template



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