Class: Group
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Group
- Defined in:
- app/models/group.rb
Constant Summary collapse
- MAX_GROUP_NO =
64
Class Method Summary collapse
Instance Method Summary collapse
- #add_member(user) ⇒ Object
-
#add_members(users) ⇒ Object
(also: #members=)
-
users (Array of User) : members to add.
-
- #before_destroy ⇒ Object
- #bit_mask ⇒ Object
- #del_member(user) ⇒ Object
- #del_members(users) ⇒ Object
- #members ⇒ Object
- #save_file ⇒ Object
- #set_id ⇒ Object
Class Method Details
.bit_mask_for(*groups) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/models/group.rb', line 20 def self.bit_mask_for(*groups) ids = Array.new groups.each do |g| case g when Integer id = g ids.push(id) if id>1 and id<MAX_GROUP_NO when Group ids.push(g.id) when String if (gr=Group.find_by_name(g)) # substitution, not == ids.push(gr.id) end else raise ArgumentError, "Unsupported kind of object : #{g.inspect}" end end bitmask = 0 ids.each{|id| bitmask |= (1 << (id-1))} bitmask end |
.find_by_bit_flag(groups) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/group.rb', line 46 def self.find_by_bit_flag(groups) ids = Array.new (1..MAX_GROUP_NO).each do |i| ids.push(i) if ( groups % 2 == 1 ) groups = groups >> 1 end if ids.length == 0 [] elsif ids.length == 1 [self.find(ids[0])] else self.find(*ids) end end |
Instance Method Details
#add_member(user) ⇒ Object
82 83 84 |
# File 'app/models/group.rb', line 82 def add_member(user) add_members([user]) end |
#add_members(users) ⇒ Object Also known as: members=
-
users (Array of User) : members to add
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/group.rb', line 63 def add_members(users) bm = bit_mask User.transaction do users.each do |usr| if String === usr usr = User.find(:first,:conditions=>["login=?",un=usr]) unless usr return [false, "No such a usr #{un}"] end end usr.groups = ( usr.groups | bm ) usr.save! end end return [true, "add members successed"] end |
#before_destroy ⇒ Object
107 108 109 110 111 112 113 |
# File 'app/models/group.rb', line 107 def before_destroy mask = ~bit_mask User.update_all("groups = groups & #{mask}") Node.update_all("rgroups = rgroups & #{mask}") Node.update_all("wgroups = wgroups & #{mask}") true end |
#bit_mask ⇒ Object
16 17 18 |
# File 'app/models/group.rb', line 16 def bit_mask 1 << (id-1) end |
#del_member(user) ⇒ Object
103 104 105 |
# File 'app/models/group.rb', line 103 def del_member(user) del_members([user]) end |
#del_members(users) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/group.rb', line 86 def del_members(users) mask = ~bit_mask User.transaction do users.each do |usr| unless User === usr usr = User.find(:first,:conditions=>["login=?",un=usr]) unless usr return [false, "No such a usr #{un}"] end end usr.groups = ( usr.groups & mask ) usr.save! end end return [true, "delete member successed"] end |
#members ⇒ Object
42 43 44 |
# File 'app/models/group.rb', line 42 def members User.find(:all, :conditions => ["(groups & ?) != 0",bit_mask]) end |
#save_file ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/models/group.rb', line 124 def save_file fname = File.join(RAILS_ROOT,"db","group.yml") if File.exist?(fname) yml = YAML.load(File.read(fname)) File.rename(fname, fname+"."+Time.now.strftime("%Y%m%d%H%M%S")) unless Hash === yml raise "data is invalid" end else yml = Hash.new end hash = Hash.new self.class.column_names.each{|name| hash[name] = self[name] } yml[self.name] = hash File.open(fname,"w"){|file| file.print yml.to_yaml} end |
#set_id ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'app/models/group.rb', line 115 def set_id min_id = ((1..MAX_GROUP_NO).to_a - Group.find(:all).collect{|g| g.id})[0] if min_id.nil? raise("Exceeded max group num (#{MAX_GROUP_NO}). Delete one or more.") end self.id = min_id true end |