Discord

Preparing your files

Please wait until the download is ready.

Luka's Scripting Utilities

Luka's Scripting Utilities

by Luka S.J.

A pack full of various scripting utilities to ease your development!

v4.1.135,194
Download144 KB

Helper Classes

Two small base classes used throughout LUTS for declaring data and deferring
callbacks.

AttributeContainer

AttributeContainer is a base class for objects that declare a fixed set of
attributes with default values. Subclass it and register attributes; each one
gets a reader, a writer, and — when the default is a boolean — a ? predicate.

class Settings < AttributeContainer
  with_attribute :volume, default: 100
  with_attribute :muted,  default: false
end

s = Settings.new
s.volume        # => 100
s.muted?        # => false  (predicate, because the default is boolean)
s.volume = 50
  • SubClass.with_attribute(key, default: nil) declares an attribute (and its predicate, for booleans)
  • SubClass.attribute_list the registered attributes mapped to their defaults
  • SubClass.new initialises every attribute to its default value

Attribute definitions are inherited by further subclasses.

CallbackWrapper

CallbackWrapper stores a block and executes it later in an isolated context,
with keyword params exposed inside the block as instance variables.

cb = CallbackWrapper.with_params(name: "Pikachu") { puts @name }
cb.execute                       # prints "Pikachu"

# equivalent long form:
cb = CallbackWrapper.new { puts @name }
cb.set_params(name: "Pikachu")
cb.execute
  • CallbackWrapper.with_params(**kwargs, &block) builds a wrapper and immediately sets its params
  • wrapper.set_params(**kwargs) exposes the given keywords as instance variables in the block context (chainable — returns self)
  • wrapper.execute runs the block in the isolated context and returns its result (or nil if no block was given)