Skip to content
gesslar edited this page Nov 17, 2024 · 4 revisions

Glu

A modular utility library for Mudlet that emphasizes clean module interaction and easy extensibility.

Core Modules

  • colour: Colour operations
  • date: Time and date operations
  • dependency: Dependency management
  • fd: File and directory operations
  • http: Download management and HTTP handling
  • number: Number utilities
  • preferences: Preferences management (loading and saving)
  • queue: Queue management and processing
  • regex: Module with static properties of regular expressions
  • same: Operations for testing equality
  • string: String operations
  • table: Enhanced table operations
  • test: Unit testing framework
  • timer: Timer operations
  • url: URL parsing and manipulation
  • util: General utilities
  • valid: Input validation
  • version: Version comparison

Using Glu

Installation

As a standalone package

Package developers may require Glu to be installed as package and then depend upon it for their own package. To install Glu as a package, use the following command in the Mudlet console:

lua installPackage("https://github.com/gesslar/Glu/releases/latest/download/Glu.mpackage")

Including in your package

For package developers using muddler, you can include the Glu source files as a subdirectory of your resources directory, for example src/resources/glu.

Basic Usage

As a standalone package

local MyModuleTable.glu = MyModuleTable.glu or Glu("MyPackageName")

Including in your package

-- In your package's init script
local package_name = "__PKGNAME__"
local glu_dir = "Glu"
_G[package_name] = _G[package_name] or
  require("__PKGNAME__/"..glu_dir.."/glu"). -- Load Glu from path relative to your
                                            -- package's root
  ("package_name")                          -- Instantiate Glu with your package name

-- Time formatting
local hours, minutes, seconds = glu.date.shms(3665)  -- "01", "01", "05"
local formatted = glu.date.shms(3665, true)          -- "1h 1m 5s"

-- Table operations
local result = glu.table.includes({1,2,3}, 2)      -- true
local values = glu.table.values({a=1, b=2})        -- {1,2}

-- File operations
glu.fd.write_file("test.txt", "Hello World")
local content = glu.fd.read_file("test.txt")

Developing for Glu

Creating a Module

local TryClass = Glu.glass.register({
  name = "try",                  -- Required: The module name used to access via anchor
  class_name = "TryClass",       -- Required: The class name for the module
  dependencies = {"table"},      -- Optional: Other Glu glass names needed
  inherit_from = nil,            -- Optional: Specify a Glass to inherit from
  call = "clone",                -- Optional: Function to call when instantiating

  setup = function(___, self)    -- Required for functionality
    -- Add methods to your module
    function self.example_method(arg1, arg2)
      -- Access validation through ___.v
      ___.v.type(arg1, "number", 1, false)
      ___.v.type(arg2, "string", 2, false)

      -- Use other modules through the anchor
      return ___.string.capitalize(arg2)
    end
  end,

  -- Optional: Add validation functions
  valid = function(___, self)
    return {
      custom_validation = function(value, argument_index, nil_allowed)
        if nil_allowed and value == nil then
          return
        end

        local last = ___.get_last_traceback_line()
        assert(some_condition, "Invalid value to argument " ..
          argument_index .. " in\n" .. last)
      end
    }
  end
})

The Anchor System

Every module in Glu has access to an anchor (___) through closure, which provides seamless access to all other registered modules and validation functions:

setup = function(___, self)    -- The anchor is passed to setup
  function self.complex_operation(input)
    -- Access any module through the anchor
    if ___.table.includes(input, "test") then
      return ___.date.shms(___.number.round(input[1]))
    end
  end

  function self.validated_operation(num1, num2)
    -- Access validations through ___.v
    ___.v.type(num1, "number", 1, false)  -- Mandatory
    ___.v.type(num2, "number", 2, true)   -- Optional

    return ___.number.round(num1/num2, 2)
  end
end

Dependencies

Declare module dependencies in the registration object. Dependencies refer to other Glu glass names that your module needs to access through the anchor:

dependencies = {"table", "string", "number"}

Validation Functions

Modules can provide their own validation functions by including a valid function in their registration. These functions become available through the anchor's v table:

valid = function(___, self)
  return {
    my_validator = function(value, argument_index, nil_allowed)
      -- Validation logic here
    end
  }
end

-- Usage in other modules
___.v.my_validator(value, 1, false)

Best Practices

  • Use the anchor (___) for cross-module access
  • Declare all required dependencies
  • Keep methods focused and pure
  • Use validation through ___.v for method inputs
  • Follow the established module registration pattern
  • Add documentation for all methods

License

IDGAF