Class: AccessRoles::RoleSet Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/access_roles/role_set.rb

Overview

This class is abstract.

Wraps a set of Roles

Direct Known Subclasses

DetachedRoleSet, PropertyRoleSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role_set) ⇒ RoleSet

Returns a new instance of RoleSet.



22
23
24
# File 'lib/access_roles/role_set.rb', line 22

def initialize(role_set)
  @role_set = role_set
end

Instance Attribute Details

#role_setObject (readonly)

Returns the value of attribute role_set.



13
14
15
# File 'lib/access_roles/role_set.rb', line 13

def role_set
  @role_set
end

Instance Method Details

#==(other) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/access_roles/role_set.rb', line 76

def ==(other)
  if other.is_a? RoleSet
    self.to_set == other.to_set
  else
    super
  end
end

#grant(*roles) ⇒ Object

Grants roles - i.e., adds them to the role set

Examples:

  • default scope (“resource”)

grant type: "Curator", agent: "bob"

  • explicit scope

grant type: "Curator", agent: "sue", scope: "policy"

Parameters:

  • roles (Role, Hash, RoleSet, Array)

    the role(s) to grant

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/access_roles/role_set.rb', line 32

def grant(*roles)
  raise NotImplementedError, "Subclasses must implement `grant`."
end

#granted?(role) ⇒ Boolean

Return true/false depending on whether the role has been granted

Parameters:

  • role (Role, Hash)

    the role

Returns:

  • (Boolean)

    whether the role has been granted



39
40
41
# File 'lib/access_roles/role_set.rb', line 39

def granted?(role)
  include? coerce(role)
end

#replace(*roles) ⇒ Object

Replace the current roles in the role set with new roles

Parameters:

  • roles (Role, Hash, RoleSet, Array)

    the role(s) to grant



53
54
55
56
# File 'lib/access_roles/role_set.rb', line 53

def replace(*roles)
  revoke_all
  grant(*roles)
end

#revoke(*roles) ⇒ Object

Revokes roles - i.e., removes them from the role set

Examples:

revoke type: "Curator", agent: "bob", scope: "resource"

Parameters:

  • roles (Role, Hash, RoleSet, Array)

    the role(s) to revoke

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/access_roles/role_set.rb', line 47

def revoke(*roles)
  raise NotImplementedError, "Subclasses must implement `revoke`."
end

#revoke_allRoleSet

Remove all roles from the role set

Returns:

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/access_roles/role_set.rb', line 60

def revoke_all
  raise NotImplementedError, "Subclasses must implement `revoke_all`."
end

#serializeArray<Hash>

Return the RoleSet serialized as an Array of serialized Roles

Returns:

  • (Array<Hash>)


72
73
74
# File 'lib/access_roles/role_set.rb', line 72

def serialize
  to_a.map(&:serialize)
end

#to_jsonString

Return the RoleSet serialized as JSON

Returns:

  • (String)

    the JSON string



66
67
68
# File 'lib/access_roles/role_set.rb', line 66

def to_json
  serialize.to_json
end