Module: Recommendable::Rater::Hider

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

Instance Method Summary collapse

Instance Method Details

#hidden_countFixnum

Get the number of items the user has hidden

Returns:

  • (Fixnum)

    the number of hidden items



60
61
62
63
64
# File 'lib/recommendable/rater/hider.rb', line 60

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

#hidden_count_for(klass) ⇒ Fixnum

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

Parameters:

  • the (String, Symbol, Class)

    class for which you want a count of hidden items

Returns:

  • (Fixnum)

    the number of hidden items



88
89
90
# File 'lib/recommendable/rater/hider.rb', line 88

def hidden_count_for(klass)
  Recommendable.redis.scard(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(klass, id))
end

#hidden_for(klass) ⇒ Array

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

Parameters:

  • the (String, Symbol, Class)

    class for which you want hidden records

Returns:

  • (Array)

    an array of hidden records



80
81
82
# File 'lib/recommendable/rater/hider.rb', line 80

def hidden_for(klass)
  Recommendable.query(klass, hidden_ids_for(klass))
end

#hidden_ids_for(klass) ⇒ Array

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

Parameters:

  • the (String, Symbol, Class)

    class for which you want IDs

Returns:

  • (Array)

    an array of IDs



70
71
72
73
74
# File 'lib/recommendable/rater/hider.rb', line 70

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

#hidden_ids_in_common_with(klass, user_id) ⇒ Array

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

Parameters:

  • the (User)

    other user

  • the (String, Symbol, Class)

    class of common hidden items

Returns:

  • (Array)

    an array of IDs for records that both users have hidden



107
108
109
110
# File 'lib/recommendable/rater/hider.rb', line 107

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

#hidden_in_common_with(klass, user) ⇒ Array

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

Parameters:

  • the (User)

    other user

  • the (String, Symbol, Class)

    class of common hidden items

Returns:

  • (Array)

    an array of records both users have hidden



97
98
99
# File 'lib/recommendable/rater/hider.rb', line 97

def hidden_in_common_with(klass, user)
  Recommendable.query(klass, hidden_ids_in_common_with(klass, user))
end

#hide(obj) ⇒ Object

Hide an object. This action is only possible if the user has not yet rated or bookmarked the object.

Parameters:

  • obj (Object)

    the object to be hidden

Returns:

  • true if object was hidden successfully

Raises:

  • (ArgumentError)

    if the passed object was not declared ratable



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

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

  run_hook(:before_hide, obj)
  Recommendable.redis.sadd(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(obj.class, id), obj.id)
  run_hook(:after_hide, obj)

  true
end

#hides?(obj) ⇒ Boolean

Check whether the user has hidden an object.

Parameters:

  • obj (Object)

    the object in question

Returns:

  • (Boolean)

    true if the user has hidden obj, false if not



25
26
27
# File 'lib/recommendable/rater/hider.rb', line 25

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

#hidingArray

Retrieve an array of objects the user has hidden

Returns:

  • (Array)

    an array of records



46
47
48
# File 'lib/recommendable/rater/hider.rb', line 46

def hiding
  Recommendable.config.ratable_classes.map { |klass| hidden_for(klass) }.flatten
end

#hiding_in_common_with(user) ⇒ Array

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

Returns:

  • (Array)

    an array of records



53
54
55
# File 'lib/recommendable/rater/hider.rb', line 53

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

#unhide(obj) ⇒ Object

Unhide an object. This removes the object from a user’s set of hidden items.

Parameters:

  • obj (Object)

    the object to be made visible

Returns:

  • true if the object was successfully made visible, nil if the object wasn’t already hidden



33
34
35
36
37
38
39
40
41
# File 'lib/recommendable/rater/hider.rb', line 33

def unhide(obj)
  return unless hides?(obj)

  run_hook(:before_unhide, obj)
  Recommendable.redis.srem(Recommendable::Helpers::RedisKeyMapper.hidden_set_for(obj.class, id), obj.id)
  run_hook(:after_unhide, obj)

  true
end