Class: AccessRoles::Role

Inherits:
ActiveTriples::Resource
  • Object
show all
Includes:
Hydra::Validations
Defined in:
lib/access_roles/role.rb

Overview

The assignment of a role to an agent within a scope.

Constant Summary collapse

DEFAULT_SCOPE =
Roles::RESOURCE_SCOPE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(args = {}) ⇒ Role Also known as: deserialize

Build a Role instance from hash attributes

Examples:

Role.build type: "Curator", agent: "bob", scope: "resource"

Parameters:

  • args (Hash) (defaults to: {})

    the attributes

Returns:

  • (Role)

    the role



30
31
32
33
34
35
36
37
# File 'lib/access_roles/role.rb', line 30

def build(args={})
  new.tap do |role|
    role.attributes = build_attributes(args)
    if role.invalid?
      raise "Invalid #{self.name}: #{role.errors.full_messages.join('; ')}"
    end
  end
end

.from_json(json) ⇒ Role

Deserialize a Role from JSON

Parameters:

  • json (String)

    the JSON string

Returns:

  • (Role)

    the role



44
45
46
# File 'lib/access_roles/role.rb', line 44

def from_json(json)
  deserialize JSON.parse(json)
end

Instance Method Details

#==(other) ⇒ Boolean

Roles are considered equivalent (== and eql?) if they are of the same type and have the same agent and scope.

Parameters:

  • other (Object)

    the object of comparison

Returns:

  • (Boolean)

    the result



70
71
72
73
74
75
76
# File 'lib/access_roles/role.rb', line 70

def ==(other)
  if self.class == other.class
    self.to_h == other.to_h
  else
    super
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/access_roles/role.rb', line 78

def eql?(other)
  (self == other) && (hash == other.hash)
end

#hashObject



82
83
84
# File 'lib/access_roles/role.rb', line 82

def hash
  to_h.hash
end

#in_policy_scope?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/access_roles/role.rb', line 94

def in_policy_scope?
  scope.first == Roles::POLICY_SCOPE
end

#in_resource_scope?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/access_roles/role.rb', line 90

def in_resource_scope?
  scope.first == Roles::RESOURCE_SCOPE
end

#inspectObject



98
99
100
101
# File 'lib/access_roles/role.rb', line 98

def inspect
  "#<#{self.class.name} role_type=#{role_type.first.inspect}, " \
  "agent=#{agent.first.inspect}, scope=#{scope.first.inspect}>"
end

#permissionsArray<Symbol>

Returns the permissions associated with the role

Returns:

  • (Array<Symbol>)

    the permissions



114
115
116
# File 'lib/access_roles/role.rb', line 114

def permissions
  Roles.type_map[role_type.first].permissions
end

#to_hObject Also known as: to_hash, serialize



103
104
105
106
107
108
# File 'lib/access_roles/role.rb', line 103

def to_h
  { "role_type"=>role_type,
     "agent"=>agent,
     "scope"=>scope,
  }
end

#to_sObject



86
87
88
# File 'lib/access_roles/role.rb', line 86

def to_s
  to_h.to_s
end