Module: TreasureHunt::Hunter::Model::InstanceMethods

Defined in:
lib/treasure_hunt/treasure_hunter.rb

Instance Method Summary collapse

Instance Method Details

#achieve!(reward, target = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/treasure_hunt/treasure_hunter.rb', line 36

def achieve!(reward, target=nil)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol
  achievement = ::Achievement.create(:reward => reward, :targetable => target)
  self.achievements << achievement

  achievement
end

#can_achieve?(reward) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/treasure_hunt/treasure_hunter.rb', line 30

def can_achieve?(reward)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol

  ::Achievement.new(:user => self, :reward => reward).valid?
end

#has_achievement?(reward) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/treasure_hunt/treasure_hunter.rb', line 52

def has_achievement?(reward)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol
  achievements.where(:reward_id => reward.id).any?
end

#rankObject



22
23
24
# File 'lib/treasure_hunt/treasure_hunter.rb', line 22

def rank
  self.class.count(:all, :conditions => ['points > ?', self.points]) + 1
end

#time_to_unlock(reward) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/treasure_hunt/treasure_hunter.rb', line 44

def time_to_unlock(reward)
  reward = ::Reward.find_by_name(reward.to_s.humanize) if reward.is_a? Symbol
  achievement = ::Achievement.new(:user => self, :reward => reward)
  remaining = (achievement.find_all_recently_achieved.last.updated_at rescue DateTime.new ) + reward.every.seconds - DateTime.now

  [0, remaining].max
end

#update_pointsObject



26
27
28
# File 'lib/treasure_hunt/treasure_hunter.rb', line 26

def update_points
  self.update_attribute(:points, self.achievements.sum(:points))
end