Class: Waylon::Group

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

Overview

Note:

This class can be subclassed for external authentication mechanisms per Sense

The basic, built-in Group class. Relies on Redis for storage and is managed directly by Waylon.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Group

Returns a new instance of Group.

Parameters:

  • name (String, Symbol)

    The name of the group



10
11
12
# File 'lib/waylon/group.rb', line 10

def initialize(name)
  @name = name.to_sym
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/waylon/group.rb', line 7

def name
  @name
end

Instance Method Details

#add(user) ⇒ Boolean

Add a user to the group

Parameters:

  • user (User)

    User to add

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
# File 'lib/waylon/group.rb', line 17

def add(user)
  return false if include?(user)

  users = members
  users << user.email.downcase
  storage.store(key, users)
  true
end

#include?(user) ⇒ Boolean

Checks if a user a member

Parameters:

  • user (User)

    User to look for

Returns:

  • (Boolean)


52
53
54
# File 'lib/waylon/group.rb', line 52

def include?(user)
  members.include?(user.email.downcase)
end

#membersArray<User> Also known as: to_a

Waylon Users in this group

Returns:

  • (Array<User>)

    The members of this Group



40
41
42
43
44
45
# File 'lib/waylon/group.rb', line 40

def members
  # all actions on Group funnel through here, so always make sure the key exists first
  storage.store(key, []) unless storage.key?(key)

  storage.load(key).sort.uniq
end

#remove(user) ⇒ Boolean

Remove a user from the group

Parameters:

  • user (User)

    User to remove

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/waylon/group.rb', line 29

def remove(user)
  return false unless include?(user)

  users = members
  users.delete(user.email.downcase)
  storage.store(key, users)
  true
end