Module: Recommendable::Rater::Liker
- Defined in:
- lib/recommendable/rater/liker.rb
Instance Method Summary collapse
-
#like(obj) ⇒ Object
Like an object.
-
#liked_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has liked.
-
#liked_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has liked.
-
#liked_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has liked.
-
#liked_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have liked.
-
#liked_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have liked.
-
#likes ⇒ Array
Retrieve an array of objects the user has liked.
-
#likes?(obj) ⇒ Boolean
Check whether the user has liked an object.
-
#likes_count ⇒ Fixnum
Get the number of items the user has liked.
-
#likes_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have liked.
-
#unlike(obj) ⇒ Object
Unlike an object.
Instance Method Details
#like(obj) ⇒ Object
Like an object. This will remove the item from a user’s set of dislikes, or hidden items.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/recommendable/rater/liker.rb', line 10 def like(obj) raise(ArgumentError, 'Object has not been declared ratable.') unless obj.respond_to?(:recommendable?) && obj.recommendable? return if likes?(obj) run_hook(:before_like, obj) Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.liked_set_for(obj.class, id), obj.id) Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.liked_by_set_for(obj.class, obj.id), id) run_hook(:after_like, obj) true end |
#liked_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has liked
90 91 92 |
# File 'lib/recommendable/rater/liker.rb', line 90 def liked_count_for(klass) Recommendable.redis.scard(Recommendable::Helpers::RedisKeyMapper.liked_set_for(klass, id)) end |
#liked_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has liked
82 83 84 |
# File 'lib/recommendable/rater/liker.rb', line 82 def liked_for(klass) Recommendable.query(klass, liked_ids_for(klass)) end |
#liked_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has liked
72 73 74 75 76 |
# File 'lib/recommendable/rater/liker.rb', line 72 def liked_ids_for(klass) ids = Recommendable.redis.smembers(Recommendable::Helpers::RedisKeyMapper.liked_set_for(klass, id)) ids.map!(&:to_i) if [:active_record, :data_mapper, :sequel].include?(Recommendable.config.orm) ids end |
#liked_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have liked
109 110 111 112 |
# File 'lib/recommendable/rater/liker.rb', line 109 def liked_ids_in_common_with(klass, user_id) user_id = user_id.id if user_id.is_a?(Recommendable.config.user_class) Recommendable.redis.sinter(Recommendable::Helpers::RedisKeyMapper.liked_set_for(klass, id), Recommendable::Helpers::RedisKeyMapper.liked_set_for(klass, user_id)) end |
#liked_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have liked
99 100 101 |
# File 'lib/recommendable/rater/liker.rb', line 99 def liked_in_common_with(klass, user) Recommendable.query(klass, liked_ids_in_common_with(klass, user)) end |
#likes ⇒ Array
Retrieve an array of objects the user has liked
48 49 50 |
# File 'lib/recommendable/rater/liker.rb', line 48 def likes Recommendable.config.ratable_classes.map { |klass| liked_for(klass) }.flatten end |
#likes?(obj) ⇒ Boolean
Check whether the user has liked an object.
26 27 28 |
# File 'lib/recommendable/rater/liker.rb', line 26 def likes?(obj) Recommendable.redis.sismember(Recommendable::Helpers::RedisKeyMapper.liked_set_for(obj.class, id), obj.id) end |
#likes_count ⇒ Fixnum
Get the number of items the user has liked
62 63 64 65 66 |
# File 'lib/recommendable/rater/liker.rb', line 62 def likes_count Recommendable.config.ratable_classes.inject(0) do |sum, klass| sum + liked_count_for(klass) end end |
#likes_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have liked
55 56 57 |
# File 'lib/recommendable/rater/liker.rb', line 55 def likes_in_common_with(user) Recommendable.config.ratable_classes.map { |klass| liked_in_common_with(klass, user) }.flatten end |
#unlike(obj) ⇒ Object
Unlike an object. This removes the object from a user’s set of likes.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/recommendable/rater/liker.rb', line 34 def unlike(obj) return unless likes?(obj) run_hook(:before_unlike, obj) Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.liked_set_for(obj.class, id), obj.id) Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.liked_by_set_for(obj.class, obj.id), id) run_hook(:after_unlike, obj) true end |