Module: Lich::Common::Vars

Defined in:
lib/common/vars.rb

Constant Summary collapse

@@vars =
Hash.new
@@loaded =
false
@@load =

Proc to load variables from the database.

Returns:

Raises:

  • (SQLite3::BusyException)

    if the database is busy.

proc {
  Lich.db_mutex.synchronize {
    unless @@loaded
      begin
        h = Lich.db.get_first_value('SELECT hash FROM uservars WHERE scope=?;', ["#{XMLData.game}:#{XMLData.name}".encode('UTF-8')])
      rescue SQLite3::BusyException
        sleep 0.1
        retry
      end
      if h
        begin
          hash = Marshal.load(h)
          hash.each { |k, v| @@vars[k] = v }
          md5 = Digest::MD5.hexdigest(hash.to_s)
        rescue
          respond "--- Lich: error: #{$!}"
          respond $!.backtrace[0..2]
        end
      end
      @@loaded = true
    end
  }
  nil
}
@@save =

Proc to save variables to the database.

Returns:

Raises:

  • (SQLite3::BusyException)

    if the database is busy.

proc {
  Lich.db_mutex.synchronize {
    if @@loaded
      if Digest::MD5.hexdigest(@@vars.to_s) != md5
        md5 = Digest::MD5.hexdigest(@@vars.to_s)
        blob = SQLite3::Blob.new(Marshal.dump(@@vars))
        begin
          Lich.db.execute('INSERT OR REPLACE INTO uservars(scope,hash) VALUES(?,?);', ["#{XMLData.game}:#{XMLData.name}".encode('UTF-8'), blob])
        rescue SQLite3::BusyException
          sleep 0.1
          retry
        end
      end
    end
  }
  nil
}

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object, NilClass

Retrieves the value associated with the given name.

Parameters:

  • name (String)

    the name of the variable to retrieve.

Returns:

  • (Object, NilClass)

    the value associated with the name, or nil if not found.



79
80
81
82
# File 'lib/common/vars.rb', line 79

def Vars.[](name)
  @@load.call unless @@loaded
  @@vars[name]
end

.[]=(name, val) ⇒ NilClass

Sets the value for the given name.

Parameters:

  • name (String)

    the name of the variable to set.

  • val (Object, NilClass)

    the value to assign, or nil to delete the variable.

Returns:



89
90
91
92
93
94
95
96
# File 'lib/common/vars.rb', line 89

def Vars.[]=(name, val)
  @@load.call unless @@loaded
  if val.nil?
    @@vars.delete(name)
  else
    @@vars[name] = val
  end
end

.listHash

Returns a duplicate of the current variables hash.

Returns:

  • (Hash)

    a duplicate of the variables hash.



101
102
103
104
# File 'lib/common/vars.rb', line 101

def Vars.list
  @@load.call unless @@loaded
  @@vars.dup
end

.method_missing(arg1, arg2 = '') ⇒ Object, NilClass

Note:

This method will call @@load if the variables have not been loaded yet.

Handles dynamic method calls for getting and setting variables.

Parameters:

  • arg1 (Symbol)

    the name of the variable or the setter method (ending with ‘=’).

  • arg2 (Object, NilClass) (defaults to: '')

    the value to set if it’s a setter method.

Returns:

  • (Object, NilClass)

    the value of the variable or nil if deleted.



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/common/vars.rb', line 119

def Vars.method_missing(arg1, arg2 = '')
  @@load.call unless @@loaded
  if arg1[-1, 1] == '='
    if arg2.nil?
      @@vars.delete(arg1.to_s.chop)
    else
      @@vars[arg1.to_s.chop] = arg2
    end
  else
    @@vars[arg1.to_s]
  end
end

.saveNilClass

Saves the current variables to the database.

Returns:



109
110
111
# File 'lib/common/vars.rb', line 109

def Vars.save
  @@save.call
end