lib/stasi/authorization/law.rb in stasi-0.0.1.alpha vs lib/stasi/authorization/law.rb in stasi-0.1.0

- old
+ new

@@ -3,50 +3,59 @@ class Law #--- Class methods ---# include Robotnik::DslEval - - cattr_reader :law def self.define &block @@law = self.new.evaluate(&block) end def self.reset! @@law = nil end + def self.law + @@law + end + #--- Instance methods ---# + attr_reader :statuses + + def initialize + @statuses = [] + end + def status method, &block init_role_for(method).evaluate &block end - def statuses - rules.keys + def default &block + status :default, &block end - def can? *args - subject, args = args.shift, args + def can? action, resource, options verdict = false statuses.each do |status| - if subject.send status - verdict = rules[status].can? *args - break + if status == :default || status.to_proc.call(options[:agent]) + verdict = rules[status].can? action.to_sym, resource, options end end verdict end private def rules - @rules ||= {}.with_indifferent_access + @rules ||= {} end def init_role_for method - rules[method] ||= Robotnik::Authorization::Status.new + rules[method] ||= begin + @statuses << method + Robotnik::Authorization::Status.new + end end end end end