lib/surrounded/context.rb in surrounded-0.3.0 vs lib/surrounded/context.rb in surrounded-0.3.1

- old
+ new

@@ -7,10 +7,18 @@ def self.extended(base) base.send(:include, InstanceMethods) base.singleton_class.send(:alias_method, :setup, :initialize) end + def self.default_role_type + @default_role_type ||= :module + end + + def self.default_role_type=(type) + @default_role_type = type + end + def new(*) instance = super instance.instance_variable_set('@__apply_role_policy', __apply_role_policy) instance end @@ -19,23 +27,50 @@ @triggers.dup end private + def private_const_set(name, const) + const = const_set(name, const) + private_constant name + const + end + + def default_role_type + @default_role_type ||= Surrounded::Context.default_role_type + end + + def default_role_type=(type) + @default_role_type = type + end + + def role(name, type=nil, &block) + role_type = type || default_role_type + case role_type + when :wrap, :wrapper then wrap(name, &block) + when :interface then interface(name, &block) + when :module then + mod_name = name.to_s.gsub(/(?:^|_)([a-z])/){ $1.upcase } + private_const_set(mod_name, Module.new(&block)) + else + raise InvalidRoleType.new + end + end + 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 = private_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)) + behavior = private_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 @@ -90,10 +125,16 @@ def store_trigger(name) @triggers ||= Set.new @triggers << name end + def role_const(name) + if const_defined?(name) + const_get(name) + end + end + module InstanceMethods def role?(name, &block) return false unless role_map.role?(name) accessor = eval('self', block.binding) role_map.role_player?(accessor) && role_map.assigned_player(name) @@ -126,11 +167,11 @@ def add_interface(role, behavior, object) applicator = behavior.is_a?(Class) ? method(:add_class_interface) : method(:add_module_interface) role_player = applicator.call(object, behavior) - map_role(role, role_module_basename(behavior), role_player) + map_role(role, role_module_basename(behavior), role_player) if behavior role_player.store_context(self) role_player end def add_module_interface(obj, mod) @@ -153,11 +194,11 @@ return object if !remover_name object.remove_context role_player = object.method(remover_name).call - map_role(role, role_module_basename(behavior), role_player) + map_role(role, role_module_basename(behavior), role_player) if behavior role_player end def apply_roles @@ -168,12 +209,12 @@ traverse_map method(:remove_interface) end def traverse_map(applicator) role_map.each do |role, mod_name, object| - if self.class.const_defined?(mod_name) - applicator.call(role, self.class.const_get(mod_name), object) + if role_const_defined?(mod_name) + applicator.call(role, role_const(mod_name), object) end end end def module_extension_methods @@ -196,9 +237,17 @@ role.to_s.gsub(/(?:^|_)([a-z])/) { $1.upcase }.sub(/_\d+/,'') end def role_module_basename(mod) mod.to_s.split('::').last + end + + def role_const(name) + self.class.send(:role_const, name) + end + + def role_const_defined?(name) + self.class.const_defined?(name) end end end end \ No newline at end of file