Class: Lennarb

Inherits:
Object
  • Object
show all
Defined in:
lib/lennarb/request.rb,
lib/lennarb.rb,
lib/lennarb/plugin.rb,
lib/lennarb/version.rb,
lib/lennarb/response.rb,
lib/lennarb/route_node.rb,
lib/lennarb/plugins/hooks.rb,
lib/lennarb/plugins/mount.rb

Overview

Released under the MIT License. Copyright, 2023-2024, by Aristóteles Coutinho.

Defined Under Namespace

Modules: Plugin, Plugins Classes: LennarbError, Request, Response, RouteNode

Constant Summary collapse

VERSION =
'1.3.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Lennarb

Returns a new instance of Lennarb.

Yields:

  • (_self)

Yield Parameters:

  • _self (Lennarb)

    the object that the method was called on



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lennarb.rb', line 71

def initialize
  @_mutex = Mutex.new
  @_root             = self.class.instance_variable_get(:@_root)&.dup           || RouteNode.new
  @_plugins          = self.class.instance_variable_get(:@_plugins)&.dup        || []
  @_loaded_plugins   = self.class.instance_variable_get(:@_loaded_plugins)&.dup || {}
  @_middlewares      = self.class.instance_variable_get(:@_middlewares)&.dup    || []

  build_app

  yield self if block_given?
end

Instance Attribute Details

#_appObject (readonly)

Returns the value of attribute _app.



20
21
22
# File 'lib/lennarb.rb', line 20

def _app
  @_app
end

#_loaded_pluginsObject (readonly)

Returns the value of attribute _loaded_plugins.



20
21
22
# File 'lib/lennarb.rb', line 20

def _loaded_plugins
  @_loaded_plugins
end

#_middlewaresObject (readonly)

Returns the value of attribute _middlewares.



20
21
22
# File 'lib/lennarb.rb', line 20

def _middlewares
  @_middlewares
end

#_pluginsObject (readonly)

Returns the value of attribute _plugins.



20
21
22
# File 'lib/lennarb.rb', line 20

def _plugins
  @_plugins
end

#_rootObject (readonly)

Returns the value of attribute _root.



20
21
22
# File 'lib/lennarb.rb', line 20

def _root
  @_root
end

Class Method Details

.delete(path, &block) ⇒ Object



32
# File 'lib/lennarb.rb', line 32

def self.delete(path, &block)  = add_route(path, :DELETE, block)

.freeze!Object



57
58
59
60
61
# File 'lib/lennarb.rb', line 57

def self.freeze!
  app = new
  app.freeze!
  app
end

.get(path, &block) ⇒ Object



27
# File 'lib/lennarb.rb', line 27

def self.get(path, &block)     = add_route(path, :GET, block)

.head(path, &block) ⇒ Object



30
# File 'lib/lennarb.rb', line 30

def self.head(path, &block)    = add_route(path, :HEAD, block)

.inherited(subclass) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/lennarb.rb', line 35

def self.inherited(subclass)
  super
  subclass.instance_variable_set(:@_root, RouteNode.new)
  subclass.instance_variable_set(:@_plugins, [])
  subclass.instance_variable_set(:@_middlewares, @_middlewares&.dup || [])

  Plugin.load_defaults! if Plugin.load_defaults?
end

.options(path, &block) ⇒ Object



33
# File 'lib/lennarb.rb', line 33

def self.options(path, &block) = add_route(path, :OPTIONS, block)

.patch(path, &block) ⇒ Object



31
# File 'lib/lennarb.rb', line 31

def self.patch(path, &block)   = add_route(path, :PATCH, block)

.plugin(plugin_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lennarb.rb', line 44

def self.plugin(plugin_name, *, &)
  @_loaded_plugins ||= {}
  @_plugins        ||= []

  return if @_loaded_plugins.key?(plugin_name)

  plugin_module = Plugin.load(plugin_name)
  plugin_module.configure(self, *, &) if plugin_module.respond_to?(:configure)

  @_loaded_plugins[plugin_name] = plugin_module
  @_plugins << plugin_name
end

.post(path, &block) ⇒ Object



29
# File 'lib/lennarb.rb', line 29

def self.post(path, &block)    = add_route(path, :POST, block)

.put(path, &block) ⇒ Object



28
# File 'lib/lennarb.rb', line 28

def self.put(path, &block)     = add_route(path, :PUT, block)

.use(middleware, *args, &block) ⇒ Object



22
23
24
25
# File 'lib/lennarb.rb', line 22

def self.use(middleware, *args, &block)
  @_middlewares ||= []
  @_middlewares << [middleware, args, block]
end

Instance Method Details

#call(env) ⇒ Object



83
# File 'lib/lennarb.rb', line 83

def call(env) = @_mutex.synchronize { @_app.call(env) }

#delete(path, &block) ⇒ Object



101
# File 'lib/lennarb.rb', line 101

def delete(path, &block)  = add_route(path, :DELETE, block)

#freeze!Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/lennarb.rb', line 85

def freeze!
  return self if @_mounted

  @_root.freeze
  @_plugins.freeze
  @_loaded_plugins.freeze
  @_middlewares.freeze
  @_app.freeze if @_app.respond_to?(:freeze)
  self
end

#get(path, &block) ⇒ Object



96
# File 'lib/lennarb.rb', line 96

def get(path, &block)     = add_route(path, :GET, block)

#head(path, &block) ⇒ Object



99
# File 'lib/lennarb.rb', line 99

def head(path, &block)    = add_route(path, :HEAD, block)

#options(path, &block) ⇒ Object



102
# File 'lib/lennarb.rb', line 102

def options(path, &block) = add_route(path, :OPTIONS, block)

#patch(path, &block) ⇒ Object



100
# File 'lib/lennarb.rb', line 100

def patch(path, &block)   = add_route(path, :PATCH, block)

#plugin(plugin_name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/lennarb.rb', line 104

def plugin(plugin_name, *, &)
  return if @_loaded_plugins.key?(plugin_name)

  plugin_module = Plugin.load(plugin_name)
  self.class.extend plugin_module::ClassMethods     if plugin_module.const_defined?(:ClassMethods)
  self.class.include plugin_module::InstanceMethods if plugin_module.const_defined?(:InstanceMethods)
  plugin_module.configure(self, *, &)               if plugin_module.respond_to?(:configure)
  @_loaded_plugins[plugin_name] = plugin_module
  @_plugins << plugin_name
end

#post(path, &block) ⇒ Object



98
# File 'lib/lennarb.rb', line 98

def post(path, &block)    = add_route(path, :POST, block)

#put(path, &block) ⇒ Object



97
# File 'lib/lennarb.rb', line 97

def put(path, &block)     = add_route(path, :PUT, block)