Module: Recommendable::Rater::Bookmarker
- Defined in:
- lib/recommendable/rater/bookmarker.rb
Instance Method Summary collapse
-
#bookmark(obj) ⇒ Object
Bookmark an object.
-
#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 |
#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 |