Class: KAG::User::User

Inherits:
SymbolTable
  • Object
show all
Defined in:
lib/kag/user/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ User

Returns a new instance of User.



8
9
10
11
12
# File 'lib/kag/user/user.rb', line 8

def initialize(user)
  self[:authname] = user.authname
  super({})
  self.merge!(_load)
end

Class Method Details

.add_stat(user, stat, increment = 1) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/kag/user/user.rb', line 31

def self.add_stat(user,stat,increment = 1)
  stat = stat.to_sym
  u = KAG::User::User.new(user)
  u[stat] = 0 unless u[stat]
  u[stat] = u[stat].to_i + increment.to_i
  u.save
end

.stats(user) ⇒ Object



50
51
52
53
# File 'lib/kag/user/user.rb', line 50

def self.stats(user)
  u = KAG::User::User.new(user)
  "#{user.nick} has played in #{u.matches.to_i} matches, has added #{u.adds.to_i} times, subbed #{u.substitutions.to_i} times, and has deserted matches #{u.desertions.to_i} times."
end

.subtract_stat(user, stat, decrement = 1) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/kag/user/user.rb', line 39

def self.subtract_stat(user,stat,decrement = 1)
  stat = stat.to_sym
  u = KAG::User::User.new(user)
  if u[stat]
    u[stat] = u[stat].to_i - decrement.to_i
  else
    u[stat] = 0
  end
  u.save
end

Instance Method Details

#reloadObject



14
15
16
17
# File 'lib/kag/user/user.rb', line 14

def reload
  puts "Reloading data file..."
  self.merge!(self._load)
end

#saveObject



25
26
27
28
29
# File 'lib/kag/user/user.rb', line 25

def save
  File.open("data/user/#{self.authname}.json","w") do |f|
    f.write(self.to_json)
  end
end

#store(key, val) ⇒ Object

Sets the value of the given key to val.



20
21
22
23
# File 'lib/kag/user/user.rb', line 20

def store(key, val)
  super(key,val)
  self.save
end