Module: RTM::AR::Sugar::Role::Counterparts

Included in:
Association
Defined in:
lib/rtm/activerecord/sugar/role/counterparts.rb

Instance Method Summary collapse

Instance Method Details

#counterpartObject

Fetches the other role, if the parent association is binary.



27
28
29
30
31
32
# File 'lib/rtm/activerecord/sugar/role/counterparts.rb', line 27

def counterpart
  n = self.parent.roles.size
  raise "Association must be unary or binary to use counterpart method. Please use counterparts for n-ary associations." if n > 2
  return nil if n == 1
  self.counterparts.first
end

#counterparts(filter = {}) ⇒ Object

Fetches all roles from the parent association of this role, except itself (i.e. current role). A filter-hash may be used to filter for the type of the other role (:otype =>topic_reference). Examples: this_role.counterparts # returns all this_role.counterparts(:otype => some_topic) # returns only other roles which have type some_topic this_role.counterparts(:otype => “some_reference”) # as above, looking up the reference first



15
16
17
18
# File 'lib/rtm/activerecord/sugar/role/counterparts.rb', line 15

def counterparts(filter={})
  self.parent.roles.reject{|r| r.id==self.id}.
              select{|r| filter[:otype] ? r.type == self.topic_map.get(filter[:otype]) : true}
end

#counterplayerObject

Fetches the player of the other role, if the parent association is binary.



35
36
37
38
39
40
# File 'lib/rtm/activerecord/sugar/role/counterparts.rb', line 35

def counterplayer
  n = self.parent.roles.size
  raise "Association must be unary or binary to use counterplayer method. Please use counterplayers for n-ary associations." if n > 2
  return nil if n == 1
  self.counterparts.first.player
end

#counterplayers(*args) ⇒ Object

This methods fetches all players of the parent association of this role, except the player of itself. It accepts a filter-hash like the counterparts-method.



22
23
24
# File 'lib/rtm/activerecord/sugar/role/counterparts.rb', line 22

def counterplayers(*args)
  self.counterparts(*args).map{|r| r.player}
end

#peerplayersObject

Fetches all players of roles being in the same association with another topic as self Example: current_role is a role of player “me” and type “employee” current_role.peerplayers # returns all employees of my company (including me)



51
52
53
# File 'lib/rtm/activerecord/sugar/role/counterparts.rb', line 51

def peerplayers
  self.peers.map{|r| r.player}
end

#peersObject

Fetches all roles of the (binary) parent associations which play the same role in the same with the counterplayer TODO: filter not only otype but also atype and rtype.



44
45
46
47
# File 'lib/rtm/activerecord/sugar/role/counterparts.rb', line 44

def peers
  cp = self.counterpart
  cp.player.roles.select{|r| r.type == cp.type}.map{|r| r.counterpart}
end