Sha256: ea4f01a47db659ed22d56d1140e3e38290215d7f2bb233a24b144a89eb4ddd48
Contents?: true
Size: 1.11 KB
Versions: 2
Compression:
Stored size: 1.11 KB
Contents
class Policy attr_reader :model, :user, :action def initialize model:, user: nil @model = model @user = user || Policy.current_user end # pass block if you want to handle errors yourself # return true if false if block is passed def can? action, *args, &block @action = action .to_s .gsub(/[^\w+]/, '') .concat('?') .to_sym # pre check if %i(can).index(@action) raise RuntimeError.new('Method name not allowed') end unless respond_to?(@action) raise NoMethodError.new(%[Policy check "#{@action}" not found in #{self.class}]) end call *args, &block end def can Proxy.new self end private # call has to be isolated because of specifics in handling def call *args, &block return true if before(@action) == true return true if send(@action, *args) error 'Access disabled in policy' rescue Policy::Error => error message = error.message message += " - #{self.class}##{@action}" if block block.call message false else error message end end def before action false end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
egoist-0.9.0 | ./lib/egoist/base.rb |
egoist-0.8.0 | ./lib/egoist/base.rb |