module Recommendable::Rater::Bookmarker

Public Instance Methods

bookmark(obj) click to toggle source

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

@param [Object] obj the object to be bookmarked @return true if object was bookmarked successfully @raise [ArgumentError] if the passed object was not declared ratable

# 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) click to toggle source

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

@param [String, Symbol, Class] the class for which you want a count of bookmarks @return [Fixnum] the number of bookmarks

# 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) click to toggle source

Fetch records belonging to a passed class that the user has bookmarked

@param [String, Symbol, Class] the class for which you want bookmarked records @return [Array] an array of bookmarked records

# File lib/recommendable/rater/bookmarker.rb, line 79
def bookmarked_for(klass)
  Recommendable.query(klass, bookmarked_ids_for(klass))
end
bookmarked_ids_for(klass) click to toggle source

Fetch IDs for objects belonging to a passed class that the user has bookmarked

@param [String, Symbol, Class] the class for which you want IDs @return [Array] an array of IDs

# 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) click to toggle source

Get a list of IDs for records that both this user and a passed user have bookmarked

@param [User] the other user @param [String, Symbol, Class] the class of common bookmarked items @return [Array] an array of IDs for records that both users have bookmarked

# 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) click to toggle source

Get a list of records that both this user and a passed user have bookmarked

@param [User] the other user @param [String, Symbol, Class] the class of common bookmarked items @return [Array] an array of records both users have bookmarked

# 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() click to toggle source

Retrieve an array of objects the user has bookmarked

@return [Array] an array of records

# File lib/recommendable/rater/bookmarker.rb, line 45
def bookmarks
  Recommendable.config.ratable_classes.map { |klass| bookmarked_for(klass) }.flatten
end
bookmarks?(obj) click to toggle source

Check whether the user has bookmarked an object.

@param [Object] obj the object in question @return true if the user has bookmarked obj, false if not

# 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() click to toggle source

Get the number of items the user has bookmarked

@return [Fixnum] the number of bookmarked items

# 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) click to toggle source

Retrieve an array of objects both this user and another user have bookmarked

@return [Array] an array of records

# 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) click to toggle source

Unbookmark an object. This removes the object from a user's set of bookmarks.

@param [Object] obj the object to be unbookmarked @return true if the object was bookmarked successfully, nil if the object wasn't already bookmarked

# 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