lib/consul/controller.rb in consul-0.1.2 vs lib/consul/controller.rb in consul-0.2.0
- old
+ new
@@ -6,20 +6,34 @@
base.send :extend, ClassMethods
end
module ClassMethods
+ def current_power_initializer
+ @current_power_initializer || (superclass.respond_to?(:current_power_initializer) && superclass.current_power_initializer)
+ end
+
+ def current_power_initializer=(initializer)
+ @current_power_initializer = initializer
+ end
+
private
def require_power_check(options = {})
before_filter :unchecked_power, options
end
def skip_power_check(options = {})
skip_before_filter :unchecked_power, options
end
+ def current_power(&initializer)
+ self.current_power_initializer = initializer
+ around_filter :with_current_power
+ helper_method :current_power
+ end
+
def power(*args)
args_copy = args.dup
options = args_copy.extract_options!
default_power = args_copy.shift # might be nil
@@ -68,9 +82,24 @@
private
def unchecked_power
raise Consul::UncheckedPower, "This controller does not check against a power"
+ end
+
+ def current_power
+ @current_power_class && @current_power_class.current
+ end
+
+ def with_current_power(&action)
+ power = instance_eval(&self.class.current_power_initializer) or raise Consul::Error, 'current_power initializer returned nil'
+ @current_power_class = power.class
+ @current_power_class.current = power
+ action.call
+ ensure
+ if @current_power_class
+ @current_power_class.current = nil
+ end
end
end
end