Module: Recommendable::Rater::Bookmarker

Defined in:
lib/recommendable/rater/bookmarker.rb

Instance Method Summary collapse

Instance Method Details

#bookmark(obj) ⇒ Object

Bookmark an object. This is not possible if the object is hidden.

Parameters:

  • obj (Object)

    the object to be bookmarked

Returns:

  • true if object was bookmarked successfully

Raises:

  • (ArgumentError)

    if the passed object was not declared ratable



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

#bookmarksArray

Retrieve an array of objects the user has bookmarked

Returns:

  • (Array)

    an array of records



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.

Parameters:

  • obj (Object)

    the object in question

Returns:

  • (Boolean)

    true if the user has bookmarked obj, false if not



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_countFixnum

Get the number of items the user has bookmarked

Returns:

  • (Fixnum)

    the number of bookmarked items



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

Returns:

  • (Array)

    an array of records



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.

Parameters:

  • obj (Object)

    the object to be unbookmarked

Returns:

  • true if the object was bookmarked successfully, nil if the object wasn’t already bookmarked



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