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

#bookmarked_count_for(klass) ⇒ Fixnum

Get the number of items belonging to a passed class that the user has bookmarked

Parameters:

  • the (String, Symbol, Class)

    class for which you want a count of bookmarks

Returns:

  • (Fixnum)

    the number of bookmarks



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

Parameters:

  • the (String, Symbol, Class)

    class for which you want bookmarked records

Returns:

  • (Array)

    an array of bookmarked records



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

Parameters:

  • the (String, Symbol, Class)

    class for which you want IDs

Returns:

  • (Array)

    an array of IDs



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

Parameters:

  • the (User)

    other user

  • the (String, Symbol, Class)

    class of common bookmarked items

Returns:

  • (Array)

    an array of IDs for records that both users 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

Parameters:

  • the (User)

    other user

  • the (String, Symbol, Class)

    class of common bookmarked items

Returns:

  • (Array)

    an array of records both users 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

#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