Module: Recommendable::Rater::Disliker
- Defined in:
- lib/recommendable/rater/disliker.rb
Instance Method Summary collapse
-
#dislike(obj) ⇒ Object
Dislike an object.
-
#disliked_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has disliked.
-
#disliked_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has disliked.
-
#disliked_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has disliked.
-
#disliked_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have disliked.
-
#disliked_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have disliked.
-
#dislikes ⇒ Array
Retrieve an array of objects the user has disliked.
-
#dislikes?(obj) ⇒ Boolean
Check whether the user has disliked an object.
-
#dislikes_count ⇒ Fixnum
Get the number of items the user has disliked.
-
#dislikes_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have disliked.
-
#undislike(obj) ⇒ Object
Undislike an object.
Instance Method Details
#dislike(obj) ⇒ Object
Dislike an object. This will remove the item from a user’s set of likes or hidden items
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/recommendable/rater/disliker.rb', line 10 def dislike(obj) raise(ArgumentError, 'Object has not been declared ratable.') unless obj.respond_to?(:recommendable?) && obj.recommendable? return if dislikes?(obj) run_hook(:before_dislike, obj) Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(obj.class, id), obj.id) Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.disliked_by_set_for(obj.class, obj.id), id) run_hook(:after_dislike, obj) true end |
#disliked_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has disliked
90 91 92 |
# File 'lib/recommendable/rater/disliker.rb', line 90 def disliked_count_for(klass) Recommendable.redis.scard(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(klass, id)) end |
#disliked_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has disliked
82 83 84 |
# File 'lib/recommendable/rater/disliker.rb', line 82 def disliked_for(klass) Recommendable.query(klass, disliked_ids_for(klass)) end |
#disliked_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has disliked
72 73 74 75 76 |
# File 'lib/recommendable/rater/disliker.rb', line 72 def disliked_ids_for(klass) ids = Recommendable.redis.smembers(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(klass, id)) ids.map!(&:to_i) if [:active_record, :data_mapper, :sequel].include?(Recommendable.config.orm) ids end |
#disliked_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have disliked
109 110 111 112 |
# File 'lib/recommendable/rater/disliker.rb', line 109 def disliked_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.disliked_set_for(klass, id), Recommendable::Helpers::RedisKeyMapper.disliked_set_for(klass, user_id)) end |
#disliked_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have disliked
99 100 101 |
# File 'lib/recommendable/rater/disliker.rb', line 99 def disliked_in_common_with(klass, user) Recommendable.query(klass, disliked_ids_in_common_with(klass, user)) end |
#dislikes ⇒ Array
Retrieve an array of objects the user has disliked
48 49 50 |
# File 'lib/recommendable/rater/disliker.rb', line 48 def dislikes Recommendable.config.ratable_classes.map { |klass| disliked_for(klass) }.flatten end |
#dislikes?(obj) ⇒ Boolean
Check whether the user has disliked an object.
26 27 28 |
# File 'lib/recommendable/rater/disliker.rb', line 26 def dislikes?(obj) Recommendable.redis.sismember(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(obj.class, id), obj.id) end |
#dislikes_count ⇒ Fixnum
Get the number of items the user has disliked
62 63 64 65 66 |
# File 'lib/recommendable/rater/disliker.rb', line 62 def dislikes_count Recommendable.config.ratable_classes.inject(0) do |sum, klass| sum + disliked_count_for(klass) end end |
#dislikes_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have disliked
55 56 57 |
# File 'lib/recommendable/rater/disliker.rb', line 55 def dislikes_in_common_with(user) Recommendable.config.ratable_classes.map { |klass| disliked_in_common_with(klass, user) }.flatten end |
#undislike(obj) ⇒ Object
Undislike an object. This removes the object from a user’s set of dislikes.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/recommendable/rater/disliker.rb', line 34 def undislike(obj) return unless dislikes?(obj) run_hook(:before_undislike, obj) Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(obj.class, id), obj.id) Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.disliked_by_set_for(obj.class, obj.id), id) run_hook(:after_undislike, obj) true end |