Skip to content

Commit

Permalink
couple refactorings; renamed methods and removed a bit of duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
zerokarmaleft committed Jan 31, 2012
1 parent c682689 commit f19cf6f
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions plugins/karma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ def increment(m, nick)
elsif nick == m.user.nick
m.reply "Just keep patting yourself on the back there, sport."
else
@users[nick] += 1
save
score(m, nick)
update_user(nick) { |nick| @users[nick] += 1 }
show_score(m, nick)
end
end

Expand All @@ -36,25 +35,29 @@ def decrement(m, nick)
elsif nick == m.user.nick
m.reply "There are special rooms on this network for self-flagellation."
else
@users[nick] -= 1
save
score(m, nick)
update_user(nick) { |nick| @users[nick] -= 1 }
show_score(m, nick)
end
end

match /karma\?(\s+)?(\S+)?/, method: :scores
def scores(m, cmd, nick)
match /karma\?(\s+)?(\S+)?/, method: :show_scores
def show_scores(m, cmd, nick)
if nick
score(m, nick)
show_score(m, nick)
else
@users.each { |nick, score| score(m, nick) }
@users.each { |nick, score| show_score(m, nick) }
end
end

def score(m, nick)
def show_score(m, nick)
m.reply "#{ nick } has #{ @users[nick] } awesome points."
end

def update_user(nick)
yield(nick)
save
end

def save
File.open(@scores_file, "w") do |f|
f.write(@users.to_json)
Expand Down

0 comments on commit f19cf6f

Please sign in to comment.