Class: Surrounded::Context::RoleMap

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.container_class=(klass) ⇒ Object



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

def container_class=(klass)
  @container_class = klass
end

.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

#assigned_player(role) ⇒ Object

Get the object playing the given role



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

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

#containerObject



28
29
30
# File 'lib/surrounded/context/role_map.rb', line 28

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

#role?(role) ⇒ Boolean

Check if a role exists in the map

Returns:

  • (Boolean)


33
34
35
# File 'lib/surrounded/context/role_map.rb', line 33

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

#role_player?(object) ⇒ Boolean

Check if an object is playing a role in this map

Returns:

  • (Boolean)


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

def role_player?(object)
  !values(object).empty?
rescue self.container.class::ItemNotPresent
  false
end