Module: Friendis::Friendable::InstanceMethods

Defined in:
lib/friendis/friendable.rb

Instance Method Summary collapse

Instance Method Details

#_update_friendis_meta_dataObject



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/friendis/friendable.rb', line 98

def 
  Friendis.redis.multi do
    attr_hash = {}

    self.class.friendis_fields.each do |field|
      attr_hash[field] = self.send(field)
    end
    Friendis.redis.hmset friendis_meta_key, *attr_hash.to_a
  end
  true
end

#approve_friend_request(friend) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/friendis/friendable.rb', line 55

def approve_friend_request(friend)
  if !(Friendis.redis.sismember(friendis_incoming_friend_requests_key, friend.id.to_s))
    return false
  else
    Friendis.redis.multi do
      Friendis.redis.srem friend.friendis_outgoing_friend_requests_key, self.id.to_s
      Friendis.redis.srem friendis_incoming_friend_requests_key, friend.id.to_s
      Friendis.redis.sadd friendis_my_friends_key, friend.id.to_s
      Friendis.redis.sadd friend.friendis_my_friends_key, self.id.to_s
    end
    return true
  end
end

#clear_friendis_dataObject



92
93
94
95
96
# File 'lib/friendis/friendable.rb', line 92

def clear_friendis_data
  [friendis_meta_key, friendis_my_friends_key, friendis_incoming_friend_requests_key, friendis_outgoing_friend_requests_key].each do |friendis_key|
    Friendis.redis.del friendis_key
  end
end

#friendis_incoming_friend_requests_keyObject



118
119
120
# File 'lib/friendis/friendable.rb', line 118

def friendis_incoming_friend_requests_key
  "#{self.class.name.downcase}:#{self.id}:friend_requests"
end

#friendis_meta_key(uuid = nil) ⇒ Object



110
111
112
# File 'lib/friendis/friendable.rb', line 110

def friendis_meta_key(uuid = nil)
  "#{self.class.name.downcase}:#{uuid || self.id}:friendis_meta"
end

#friendis_my_friends_keyObject



114
115
116
# File 'lib/friendis/friendable.rb', line 114

def friendis_my_friends_key
  "#{self.class.name.downcase}:#{self.id}:friends"
end

#friendis_outgoing_friend_requests_keyObject



122
123
124
# File 'lib/friendis/friendable.rb', line 122

def friendis_outgoing_friend_requests_key
  "#{self.class.name.downcase}:#{self.id}:requested_friendship"
end

#friendsObject



76
77
78
# File 'lib/friendis/friendable.rb', line 76

def friends
  Friendis.redis.smembers(friendis_my_friends_key).collect {|friend_id| get_friendis_meta(friend_id)}
end

#get_friendis_meta(uuid = nil) ⇒ Object



44
45
46
# File 'lib/friendis/friendable.rb', line 44

def get_friendis_meta(uuid = nil)
  Friendis.redis.hgetall friendis_meta_key(uuid)
end

#is_friends_with?(friend) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/friendis/friendable.rb', line 80

def is_friends_with?(friend)
  Friendis.redis.sismember friendis_my_friends_key, friend.id.to_s
end

#pending_friend_requestsObject



84
85
86
# File 'lib/friendis/friendable.rb', line 84

def pending_friend_requests
  Friendis.redis.smembers(friendis_incoming_friend_requests_key).collect {|friend_id| get_friendis_meta(friend_id)}
end

#send_friend_request(friend) ⇒ Object



48
49
50
51
52
53
# File 'lib/friendis/friendable.rb', line 48

def send_friend_request(friend)
  Friendis.redis.multi do
    Friendis.redis.sadd friendis_outgoing_friend_requests_key, friend.id.to_s
    Friendis.redis.sadd friend.friendis_incoming_friend_requests_key, self.id.to_s
  end
end

#sent_friend_requestsObject



88
89
90
# File 'lib/friendis/friendable.rb', line 88

def sent_friend_requests
  Friendis.redis.smembers(friendis_outgoing_friend_requests_key).collect {|friend_id| get_friendis_meta(friend_id)}
end

#unfriend(friend) ⇒ Object



69
70
71
72
73
74
# File 'lib/friendis/friendable.rb', line 69

def unfriend(friend)
  Friendis.redis.multi do
    Friendis.redis.srem friendis_my_friends_key, friend.id
    Friendis.redis.srem friend.friendis_my_friends_key, self.id
  end
end