Module: Upvote
- Extended by:
- Upvote, Configuration
- Included in:
- Upvote
- Defined in:
- lib/upvote.rb,
lib/upvote/version.rb,
lib/upvote/configuration.rb
Defined Under Namespace
Modules: Configuration
Constant Summary
collapse
- VERSION =
'0.0.2'
Instance Method Summary
collapse
default_id_method, default_id_method=, id_method, id_methods, redis, redis=
Instance Method Details
#count(entity) ⇒ Object
34
35
36
|
# File 'lib/upvote.rb', line 34
def count(entity)
redis.zcard set_key(entity)
end
|
#create(subject, target, execute: true) ⇒ Object
12
13
14
15
16
17
18
19
|
# File 'lib/upvote.rb', line 12
def create(subject, target, execute: true)
creation = ->() {
redis.zadd set_key(subject), Time.now.to_f, key(target)
redis.zadd set_key(target), Time.now.to_f, key(subject)
}
execute ? redis.multi { creation.call } : creation
end
|
#exist?(subject, target) ⇒ Boolean
30
31
32
|
# File 'lib/upvote.rb', line 30
def exist?(subject, target)
redis.zscore(set_key(target), key(subject)).present?
end
|
#multi_count(entities) ⇒ Object
62
63
64
|
# File 'lib/upvote.rb', line 62
def multi_count(entities)
redis.multi { entities.each &method(:count) }
end
|
#multi_create(upvotes) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/upvote.rb', line 42
def multi_create(upvotes)
redis.multi do
upvotes.each do |subject, *targets|
targets.each do |target|
create(subject, target, execute: false).call
end
end
end
end
|
#multi_remove(removals) ⇒ Object
52
53
54
55
56
57
58
59
60
|
# File 'lib/upvote.rb', line 52
def multi_remove(removals)
redis.multi do
removals.each do |subject, *targets|
targets.each do |target|
remove(subject, target, execute: false).call
end
end
end
end
|
#multi_voters(entities, start: 0, offset: -1)) ⇒ Object
66
67
68
69
70
|
# File 'lib/upvote.rb', line 66
def multi_voters(entities, start: 0, offset: -1)
redis.multi {
entities.each { |e| voters entities, start: start, offset: offset }
}
end
|
#remove(subject, target, execute: true) ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/upvote.rb', line 21
def remove(subject, target, execute: true)
removal = ->() {
redis.zrem set_key(subject), key(target)
redis.zrem set_key(target), key(subject)
}
execute ? redis.multi { removal.call } : removal
end
|
#voters(entity, start: 0, offset: -1)) ⇒ Object
38
39
40
|
# File 'lib/upvote.rb', line 38
def voters(entity, start: 0, offset: -1)
redis.zrevrange set_key(entity), start, offset
end
|