Class: Checkpoint::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/checkpoint/agent.rb,
lib/checkpoint/agent/token.rb,
lib/checkpoint/agent/resolver.rb

Overview

An Agent is an any person or entity that might be granted various permission, such as a user, group, or institution.

The application objects that an agent represents may be of any type; this is more of an interface or role than a base class. The important concept is that permits are granted to agents, and that agents may be representative of multiple concrete actors, such as any person affiliated with a given institution or any member of a given group.

Defined Under Namespace

Classes: Resolver, Token

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor) ⇒ Agent

Create an Agent. This should not be called externally; use from instead.



19
20
21
# File 'lib/checkpoint/agent.rb', line 19

def initialize(actor)
  @actor = actor
end

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



16
17
18
# File 'lib/checkpoint/agent.rb', line 16

def actor
  @actor
end

Class Method Details

.from(actor) ⇒ Agent

Default conversion from an actor to an Checkpoint::Agent.

If the actor implements #to_agent, we will delegate to it. Otherwise, we check if the actor implements #agent_type or #agent_id; if so, we use them as the type and id, respectively. If not, we use the actor’s class name as the type and call #id for the id. If the actor does not implement any of the ways to supply an #id, an error will be raised.

Returns:

  • (Agent)

    the actor converted to an agent



32
33
34
35
36
37
38
# File 'lib/checkpoint/agent.rb', line 32

def self.from(actor)
  if actor.respond_to?(:to_agent)
    actor.to_agent
  else
    new(actor)
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Check whether two Agents refer to the same concrete actor.

Parameters:

  • other (Agent)

    Another Agent to compare with

Returns:

  • (Boolean)

    true when the other Agent’s actor is the same as determined by comparing them with ‘==`.



89
90
91
# File 'lib/checkpoint/agent.rb', line 89

def ==(other)
  other.is_a?(Agent) && actor == other.actor
end

#eql?(other) ⇒ Boolean

Check whether two Agents refer to the same concrete actor.

Parameters:

  • other (Agent)

    Another Agent to compare with

Returns:

  • (Boolean)

    true when the other Agent’s actor is the same as determined by comparing them with ‘#eql?`.



81
82
83
# File 'lib/checkpoint/agent.rb', line 81

def eql?(other)
  other.is_a?(Agent) && actor.eql?(other.actor)
end

#idString

Get the captive actor’s id.

If the entity implements ‘#to_agent`, we will call that and use the returned agent’s id. If not, but it implements ‘#agent_id`, we will use that. Otherwise, we call `#id`. If the the actor does not implement any of these methods, we raise a NoIdentifierError.

Returns:

  • (String)

    the name of the actor’s type after calling ‘#to_s` on it.



63
64
65
66
67
68
69
70
71
# File 'lib/checkpoint/agent.rb', line 63

def id
  if actor.respond_to?(:agent_id)
    actor.agent_id
  elsif actor.respond_to?(:id)
    actor.id
  else
    raise NoIdentifierError, "No usable identifier on actor of type: #{actor.class}"
  end.to_s
end

#tokenObject



73
74
75
# File 'lib/checkpoint/agent.rb', line 73

def token
  @token ||= Token.new(type, id)
end

#typeString

Get the captive actor’s type.

If the entity implements ‘#to_agent`, we will call that and use the returned agent’s type. If not, but it implements ‘#agent_type`, we will use that. Otherwise, we use the actors’s class name.

Returns:

  • (String)

    the name of the actor’s type after calling ‘#to_s` on it.



47
48
49
50
51
52
53
# File 'lib/checkpoint/agent.rb', line 47

def type
  if actor.respond_to?(:agent_type)
    actor.agent_type
  else
    actor.class
  end.to_s
end