module Ohm::Tallyable::Macros

Public Instance Methods

_has_tally(attribute, by=nil) click to toggle source
# File lib/ohm/tallyable.rb, line 29
def _has_tally(attribute, by=nil)
  tally = tallies[attribute]
  !!(tally && (!tally[:by] || (by && by.include?(tally[:by]))))
end
_load_zset(key) click to toggle source
# File lib/ohm/tallyable.rb, line 48
def _load_zset(key)
  key.zrevrange(0, -1, with_scores: true)
end
_tally_key(attribute, by=nil) click to toggle source
# File lib/ohm/tallyable.rb, line 34
def _tally_key(attribute, by=nil)
  key = self.key[:tallies][attribute]
  if by
    key = key[by.keys.first][by.values.first]
  end
  key
end
_tally_keys(attribute) click to toggle source
# File lib/ohm/tallyable.rb, line 42
def _tally_keys(attribute)
  keys = db.keys(_tally_key(attribute))
  keys.concat(db.keys(_tally_key(attribute)["*"]))
end
leaderboard(attribute, by=nil) click to toggle source
# File lib/ohm/tallyable.rb, line 21
def leaderboard(attribute, by=nil)
  raise ArgumentError unless _has_tally(attribute, by)

  _load_zset(_tally_key(attribute, by))
    .map { |k, v| [k, v.to_i] }
    .sort_by { |k, v| [-v, k] }
end
retally(attribute) click to toggle source
# File lib/ohm/tallyable.rb, line 15
def retally(attribute)
  raise ArgumentError unless tallies.include?(attribute)
  db.del(*_tally_keys(attribute))
  all.each { |e| e.send(:_increment_tallies) }
end
tallies() click to toggle source
# File lib/ohm/tallyable.rb, line 11
def tallies
  @tallies ||= {}
end
tally(attribute, options={}) click to toggle source
# File lib/ohm/tallyable.rb, line 7
def tally(attribute, options={})
  tallies[attribute] = options
end