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

#assigned_player(role) ⇒ Object

Get the object playing the given role



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

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

#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

Returns:

  • (Boolean)


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

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