Module: Lich::Common::UserVars

Defined in:
documented/common/uservars.rb

Overview

Provides a compatibility layer and additional convenience methods for Vars. This module delegates most operations to Vars while providing legacy method support and some additional functionality like comma-separated list management.

Provides methods for managing user-specific variables This module acts as a compatibility layer for variable management.

Examples:

Basic delegation

UserVars['my_var'] = 'value'
UserVars.my_var  #=> 'value'

Managing comma-separated lists

UserVars.change('tags', 'foo, bar')
UserVars.add('tags', 'baz')  #=> 'foo, bar, baz'

Accessing a user variable

UserVars["my_var"] = "value"

See Also:

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object?

Retrieves a variable value by name (bracket notation)

Delegates to Vars.[].

Parameters:

  • name (String, Symbol)

    the variable name

Returns:

  • (Object, nil)

    the variable value, or nil if not set



45
46
47
# File 'documented/common/uservars.rb', line 45

def UserVars.[](name)
  Vars[name]
end

.[]=(name, val) ⇒ Object?

Sets a variable value by name (bracket notation)

Delegates to Vars.[]=.

Parameters:

  • name (String, Symbol)

    the variable name

  • val (Object, nil)

    the value to set; nil deletes the variable

Returns:

  • (Object, nil)

    the value that was set



57
58
59
# File 'documented/common/uservars.rb', line 57

def UserVars.[]=(name, val)
  Vars[name] = val
end

.add(var_name, value, _t = nil) ⇒ String

Note:

This method assumes the variable contains a comma-separated string. If the variable contains a different data type, this will raise an error.

Adds a value to a comma-separated list variable

If the variable contains a comma-separated string, this method appends the new value to the list. If the variable doesn’t exist or is nil, it creates a new single-value string.

The third parameter is ignored and exists for legacy API compatibility.

Examples:

UserVars['tags'] = 'foo, bar'
UserVars.add('tags', 'baz')  #=> 'foo, bar, baz'

Parameters:

  • var_name (String, Symbol)

    the variable name

  • value (String)

    the value to add to the list

  • _t (Object) (defaults to: nil)

    unused legacy parameter (ignored)

Returns:

  • (String)

    the updated comma-separated list



126
127
128
129
130
131
132
133
# File 'documented/common/uservars.rb', line 126

def UserVars.add(var_name, value, _t = nil)
  current = Vars[var_name]
  if current.nil? || current.empty?
    Vars[var_name] = value.to_s
  else
    Vars[var_name] = current.to_s.split(', ').push(value.to_s).join(', ')
  end
end

.change(var_name, value, _t = nil) ⇒ Object

Sets a variable to a new value

This is a convenience method for backward compatibility. The third parameter is ignored and exists for legacy API compatibility.

Examples:

UserVars.change('my_var', 'new value')

Parameters:

  • var_name (String, Symbol)

    the variable name

  • value (Object)

    the new value

  • _t (Object) (defaults to: nil)

    unused legacy parameter (ignored)

Returns:

  • (Object)

    the value that was set



102
103
104
# File 'documented/common/uservars.rb', line 102

def UserVars.change(var_name, value, _t = nil)
  Vars[var_name] = value
end

.delete(var_name, _t = nil) ⇒ nil

Deletes a variable by setting it to nil

This is a convenience method for backward compatibility. The second parameter is ignored and exists for legacy API compatibility.

Examples:

UserVars.delete('my_var')

Parameters:

  • var_name (String, Symbol)

    the variable name

  • _t (Object) (defaults to: nil)

    unused legacy parameter (ignored)

Returns:

  • (nil)


147
148
149
# File 'documented/common/uservars.rb', line 147

def UserVars.delete(var_name, _t = nil)
  Vars[var_name] = nil
end

.listHash

Returns a duplicate of all variables as a Hash

Delegates to Vars.list.

Examples:

all_vars = UserVars.list
all_vars.keys  #=> ['var1', 'var2', ...]

Returns:

  • (Hash)

    a copy of all stored variables with string keys



34
35
36
# File 'documented/common/uservars.rb', line 34

def UserVars.list
  Vars.list
end

.list_charHash

Returns a duplicate of all character-specific variables as a Hash

This is an alias for #list provided for backward compatibility.

Examples:

char_vars = UserVars.list_char
char_vars.keys  #=> ['var1', 'var2', ...]

Returns:

  • (Hash)

    a copy of all stored variables with string keys



174
175
176
# File 'documented/common/uservars.rb', line 174

def UserVars.list_char
  Vars.list
end

.list_globalArray

Deprecated.

Use #list or #list_char instead

Returns an empty array (legacy global variables are not supported)

This method exists for backward compatibility with older code that expected separate global and character-specific variables. In the current implementation, all variables are character-specific.

Returns:

  • (Array)

    always returns an empty array



160
161
162
# File 'documented/common/uservars.rb', line 160

def UserVars.list_global
  Array.new
end

.method_missing(method_name, *args) ⇒ Object?

Handles dynamic method calls for variable access

Delegates to Vars.method_missing to support both getter and setter syntax.

Examples:

UserVars.my_setting = 'value'
UserVars.my_setting  #=> 'value'

Parameters:

  • method_name (Symbol)

    the method name being called

  • args (Array)

    arguments passed to the method

Returns:

  • (Object, nil)

    the variable value or result of setter



73
74
75
# File 'documented/common/uservars.rb', line 73

def UserVars.method_missing(method_name, *args)
  Vars.method_missing(method_name, *args)
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Declares that method_missing can respond to valid Ruby method names

Delegates to Vars.respond_to_missing?.

Parameters:

  • method_name (Symbol)

    the method name to check

  • include_private (Boolean) (defaults to: false)

    whether to include private methods

Returns:

  • (Boolean)

    true if the method name is a valid variable name



85
86
87
# File 'documented/common/uservars.rb', line 85

def UserVars.respond_to_missing?(method_name, include_private = false)
  Vars.respond_to_missing?(method_name, include_private)
end