Module: Recommendable::Rater::Disliker

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

Instance Method Summary collapse

Instance Method Details

#dislike(obj) ⇒ Object

Dislike an object. This will remove the item from a user’s set of likes or hidden items

Parameters:

  • obj (Object)

    the object to be disliked

Returns:

  • true if object was disliked successfully

Raises:

  • (ArgumentError)

    if the passed object was not declared ratable



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/recommendable/rater/disliker.rb', line 10

def dislike(obj)
  raise(ArgumentError, 'Object has not been declared ratable.') unless obj.respond_to?(:recommendable?) && obj.recommendable?
  return if dislikes?(obj)

  run_hook(:before_dislike, obj)
  Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(obj.class, id), obj.id)
  Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.disliked_by_set_for(obj.class, obj.id), id)
  run_hook(:after_dislike, obj)

  true
end

#disliked_count_for(klass) ⇒ Fixnum

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

Parameters:

  • the (String, Symbol, Class)

    class for which you want a count of dislikes

Returns:

  • (Fixnum)

    the number of dislikes



90
91
92
# File 'lib/recommendable/rater/disliker.rb', line 90

def disliked_count_for(klass)
  Recommendable.redis.scard(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(klass, id))
end

#disliked_for(klass) ⇒ Array

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

Parameters:

  • the (String, Symbol, Class)

    class for which you want disliked records

Returns:

  • (Array)

    an array of disliked records



82
83
84
# File 'lib/recommendable/rater/disliker.rb', line 82

def disliked_for(klass)
  Recommendable.query(klass, disliked_ids_for(klass))
end

#disliked_ids_for(klass) ⇒ Array

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

Parameters:

  • the (String, Symbol, Class)

    class for which you want IDs

Returns:

  • (Array)

    an array of IDs



72
73
74
75
76
# File 'lib/recommendable/rater/disliker.rb', line 72

def disliked_ids_for(klass)
  ids = Recommendable.redis.smembers(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(klass, id))
  ids.map!(&:to_i) if [:active_record, :data_mapper, :sequel].include?(Recommendable.config.orm)
  ids
end

#disliked_ids_in_common_with(klass, user_id) ⇒ Array

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

Parameters:

  • the (User, Fixnum)

    other user (or its ID)

  • the (String, Symbol, Class)

    class of common disliked items

Returns:

  • (Array)

    an array of IDs for records that both users have disliked



109
110
111
112
# File 'lib/recommendable/rater/disliker.rb', line 109

def disliked_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.disliked_set_for(klass, id), Recommendable::Helpers::RedisKeyMapper.disliked_set_for(klass, user_id))
end

#disliked_in_common_with(klass, user) ⇒ Array

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

Parameters:

  • the (User)

    other user

  • the (String, Symbol, Class)

    class of common disliked items

Returns:

  • (Array)

    an array of records both users have disliked



99
100
101
# File 'lib/recommendable/rater/disliker.rb', line 99

def disliked_in_common_with(klass, user)
  Recommendable.query(klass, disliked_ids_in_common_with(klass, user))
end

#dislikesArray

Retrieve an array of objects the user has disliked

Returns:

  • (Array)

    an array of records



48
49
50
# File 'lib/recommendable/rater/disliker.rb', line 48

def dislikes
  Recommendable.config.ratable_classes.map { |klass| disliked_for(klass) }.flatten
end

#dislikes?(obj) ⇒ Boolean

Check whether the user has disliked an object.

Parameters:

  • obj (Object)

    the object in question

Returns:

  • (Boolean)

    true if the user has disliked obj, false if not



26
27
28
# File 'lib/recommendable/rater/disliker.rb', line 26

def dislikes?(obj)
  Recommendable.redis.sismember(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(obj.class, id), obj.id)
end

#dislikes_countFixnum

Get the number of items the user has disliked

Returns:

  • (Fixnum)

    the number of disliked items



62
63
64
65
66
# File 'lib/recommendable/rater/disliker.rb', line 62

def dislikes_count
  Recommendable.config.ratable_classes.inject(0) do |sum, klass|
    sum + disliked_count_for(klass)
  end
end

#dislikes_in_common_with(user) ⇒ Array

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

Returns:

  • (Array)

    an array of records



55
56
57
# File 'lib/recommendable/rater/disliker.rb', line 55

def dislikes_in_common_with(user)
  Recommendable.config.ratable_classes.map { |klass| disliked_in_common_with(klass, user) }.flatten
end

#undislike(obj) ⇒ Object

Undislike an object. This removes the object from a user’s set of dislikes.

Parameters:

  • obj (Object)

    the object to be undisliked

Returns:

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



34
35
36
37
38
39
40
41
42
43
# File 'lib/recommendable/rater/disliker.rb', line 34

def undislike(obj)
  return unless dislikes?(obj)

  run_hook(:before_undislike, obj)
  Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.disliked_set_for(obj.class, id), obj.id)
  Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.disliked_by_set_for(obj.class, obj.id), id)
  run_hook(:after_undislike, obj)

  true
end