Module: Amico::Relationships
- Included in:
- Amico
- Defined in:
- lib/amico/relationships.rb
Instance Method Summary collapse
-
#accept(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Accept a relationship that is pending between two IDs.
-
#all(id, type, scope = Amico.default_scope_key) ⇒ Object
Retrieve all of the individuals for a given id, type (e.g. following) and scope.
-
#block(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Block a relationship between two IDs.
-
#blocked(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of blocked individuals for a given ID.
-
#blocked?(id, blocked_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual has blocked another individual.
-
#blocked_by(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of individuals who have blocked a given ID.
-
#blocked_by?(id, blocked_by_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual is blocked by another individual.
-
#blocked_by_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals blocking another.
-
#blocked_by_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of blocked_by relationships for an individual.
-
#blocked_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that someone has blocked.
-
#blocked_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of blocked relationships for an individual.
-
#clear(id, scope = Amico.default_scope_key) ⇒ Object
Clears all relationships (in either direction) stored for an individual.
-
#count(id, type, scope = Amico.default_scope_key) ⇒ Object
Count of all of a given type of relationship for the specified id.
-
#deny(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Deny a relationship that is pending between two IDs.
-
#follow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Establish a follow relationship between two IDs.
-
#follower?(id, follower_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual is a follower of another individual.
-
#followers(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of followers for a given ID.
-
#followers_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that are following someone.
-
#followers_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of follower relationships for an individual.
-
#following(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of followed individuals for a given ID.
-
#following?(id, following_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual is following another individual.
-
#following_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that someone is following.
-
#following_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of following relationships for an individual.
-
#page_count(id, type, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Page count of a given type of relationship for the specified id.
-
#pending(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of pending relationships for a given ID.
-
#pending?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual has a pending relationship in following another individual.
-
#pending_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of relationships pending for an individual.
-
#pending_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of pending relationships for an individual.
-
#pending_with(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of individuals that are waiting to approve the given ID.
-
#pending_with?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual has a pending relationship with another.
-
#pending_with_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of relationships an individual has pending with another.
-
#pending_with_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of individuals waiting to approve another individual.
-
#reciprocated(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
Retrieve a page of individuals that have reciprocated a follow for a given ID.
-
#reciprocated?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
Check to see if one individual has reciprocated in following another individual.
-
#reciprocated_count(id, scope = Amico.default_scope_key) ⇒ Object
Count the number of individuals that have reciprocated a following relationship.
-
#reciprocated_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Count the number of pages of reciprocated relationships for an individual.
-
#unblock(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Unblock a relationship between two IDs.
-
#unfollow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Remove a follow relationship between two IDs.
Instance Method Details
#accept(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
113 114 115 116 117 |
# File 'lib/amico/relationships.rb', line 113 def accept(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id add_following_followers_reciprocated(from_id, to_id, scope) end |
#all(id, type, scope = Amico.default_scope_key) ⇒ Object
Retrieve all of the individuals for a given id, type (e.g. following) and scope
634 635 636 637 638 |
# File 'lib/amico/relationships.rb', line 634 def all(id, type, scope = Amico.default_scope_key) validate_relationship_type(type) count = self.send("#{type.to_s}_count".to_sym, id, scope) count > 0 ? self.send("#{type}", id, {:page_size => count}, scope) : [] end |
#block(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Block a relationship between two IDs. This method also has the side effect of removing any follower or following relationship between the two IDs.
Examples
Amico.block(1, 11)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/amico/relationships.rb', line 64 def block(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id Amico.redis.multi do Amico.redis.zrem("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{to_id}", from_id) Amico.redis.zadd("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{from_id}", Time.now.to_i, to_id) Amico.redis.zadd("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{to_id}", Time.now.to_i, from_id) end end |
#blocked(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
434 435 436 |
# File 'lib/amico/relationships.rb', line 434 def blocked(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", ) end |
#blocked?(id, blocked_id, scope = Amico.default_scope_key) ⇒ Boolean
318 319 320 |
# File 'lib/amico/relationships.rb', line 318 def blocked?(id, blocked_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", blocked_id).nil? end |
#blocked_by(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
451 452 453 |
# File 'lib/amico/relationships.rb', line 451 def blocked_by(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{id}", ) end |
#blocked_by?(id, blocked_by_id, scope = Amico.default_scope_key) ⇒ Boolean
334 335 336 |
# File 'lib/amico/relationships.rb', line 334 def blocked_by?(id, blocked_by_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{id}", blocked_by_id).nil? end |
#blocked_by_count(id, scope = Amico.default_scope_key) ⇒ Object
222 223 224 |
# File 'lib/amico/relationships.rb', line 222 def blocked_by_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{id}") end |
#blocked_by_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
572 573 574 |
# File 'lib/amico/relationships.rb', line 572 def blocked_by_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{id}", page_size) end |
#blocked_count(id, scope = Amico.default_scope_key) ⇒ Object
207 208 209 |
# File 'lib/amico/relationships.rb', line 207 def blocked_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}") end |
#blocked_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
555 556 557 |
# File 'lib/amico/relationships.rb', line 555 def blocked_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{id}", page_size) end |
#clear(id, scope = Amico.default_scope_key) ⇒ Object
Clears all relationships (in either direction) stored for an individual. Helpful to prevent orphaned associations when deleting users.
Examples
Amico.follow(1, 11)
Amico.followers_count(11) => 1
Amico.clear(1)
Amico.followers_count(11) => 0
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/amico/relationships.rb', line 153 def clear(id, scope = Amico.default_scope_key) # no longer following (or followed by) anyone clear_bidirectional_sets_for_id(id, Amico.following_key, Amico.followers_key, scope) clear_bidirectional_sets_for_id(id, Amico.followers_key, Amico.following_key, scope) clear_bidirectional_sets_for_id(id, Amico.reciprocated_key, Amico.reciprocated_key, scope) # no longer blocked by (or blocking) anyone clear_bidirectional_sets_for_id(id, Amico.blocked_by_key, Amico.blocked_key, scope) clear_bidirectional_sets_for_id(id, Amico.blocked_key, Amico.blocked_by_key, scope) # no longer pending with anyone (or have any pending followers) clear_bidirectional_sets_for_id(id, Amico.pending_with_key, Amico.pending_key, scope) clear_bidirectional_sets_for_id(id, Amico.pending_key, Amico.pending_with_key, scope) end |
#count(id, type, scope = Amico.default_scope_key) ⇒ Object
Returns Count of all of a given type of relationship for the specified id.
647 648 649 650 |
# File 'lib/amico/relationships.rb', line 647 def count(id, type, scope = Amico.default_scope_key) validate_relationship_type(type) self.send("#{type.to_s}_count".to_sym, id, scope) end |
#deny(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/amico/relationships.rb', line 132 def deny(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id Amico.redis.multi do Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{from_id}", to_id) end end |
#follow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
Establish a follow relationship between two IDs. After adding the follow relationship, it checks to see if the relationship is reciprocated and establishes that relationship if so.
Examples
Amico.follow(1, 11)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/amico/relationships.rb', line 14 def follow(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id return if blocked?(to_id, from_id, scope) return if Amico.pending_follow && pending?(from_id, to_id, scope) if Amico.pending_follow Amico.redis.multi do |transaction| transaction.zadd("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", Time.now.to_i, from_id) transaction.zadd("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{from_id}", Time.now.to_i, to_id) end else add_following_followers_reciprocated(from_id, to_id, scope) end end |
#follower?(id, follower_id, scope = Amico.default_scope_key) ⇒ Boolean
302 303 304 |
# File 'lib/amico/relationships.rb', line 302 def follower?(id, follower_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", follower_id).nil? end |
#followers(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
417 418 419 |
# File 'lib/amico/relationships.rb', line 417 def followers(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", ) end |
#followers_count(id, scope = Amico.default_scope_key) ⇒ Object
192 193 194 |
# File 'lib/amico/relationships.rb', line 192 def followers_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}") end |
#followers_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
538 539 540 |
# File 'lib/amico/relationships.rb', line 538 def followers_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{id}", page_size) end |
#following(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
400 401 402 |
# File 'lib/amico/relationships.rb', line 400 def following(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", ) end |
#following?(id, following_id, scope = Amico.default_scope_key) ⇒ Boolean
286 287 288 |
# File 'lib/amico/relationships.rb', line 286 def following?(id, following_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", following_id).nil? end |
#following_count(id, scope = Amico.default_scope_key) ⇒ Object
177 178 179 |
# File 'lib/amico/relationships.rb', line 177 def following_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}") end |
#following_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
521 522 523 |
# File 'lib/amico/relationships.rb', line 521 def following_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{id}", page_size) end |
#page_count(id, type, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
Returns Page count of a given type of relationship for the specified id.
660 661 662 663 |
# File 'lib/amico/relationships.rb', line 660 def page_count(id, type, page_size = Amico.page_size, scope = Amico.default_scope_key) validate_relationship_type(type) self.send("#{type.to_s}_page_count".to_sym, id, page_size, scope) end |
#pending(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
487 488 489 |
# File 'lib/amico/relationships.rb', line 487 def pending(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", ) end |
#pending?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
367 368 369 |
# File 'lib/amico/relationships.rb', line 367 def pending?(from_id, to_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", from_id).nil? end |
#pending_count(id, scope = Amico.default_scope_key) ⇒ Object
254 255 256 |
# File 'lib/amico/relationships.rb', line 254 def pending_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}") end |
#pending_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
608 609 610 |
# File 'lib/amico/relationships.rb', line 608 def pending_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{id}", page_size) end |
#pending_with(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
504 505 506 |
# File 'lib/amico/relationships.rb', line 504 def pending_with(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{id}", ) end |
#pending_with?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
383 384 385 |
# File 'lib/amico/relationships.rb', line 383 def pending_with?(from_id, to_id, scope = Amico.default_scope_key) !Amico.redis.zscore("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{to_id}", from_id).nil? end |
#pending_with_count(id, scope = Amico.default_scope_key) ⇒ Object
270 271 272 |
# File 'lib/amico/relationships.rb', line 270 def pending_with_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{id}") end |
#pending_with_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
625 626 627 |
# File 'lib/amico/relationships.rb', line 625 def pending_with_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{id}", page_size) end |
#reciprocated(id, page_options = default_paging_options, scope = Amico.default_scope_key) ⇒ Object
470 471 472 |
# File 'lib/amico/relationships.rb', line 470 def reciprocated(id, = , scope = Amico.default_scope_key) members("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}", ) end |
#reciprocated?(from_id, to_id, scope = Amico.default_scope_key) ⇒ Boolean
351 352 353 |
# File 'lib/amico/relationships.rb', line 351 def reciprocated?(from_id, to_id, scope = Amico.default_scope_key) following?(from_id, to_id, scope) && following?(to_id, from_id, scope) end |
#reciprocated_count(id, scope = Amico.default_scope_key) ⇒ Object
238 239 240 |
# File 'lib/amico/relationships.rb', line 238 def reciprocated_count(id, scope = Amico.default_scope_key) Amico.redis.zcard("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}") end |
#reciprocated_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) ⇒ Object
591 592 593 |
# File 'lib/amico/relationships.rb', line 591 def reciprocated_page_count(id, page_size = Amico.page_size, scope = Amico.default_scope_key) total_pages("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{id}", page_size) end |
#unblock(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/amico/relationships.rb', line 91 def unblock(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id Amico.redis.multi do Amico.redis.zrem("#{Amico.namespace}:#{Amico.blocked_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.blocked_by_key}:#{scope}:#{to_id}", from_id) end end |
#unfollow(from_id, to_id, scope = Amico.default_scope_key) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/amico/relationships.rb', line 41 def unfollow(from_id, to_id, scope = Amico.default_scope_key) return if from_id == to_id Amico.redis.multi do Amico.redis.zrem("#{Amico.namespace}:#{Amico.following_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.followers_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{from_id}", to_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.reciprocated_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_key}:#{scope}:#{to_id}", from_id) Amico.redis.zrem("#{Amico.namespace}:#{Amico.pending_with_key}:#{scope}:#{from_id}", to_id) end end |