Class: LiveData::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/live_data/group.rb

Overview

Group is used to maintain collection of users

Example

 chat     = LiveData::Channel.new
 guest1   = chat.create_user('guest1')
 guest2   = chat.create_user('guest2')
user_grp = chat.create_group('user')

user_grp.add_user( guest1 )
user_grp.add_user( guest2 )

user_grp.write( { :title => "Greeting", 
   :message => "Wecome to LiveData" } )

group1.read      #  { :title => "Greeti..... }
group2.read      #  { :title => "Greeti..... }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, channel = nil) ⇒ Group

Create a group



25
26
27
28
29
# File 'lib/live_data/group.rb', line 25

def initialize( name = nil, channel = nil )
   @name    = name || self
	 @channel = channel
   @users = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/live_data/group.rb', line 22

def name
  @name
end

#usersObject

Returns the value of attribute users.



21
22
23
# File 'lib/live_data/group.rb', line 21

def users
  @users
end

Instance Method Details

#add_user(user) ⇒ Object

Add user to the group



46
47
48
49
50
51
# File 'lib/live_data/group.rb', line 46

def add_user( user )
   if( !@users.include?(user))
      @users.push( user )
      user.groups.push( self )
   end
end

#destroyObject



59
60
61
62
63
64
65
66
67
# File 'lib/live_data/group.rb', line 59

def destroy
   @users.each{|user|
      user.groups.delete( self )
   }
	 if( @channel )
		 @channel.groups.delete( @name ) 
		 @channel.group_have_users.delete( @name )
	 end
end

#remove_user(user) ⇒ Object

Delete user from the group



54
55
56
57
# File 'lib/live_data/group.rb', line 54

def remove_user( user )
   @users.delete( user )
   user.groups.delete( self )
end

#write(data) ⇒ Object

Write any object



39
40
41
42
43
# File 'lib/live_data/group.rb', line 39

def write( data )
   @users.each{|user|
      user.write( data )
   }
end

#write_yaml(yaml_data) ⇒ Object

Write data, which contain yaml format



32
33
34
35
36
# File 'lib/live_data/group.rb', line 32

def write_yaml( yaml_data )
   @users.each{|user|
      user.write_yaml( yaml_data )
   }
end