lib/surrounded/context.rb in surrounded-0.5.1 vs lib/surrounded/context.rb in surrounded-0.6.0

- old
+ new

@@ -16,35 +16,23 @@ module Surrounded module Context def self.extended(base) base.class_eval { @triggers = Set.new - @methods_as_triggers = Surrounded::Context.methods_as_triggers include InstanceMethods } base.singleton_class.send(:alias_method, :setup, :initialize) end def self.default_role_type @default_role_type ||= :module end class << self - attr_writer :default_role_type, :methods_as_triggers + attr_writer :default_role_type end - attr_reader :methods_as_triggers - - def self.methods_as_triggers - return @methods_as_triggers if defined?(@methods_as_triggers) - @methods_as_triggers = false - end - - def set_methods_as_triggers - @methods_as_triggers = true - end - def new(*args, &block) instance = allocate instance.send(:preinitialize) instance.send(:initialize, *args, &block) instance.send(:postinitialize) @@ -103,10 +91,11 @@ meth.call(name, &block) end rescue NameError => e raise e.extend(InvalidRoleType) end + alias_method :role_methods, :role def apply_roles_on(which) @__apply_role_policy = which end @@ -130,56 +119,55 @@ def private_attr_reader(*method_names) attr_reader(*method_names) private(*method_names) end - def trigger(name, *args, &block) - store_trigger(name) + def trigger(*names, &block) + if block.nil? + names.each do |name| + unless triggers.include?(name) || name.nil? + alias_method :"__trigger_#{name}", :"#{name}" + private :"__trigger_#{name}" + remove_method :"#{name}" + redo_method(name) + store_trigger(name) + end + end + else + name = names.first - define_method(:"__trigger_#{name}", *args, &block) + define_method(:"__trigger_#{name}", &block) - private :"__trigger_#{name}" + private :"__trigger_#{name}" + store_trigger(name) - redo_method(name, args) + redo_method(name) + end end - def store_trigger(name) - @triggers << name + def store_trigger(*names) + @triggers.merge(names) end def role_const(name) if const_defined?(name) const_get(name) end end - def redo_method(name, args) + def redo_method(name) class_eval %{ - def #{name}(#{args.join(', ')}) + def #{name} begin apply_roles if __apply_role_policy == :trigger - self.send("__trigger_#{name}", #{args.join(', ')}) + self.send("__trigger_#{name}") ensure remove_roles if __apply_role_policy == :trigger end end } - end - - def method_added(name) - if methods_as_triggers - unless name.to_s.match(/^__trigger|initialize/) || (@triggers && triggers.include?(name)) - store_trigger(name) - args = self.instance_method(name).parameters.map{|p| p.last } - alias_method :"__trigger_#{name}", :"#{name}" - private :"__trigger_#{name}" - remove_method :"#{name}" - redo_method(name, args) - end - end - super end module InstanceMethods def role?(name, &block) return false unless role_map.role?(name) \ No newline at end of file