Module: Surrounded::Context::RoleBuilders
- Extended by:
- Seclusion
- Defined in:
- lib/surrounded/context/role_builders.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#delegate_class(name, class_name, &block) ⇒ Object
Create a named behavior for a role using the standard library DelegateClass.
-
#interface(name, &block) ⇒ Object
Create an object which will bind methods to the role player.
-
#role(name, type = default_role_type, &block) ⇒ Object
(also: #role_methods)
Define behaviors for your role players.
-
#wrap(name, &block) ⇒ Object
(also: #wrapper)
Create a named behavior for a role using the standard library SimpleDelegator.
Methods included from Seclusion
private_attr_reader, private_const_set
Class Method Details
.extended(base) ⇒ Object
8 9 10 |
# File 'lib/surrounded/context/role_builders.rb', line 8 def self.extended(base) Surrounded::Exceptions.define(base, exceptions: :InvalidRoleType) end |
Instance Method Details
#delegate_class(name, class_name, &block) ⇒ Object
Create a named behavior for a role using the standard library DelegateClass. This ties the implementation of the role to a specific class or module API.
38 39 40 41 42 43 44 |
# File 'lib/surrounded/context/role_builders.rb', line 38 def delegate_class(name, class_name, &block) require 'delegate' wrapper_name = RoleName(name) klass = private_const_set(wrapper_name, DelegateClass(Object.const_get(class_name.to_s))) klass.class_eval(&block) klass.send(:include, Surrounded) end |
#interface(name, &block) ⇒ Object
Create an object which will bind methods to the role player.
This object will behave differently that a wrapper or delegate_class. The interface object should only be used for objects whose methods _will not_ call to the other objects in the context. Because the interface methods are applied individually to an object, that object is unaware of the other objects in the context and cannot access them from any of its methods.
54 55 56 57 58 59 60 61 62 |
# File 'lib/surrounded/context/role_builders.rb', line 54 def interface(name, &block) # AdminInterface interface_name = RoleName(name, 'Interface') behavior = private_const_set(interface_name, Module.new(&block)) require 'surrounded/context/negotiator' # Admin private_const_set(RoleName(name), Negotiator.for_role(behavior)) end |
#role(name, type = default_role_type, &block) ⇒ Object Also known as: role_methods
Define behaviors for your role players
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/surrounded/context/role_builders.rb', line 13 def role(name, type=default_role_type, &block) if type == :module mod_name = RoleName(name) mod = Module.new(&block).send(:include, ::Surrounded) private_const_set(mod_name, mod) else meth = method(type) meth.call(name, &block) end rescue NameError => e raise self::InvalidRoleType, e. end |
#wrap(name, &block) ⇒ Object Also known as: wrapper
Create a named behavior for a role using the standard library SimpleDelegator.
28 29 30 31 32 33 |
# File 'lib/surrounded/context/role_builders.rb', line 28 def wrap(name, &block) require 'delegate' wrapper_name = RoleName(name) klass = private_const_set(wrapper_name, Class.new(SimpleDelegator, &block)) klass.send(:include, Surrounded) end |