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.
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.
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
-
.[](name) ⇒ Object, NilClass
Retrieves the value associated with the given name.
-
.[]=(name, val) ⇒ NilClass
Sets the value for the given name.
-
.list ⇒ Hash
Returns a duplicate of the current variables hash.
-
.method_missing(arg1, arg2 = '') ⇒ Object, NilClass
Handles dynamic method calls for getting and setting variables.
-
.save ⇒ NilClass
Saves the current variables to the database.
Class Method Details
.[](name) ⇒ Object, NilClass
Retrieves the value associated with the given name.
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.
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 |
.list ⇒ Hash
Returns a duplicate of the current 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.
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 |