Class: Lita::Handlers::Karma::Term

Inherits:
Object
  • Object
show all
Includes:
Lita::Handler::Common
Defined in:
lib/lita/handlers/karma/term.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot, term, normalize: true) ⇒ Term

Returns a new instance of Term.



28
29
30
31
32
# File 'lib/lita/handlers/karma/term.rb', line 28

def initialize(robot, term, normalize: true)
  super(robot)
  @term = normalize ? normalize_term(term) : term
  @link_cache = {}
end

Instance Attribute Details

#termObject (readonly)

Returns the value of attribute term.



7
8
9
# File 'lib/lita/handlers/karma/term.rb', line 7

def term
  @term
end

Class Method Details

.list_best(robot, n = 5) ⇒ Object



10
11
12
# File 'lib/lita/handlers/karma/term.rb', line 10

def list_best(robot, n = 5)
  list(:zrevrange, robot, n)
end

.list_worst(robot, n = 5) ⇒ Object



14
15
16
# File 'lib/lita/handlers/karma/term.rb', line 14

def list_worst(robot, n = 5)
  list(:zrange, robot, n)
end

Instance Method Details

#checkObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/lita/handlers/karma/term.rb', line 34

def check
  string = "#{self}: #{total_score}"

  unless links_with_scores.empty?
    link_text = links_with_scores.map { |term, score| "#{term}: #{score}" }.join(", ")
    string << " (#{own_score}), #{t("linked_to")}: #{link_text}"
  end

  string
end

#decrement(user) ⇒ Object



45
46
47
# File 'lib/lita/handlers/karma/term.rb', line 45

def decrement(user)
  modify(user, -1)
end

#deleteObject



49
50
51
52
53
54
55
56
57
# File 'lib/lita/handlers/karma/term.rb', line 49

def delete
  redis.zrem("terms", to_s)
  redis.del("modified:#{self}")
  redis.del("links:#{self}")
  redis.smembers("linked_to:#{self}").each do |key|
    redis.srem("links:#{key}", to_s)
  end
  redis.del("linked_to:#{self}")
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/lita/handlers/karma/term.rb', line 59

def eql?(other)
  term.eql?(other.term)
end

#hashObject



63
64
65
# File 'lib/lita/handlers/karma/term.rb', line 63

def hash
  term.hash
end

#increment(user) ⇒ Object



67
68
69
# File 'lib/lita/handlers/karma/term.rb', line 67

def increment(user)
  modify(user, 1)
end


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lita/handlers/karma/term.rb', line 71

def link(other)
  if config.link_karma_threshold
    threshold = config.link_karma_threshold.abs

    if own_score.abs < threshold || other.own_score.abs < threshold
      return threshold
    end
  end

  redis.sadd("links:#{self}", other.to_s) && redis.sadd("linked_to:#{other}", to_s)
end


83
84
85
86
87
88
89
90
# File 'lib/lita/handlers/karma/term.rb', line 83

def links
  @links ||= begin
    redis.smembers("links:#{self}").each do |term|
      linked_term = self.class.new(robot, term)
      @link_cache[linked_term.term] = linked_term
    end
  end
end


92
93
94
95
96
97
98
99
100
# File 'lib/lita/handlers/karma/term.rb', line 92

def links_with_scores
  @links_with_scores ||= begin
    {}.tap do |h|
      links.each do |link|
        h[link] = @link_cache[link].own_score
      end
    end
  end
end

#modifiedObject



102
103
104
105
106
# File 'lib/lita/handlers/karma/term.rb', line 102

def modified
  redis.smembers("modified:#{term}").map do |user_id|
    Lita::User.find_by_id(user_id)
  end
end

#own_scoreObject



108
109
110
# File 'lib/lita/handlers/karma/term.rb', line 108

def own_score
  @own_score ||= redis.zscore("terms", term).to_i
end

#to_sObject



112
113
114
# File 'lib/lita/handlers/karma/term.rb', line 112

def to_s
  term
end

#total_scoreObject



116
117
118
119
120
121
122
# File 'lib/lita/handlers/karma/term.rb', line 116

def total_score
  @total_score ||= begin
    links.inject(own_score) do |memo, linked_term|
      memo + @link_cache[linked_term].own_score
    end
  end
end


124
125
126
# File 'lib/lita/handlers/karma/term.rb', line 124

def unlink(other)
  redis.srem("links:#{self}", other.to_s) && redis.srem("linked_to:#{other}", to_s)
end