Class: Surrounded::Context::RoleMap

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/surrounded/context/role_map.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.container_class=(value) ⇒ Object (writeonly)

Sets the attribute container_class

Parameters:

  • value

    the value to set the attribute container_class to.



21
22
23
# File 'lib/surrounded/context/role_map.rb', line 21

def container_class=(value)
  @container_class = value
end

Class Method Details

.from_base(klass = ::Triad) ⇒ Object

Get the role map container and provide an alternative if desired Ex: RoleMap.from_base(SomeCustomContainer)



11
12
13
14
15
16
17
18
19
# File 'lib/surrounded/context/role_map.rb', line 11

def from_base(klass = ::Triad)
  unless const_defined?(:Container)
    role_mapper = Class.new(self)
    role_mapper.container_class = (klass)
    Surrounded::Exceptions.define(role_mapper, exceptions: :ItemNotPresent, namespace: klass)
    const_set(:Container, role_mapper)
  end
  const_get(:Container)
end

Instance Method Details

#apply(role, player) ⇒ Object

Record the behaviored player applied to a role for the duration of a trigger. The assigned domain object stays in the container; the player (a wrapper, or the same object for cast roles) is tracked here.



38
39
40
# File 'lib/surrounded/context/role_map.rb', line 38

def apply(role, player)
  applied[role] = player
end

#assigned_player(role) ⇒ Object

Get the domain object assigned to the given role



61
62
63
# File 'lib/surrounded/context/role_map.rb', line 61

def assigned_player(role)
  values(role).first
end

#containerObject



26
27
28
# File 'lib/surrounded/context/role_map.rb', line 26

def container
  @container ||= self.class.instance_variable_get(:@container_class).new
end

#current_player(role) ⇒ Object

The player a role currently presents: the applied player while a trigger is running, otherwise the assigned domain object.



44
45
46
# File 'lib/surrounded/context/role_map.rb', line 44

def current_player(role)
  applied.fetch(role) { assigned_player(role) }
end

#reset_appliedObject

Forget all applied players, e.g. after a trigger removes behaviors.



49
50
51
# File 'lib/surrounded/context/role_map.rb', line 49

def reset_applied
  applied.clear
end

#role?(role) ⇒ Boolean

Check if a role exists in the map

Returns:

  • (Boolean)


31
32
33
# File 'lib/surrounded/context/role_map.rb', line 31

def role?(role)
  keys.include?(role)
end

#role_player?(object) ⇒ Boolean

Check if an object is playing a role in this map, by identity — whether it is the assigned domain object or the applied player wrapping it.

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/surrounded/context/role_map.rb', line 55

def role_player?(object)
  values.any? { |player| player.equal?(object) } ||
    applied.values.any? { |player| player.equal?(object) }
end