Module: Recommendable::Rater::Bookmarker
- Defined in:
- lib/recommendable/rater/bookmarker.rb
Instance Method Summary collapse
-
#bookmark(obj) ⇒ Object
Bookmark an object.
-
#bookmarked_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has bookmarked.
-
#bookmarked_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has bookmarked.
-
#bookmarked_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has bookmarked.
-
#bookmarked_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have bookmarked.
-
#bookmarked_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have bookmarked.
-
#bookmarks ⇒ Array
Retrieve an array of objects the user has bookmarked.
-
#bookmarks?(obj) ⇒ Boolean
Check whether the user has bookmarked an object.
-
#bookmarks_count ⇒ Fixnum
Get the number of items the user has bookmarked.
-
#bookmarks_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have bookmarked.
-
#unbookmark(obj) ⇒ Object
Unbookmark an object.
Instance Method Details
#bookmark(obj) ⇒ Object
Bookmark an object. This is not possible if the object is hidden.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/recommendable/rater/bookmarker.rb', line 9 def bookmark(obj) raise(ArgumentError, 'Object has not been declared ratable.') unless obj.respond_to?(:recommendable?) && obj.recommendable? return if hides?(obj) || bookmarks?(obj) run_hook(:before_bookmark, obj) Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(obj.class, id), obj.id) run_hook(:after_bookmark, obj) true end |
#bookmarked_count_for(klass) ⇒ Fixnum
Get the number of items belonging to a passed class that the user has bookmarked
87 88 89 |
# File 'lib/recommendable/rater/bookmarker.rb', line 87 def bookmarked_count_for(klass) Recommendable.redis.scard(Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(klass, id)) end |
#bookmarked_for(klass) ⇒ Array
Fetch records belonging to a passed class that the user has bookmarked
79 80 81 |
# File 'lib/recommendable/rater/bookmarker.rb', line 79 def bookmarked_for(klass) Recommendable.query(klass, bookmarked_ids_for(klass)) end |
#bookmarked_ids_for(klass) ⇒ Array
Fetch IDs for objects belonging to a passed class that the user has bookmarked
69 70 71 72 73 |
# File 'lib/recommendable/rater/bookmarker.rb', line 69 def bookmarked_ids_for(klass) ids = Recommendable.redis.smembers(Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(klass, id)) ids.map!(&:to_i) if [:active_record, :data_mapper, :sequel].include?(Recommendable.config.orm) ids end |
#bookmarked_ids_in_common_with(klass, user_id) ⇒ Array
Get a list of IDs for records that both this user and a passed user have bookmarked
107 108 109 110 |
# File 'lib/recommendable/rater/bookmarker.rb', line 107 def bookmarked_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.bookmarked_set_for(klass, id), Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(klass, user_id)) end |
#bookmarked_in_common_with(klass, user) ⇒ Array
Get a list of records that both this user and a passed user have bookmarked
97 98 99 |
# File 'lib/recommendable/rater/bookmarker.rb', line 97 def bookmarked_in_common_with(klass, user) Recommendable.query(klass, bookmarked_ids_in_common_with(klass, user)) end |
#bookmarks ⇒ Array
Retrieve an array of objects the user has bookmarked
45 46 47 |
# File 'lib/recommendable/rater/bookmarker.rb', line 45 def bookmarks Recommendable.config.ratable_classes.map { |klass| bookmarked_for(klass) }.flatten end |
#bookmarks?(obj) ⇒ Boolean
Check whether the user has bookmarked an object.
24 25 26 |
# File 'lib/recommendable/rater/bookmarker.rb', line 24 def bookmarks?(obj) Recommendable.redis.sismember(Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(obj.class, id), obj.id) end |
#bookmarks_count ⇒ Fixnum
Get the number of items the user has bookmarked
59 60 61 62 63 |
# File 'lib/recommendable/rater/bookmarker.rb', line 59 def bookmarks_count Recommendable.config.ratable_classes.inject(0) do |sum, klass| sum + bookmarked_count_for(klass) end end |
#bookmarks_in_common_with(user) ⇒ Array
Retrieve an array of objects both this user and another user have bookmarked
52 53 54 |
# File 'lib/recommendable/rater/bookmarker.rb', line 52 def bookmarks_in_common_with(user) Recommendable.config.ratable_classes.map { |klass| bookmarked_in_common_with(klass, user) }.flatten end |
#unbookmark(obj) ⇒ Object
Unbookmark an object. This removes the object from a user’s set of bookmarks.
32 33 34 35 36 37 38 39 40 |
# File 'lib/recommendable/rater/bookmarker.rb', line 32 def unbookmark(obj) return unless bookmarks?(obj) run_hook(:before_unbookmark, obj) Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.bookmarked_set_for(obj.class, id), obj.id) run_hook(:after_unbookmark, obj) true end |