Skip to content

Commit

Permalink
rvmrc and test tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
mrb committed Oct 28, 2010
1 parent 70292af commit 4245de8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm 1.8.7
19 changes: 9 additions & 10 deletions lib/achievements.rb
Expand Up @@ -98,7 +98,8 @@ class Engine
def initialize(contexts)
connect if @redis.nil?
@contexts = contexts
@achievements = @contexts.collect{|c| {c=>[]}}
@achievements = {}
@contexts.collect{|c| @achievements[c] = []}
end

def connect
Expand Down Expand Up @@ -126,7 +127,7 @@ def trigger(context, agent_id, name)
counter = Counter.new(context,agent_id,name)
result = incr counter
# Check Threshold
if result > @redis.get("#{context}:#{name}")
if result > (@redis.get("#{context}:#{name}") || 0)
achieved << [context,name]
return achieved
else
Expand All @@ -149,14 +150,7 @@ def deactiveate(counter)
end

## Class Methods
def self.find_achievement(name)
results = @achievements.delete_if{|achievement| achievement.name != name}
if result = results.uniq[0]
result
else
nil
end
end


end

Expand All @@ -171,6 +165,11 @@ def initialize(context, name, threshold)
@threshold = threshold
@context = context
end

def to_hash
{:name => @name, :threshold => @threshold, :context => @context}
end

end

# Counter class is really a Redis key factory. Responsibility of
Expand Down
7 changes: 5 additions & 2 deletions test/achievements_test.rb
Expand Up @@ -39,8 +39,11 @@ class Item
end

test "achievement instantiation" do
assert_equal User.engine.achievements, [{:context1 => [:one_time]},
{:context2 => [:three_times]}]

end

test "binding achievement should create key value pair with name, context, threshold" do
assert_equal User.redis.get("")
end

test "first time trigger should create two counters and increment both" do
Expand Down

0 comments on commit 4245de8

Please sign in to comment.