Module: Recommendable::Rater::Hider
- Defined in:
- lib/recommendable/rater/hider.rb
Instance Method Summary collapse
-
#hidden_count ⇒ Fixnum
Get the number of items the user has hidden.
-
#hidden_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has hidden.
-
#hidden_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has hidden.
-
#hidden_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has hidden.
-
#hidden_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have hidden.
-
#hidden_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have hidden.
-
#hide(obj) ⇒ Object
Hide an object.
-
#hides?(obj) ⇒ Boolean
Check whether the user has hidden an object.
-
#hiding ⇒ Array
Retrieve an array of objects the user has hidden.
-
#hiding_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have hidden.
-
#unhide(obj) ⇒ Object
Unhide an object.
Instance Method Details
#hidden_count ⇒ Fixnum
Get the number of items the user has hidden
60 61 62 63 64 |
# File 'lib/recommendable/rater/hider.rb', line 60 def hidden_count Recommendable.config.ratable_classes.inject(0) do |sum, klass| sum + hidden_count_for(klass) end end |
#hidden_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has hidden
88 89 90 |
# File 'lib/recommendable/rater/hider.rb', line 88 def hidden_count_for(klass) Recommendable.redis.scard(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(klass, id)) end |
#hidden_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has hidden
80 81 82 |
# File 'lib/recommendable/rater/hider.rb', line 80 def hidden_for(klass) Recommendable.query(klass, hidden_ids_for(klass)) end |
#hidden_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has hidden
70 71 72 73 74 |
# File 'lib/recommendable/rater/hider.rb', line 70 def hidden_ids_for(klass) ids = Recommendable.redis.smembers(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(klass, id)) ids.map!(&:to_i) if [:active_record, :data_mapper, :sequel].include?(Recommendable.config.orm) ids end |
#hidden_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have hidden
107 108 109 110 |
# File 'lib/recommendable/rater/hider.rb', line 107 def hidden_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.hidden_set_for(klass, id), Recommendable::Helpers::RedisKeyMapper.hidden_set_for(klass, user_id)) end |
#hidden_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have hidden
97 98 99 |
# File 'lib/recommendable/rater/hider.rb', line 97 def hidden_in_common_with(klass, user) Recommendable.query(klass, hidden_ids_in_common_with(klass, user)) end |
#hide(obj) ⇒ Object
Hide an object. This action is only possible if the user has not yet rated or bookmarked the object.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/recommendable/rater/hider.rb', line 10 def hide(obj) raise(ArgumentError, 'Object has not been declared ratable.') unless obj.respond_to?(:recommendable?) && obj.recommendable? return if likes?(obj) || dislikes?(obj) || bookmarks?(obj) || hides?(obj) run_hook(:before_hide, obj) Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(obj.class, id), obj.id) run_hook(:after_hide, obj) true end |
#hides?(obj) ⇒ Boolean
Check whether the user has hidden an object.
25 26 27 |
# File 'lib/recommendable/rater/hider.rb', line 25 def hides?(obj) Recommendable.redis.sismember(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(obj.class, id), obj.id) end |
#hiding ⇒ Array
Retrieve an array of objects the user has hidden
46 47 48 |
# File 'lib/recommendable/rater/hider.rb', line 46 def hiding Recommendable.config.ratable_classes.map { |klass| hidden_for(klass) }.flatten end |
#hiding_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have hidden
53 54 55 |
# File 'lib/recommendable/rater/hider.rb', line 53 def hiding_in_common_with(user) Recommendable.config.ratable_classes.map { |klass| hidden_in_common_with(klass, user) }.flatten end |
#unhide(obj) ⇒ Object
Unhide an object. This removes the object from a user’s set of hidden items.
33 34 35 36 37 38 39 40 41 |
# File 'lib/recommendable/rater/hider.rb', line 33 def unhide(obj) return unless hides?(obj) run_hook(:before_unhide, obj) Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(obj.class, id), obj.id) run_hook(:after_unhide, obj) true end |