Class: KAG::Data

Inherits:
Hash
  • Object
show all
Defined in:
lib/kag/data.rb

Instance Method Summary collapse

Methods inherited from Hash

#random_key, #shuffle, #shuffle!

Constructor Details

#initialize(hash = nil) ⇒ Data

Returns a new instance of Data.



7
8
9
10
# File 'lib/kag/data.rb', line 7

def initialize(hash=nil)
  super
  self.merge!(self._load)
end

Instance Method Details

#_loadObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kag/data.rb', line 12

def _load
  unless File.exists?("config/data.json")
    File.open("config/data.json","w") {|f| f.write("{}") }
  end
  f = ::IO.read("config/data.json")
  if f and !f.empty?
    SymbolTable.new(JSON.parse(f))
  else
    SymbolTable.new
  end
end

#add_action(user) ⇒ Object



41
42
43
44
45
46
# File 'lib/kag/data.rb', line 41

def add_action(user)
  self[:action_log] = [] unless self[:action_log]
  self[:action_log] << user.nick
  self[:action_log].shift if self[:action_log].length > 10
  save
end

#flooding?(user) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/kag/data.rb', line 48

def flooding?(user)
  flooding = false
  if self[:action_log]
    flooding = true if self[:action_log].count(user.nick) > 8
  end
  flooding
end

#reloadObject



24
25
26
27
# File 'lib/kag/data.rb', line 24

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

#saveObject



35
36
37
38
39
# File 'lib/kag/data.rb', line 35

def save
  File.open("config/data.json","w") do |f|
    f.write(self.to_json)
  end
end

#store(key, val) ⇒ Object

Sets the value of the given key to val.



30
31
32
33
# File 'lib/kag/data.rb', line 30

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