lib/surrounded/context.rb in surrounded-0.7.2 vs lib/surrounded/context.rb in surrounded-0.7.3

- old
+ new

@@ -1,8 +1,9 @@ require 'set' require 'surrounded/context/role_map' require 'surrounded/access_control' +require 'surrounded/shortcuts' # Some features are only available in versions of Ruby # where this method is true def module_method_rebinding? return @__module_method_rebinding__ if defined?(@__module_method_rebinding__) @@ -19,25 +20,12 @@ def self.extended(base) base.class_eval { @triggers = Set.new 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 - end - - def protect_triggers - self.extend(::Surrounded::AccessControl) - end - def new(*args, &block) instance = allocate instance.send(:preinitialize) instance.send(:initialize, *args, &block) instance.send(:postinitialize) @@ -48,10 +36,22 @@ @triggers.dup end private + def self.default_role_type + @default_role_type ||= :module + end + + class << self + attr_writer :default_role_type + end + + # Additional Features + def protect_triggers; self.extend(::Surrounded::AccessControl); end + def shortcut_triggers; self.extend(::Surrounded::Shortcuts); end + def private_const_set(name, const) const = const_set(name, const) private_constant name.to_sym const end @@ -113,31 +113,26 @@ class_eval " def initialize(#{setup_args.join(',')}) preinitialize arguments = method(__method__).parameters.map{|arg| eval(arg[1].to_s) } + @role_map = RoleMap.new map_roles(#{setup_args}.zip(arguments)) postinitialize end - " + ", __FILE__, __LINE__ end def private_attr_reader(*method_names) attr_reader(*method_names) private(*method_names) end 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 + define_trigger_method(name, &block) end else name = names.first define_method(:"__trigger_#{name}", &block) @@ -156,10 +151,20 @@ def role_const(name) if const_defined?(name) const_get(name) end end + + def define_trigger_method(name, &block) + 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 def redo_method(name) class_eval %{ def #{name} begin @@ -169,10 +174,10 @@ ensure remove_roles if __apply_role_policy == :trigger end end - } + }, __FILE__, __LINE__ end module InstanceMethods def role?(name, &block) return false unless role_map.role?(name) \ No newline at end of file