Class: GitAuth::Group
- Inherits:
-
SaveableClass
- Object
- SaveableClass
- GitAuth::Group
- Includes:
- Loggable
- Defined in:
- lib/gitauth/group.rb
Instance Attribute Summary collapse
-
#members ⇒ Object
Returns the value of attribute members.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #==(group) ⇒ Object
- #add_member(member) ⇒ Object
- #destroy! ⇒ Object
-
#initialize(name) ⇒ Group
constructor
A new instance of Group.
- #member?(user_or_group, recurse = false, level = 0) ⇒ Boolean
- #remove_member(member) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name) ⇒ Group
Returns a new instance of Group.
26 27 28 29 |
# File 'lib/gitauth/group.rb', line 26 def initialize(name) @name = name @members = [] end |
Instance Attribute Details
#members ⇒ Object
Returns the value of attribute members.
24 25 26 |
# File 'lib/gitauth/group.rb', line 24 def members @members end |
#name ⇒ Object
Returns the value of attribute name.
24 25 26 |
# File 'lib/gitauth/group.rb', line 24 def name @name end |
Class Method Details
.create(name) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/gitauth/group.rb', line 69 def self.create(name) name = name.to_s.strip.gsub(/^@/, "") return false if name.empty? || name !~ /^([\w\_\-\.]+)$/ self.add_item self.new(name) return true end |
.get(name) ⇒ Object
76 77 78 79 80 |
# File 'lib/gitauth/group.rb', line 76 def self.get(name) logger.debug "Getting group named #{name.inspect}" real_name = name.to_s.gsub(/^@/, "") self.all.detect { |g| g.name == real_name } end |
.group?(name) ⇒ Boolean
82 83 84 |
# File 'lib/gitauth/group.rb', line 82 def self.group?(name) name.to_s =~ /^@/ && !get(name).nil? end |
Instance Method Details
#==(group) ⇒ Object
49 50 51 |
# File 'lib/gitauth/group.rb', line 49 def ==(group) group.is_a?(Group) && group.name == self.name end |
#add_member(member) ⇒ Object
39 40 41 42 43 |
# File 'lib/gitauth/group.rb', line 39 def add_member(member) return if member == self @members << member.to_s @members.uniq! end |
#destroy! ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/gitauth/group.rb', line 31 def destroy! GitAuth::Repo.all.each { |r| r.(self) } self.class.all.each { |r| r.remove_member(self) } self.class.all.reject! { |g| g == self } GitAuth::Repo.save! self.class.save! end |
#member?(user_or_group, recurse = false, level = 0) ⇒ Boolean
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gitauth/group.rb', line 53 def member?(user_or_group, recurse = false, level = 0) member = @members.include?(user_or_group.to_s) Thread.current[:checked_groups] = [] if level == 0 if !member return false if level > 0 && Thread.current[:checked_groups].include?(self) Thread.current[:checked_groups] << self member = recurse && @members.map { |m| Group.get(m) }.compact.any? { |g| g.member?(user_or_group, true, level + 1) } end Thread.current[:checked_groups] = nil if level == 0 return member end |
#remove_member(member) ⇒ Object
45 46 47 |
# File 'lib/gitauth/group.rb', line 45 def remove_member(member) @members.reject! { |m| m == member.to_s } end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/gitauth/group.rb', line 65 def to_s "@#{name}" end |