lib/surrounded/context.rb in surrounded-0.2.1 vs lib/surrounded/context.rb in surrounded-0.3.0
- old
+ new
@@ -1,43 +1,66 @@
require 'set'
require 'surrounded/context/role_map'
+require 'redcard'
+
module Surrounded
module Context
def self.extended(base)
base.send(:include, InstanceMethods)
base.singleton_class.send(:alias_method, :setup, :initialize)
end
+ def new(*)
+ instance = super
+ instance.instance_variable_set('@__apply_role_policy', __apply_role_policy)
+ instance
+ end
+
def triggers
@triggers.dup
end
- def policy
- @policy ||= :trigger
- end
-
private
def wrap(name, &block)
require 'delegate'
wrapper_name = name.to_s.gsub(/(?:^|_)([a-z])/){ $1.upcase }
klass = const_set(wrapper_name, Class.new(SimpleDelegator, &block))
klass.send(:include, Surrounded)
end
+ if RedCard.check '2.0'
+ def interface(name, &block)
+ class_basename = name.to_s.gsub(/(?:^|_)([a-z])/){ $1.upcase }
+ interface_name = class_basename + 'Interface'
+
+ behavior = const_set(interface_name, Module.new(&block))
+
+ require 'surrounded/context/negotiator'
+ define_method(name) do
+ instance_variable_set("@#{name}", Negotiator.new(role_map.assigned_player(name), behavior))
+ end
+ end
+ end
+
def apply_roles_on(which)
- @policy = which
+ @__apply_role_policy = which
end
+ def __apply_role_policy
+ @__apply_role_policy ||= :trigger
+ end
+
def initialize(*setup_args)
private_attr_reader(*setup_args)
class_eval "
def initialize(#{setup_args.join(',')})
+ @__apply_role_policy = :#{__apply_role_policy}
arguments = method(__method__).parameters.map{|arg| eval(arg[1].to_s) }
map_roles(#{setup_args}.zip(arguments))
- apply_roles if policy == :initialize
+ apply_roles if __apply_role_policy == :initialize
end
"
end
def private_attr_reader(*method_names)
@@ -52,16 +75,16 @@
private :"trigger_#{name}"
define_method(name, *args){
begin
- apply_roles if policy == :trigger
+ apply_roles if __apply_role_policy == :trigger
self.send("trigger_#{name}", *args)
ensure
- remove_roles if policy == :trigger
+ remove_roles if __apply_role_policy == :trigger
end
}
end
def store_trigger(name)
@@ -95,11 +118,11 @@
def map_role(role, mod_name, object)
instance_variable_set("@#{role}", object)
role_map.update(role, mod_name, object)
end
- def policy
- @policy ||= self.class.policy
+ def __apply_role_policy
+ @__apply_role_policy
end
def add_interface(role, behavior, object)
applicator = behavior.is_a?(Class) ? method(:add_class_interface) : method(:add_module_interface)
\ No newline at end of file